// ================================================================
// John Kerl
// kerl.john.r@gmail.com
// 2005-05-02
// ================================================================

import java.util.*;
import java.io.*;

public class TextFileReader {

	// ----------------------------------------------------------------
	// Reads the input stream (nominally, System.in) and returns a
	// vector of strings containing the stream contents, one per line.
	// The strings do not contain the carriage return or line feed.
	public static Vector readInputStream(
		InputStream iStream)
	{
		int  num_bytes_available = 0;
		int  num_bytes_read = 0;
		StringBuffer line = new StringBuffer();
		Vector lines = new Vector();
		int  num_lines = 0;
		byte bytes[] = null;
		int  i;

		try {
			num_bytes_available = iStream.available();
			System.out.println("Num bytes available: " + num_bytes_available);
			System.out.print("Reading text input ... ");
			bytes = new byte[num_bytes_available];
			num_bytes_read = iStream.read(bytes);
			if (num_bytes_read != num_bytes_available) {
				System.out.println("Expected " + num_bytes_available
					+ " bytes from input; got only " + num_bytes_read
					+ ".  Exiting.");
				System.exit(1);
			}
			System.out.println("done.");
		}
		catch (java.io.IOException e) {
			System.out.println("Caught I/O exception.\n");
			System.exit(1);
		}

		System.out.print("Stringifying input ... ");
		for (i = 0; i < num_bytes_read; i++) {
			// This needs to use platform-independent line terminators ...

			//if (bytes[i] == (byte)10) {
			//}
			//else if (bytes[i] == (byte)13) {
			//	lines.addElement(line.toString());
			//	line.delete(0, line.length());
			//	num_lines++;
			//	// if ((num_lines % 100) == 0)
			//	//	 System.out.print(" " + num_lines);
			//}

			if (bytes[i] == (byte)10) {
				lines.addElement(line.toString());
				line.delete(0, line.length());
				num_lines++;
				// if ((num_lines % 100) == 0)
				//	 System.out.print(" " + num_lines);
			}

			else {
				line.append(((char)bytes[i]));
			}
		}
		System.out.println("done.");

		return lines;
	}

	// ----------------------------------------------------------------
	public static void dump(Vector stringList)
	{
		for (int i = 0; i <  stringList.size(); i++) {
			System.out.println("Line: " + i + ": <<" + stringList.elementAt(i)
				+ ">>");
		}
	}
}
