sdsu.io
Class ASCIIInputStream

java.lang.Object
  |
  +--java.io.InputStream
        |
        +--java.io.FilterInputStream
              |
              +--sdsu.io.ASCIIInputStream

public class ASCIIInputStream
extends java.io.FilterInputStream
implements java.io.DataInput

This class provides a way of getting interpreted ASCII input from a stream. The standard Java library doesn't provide any way of reading things like integers from standard input. This functionality is nice to have for interactive programs.

Version:
1.1 12 Sept 1997
Author:
Andrew Scherpbier (andrew@sdsu.edu)

Constructor Summary
ASCIIInputStream(java.io.InputStream stream)
          Creates a new ASCIIInputStream.
 
Method Summary
 boolean bad()
          Deprecated.  
 boolean eof()
          Determines if stream is has reach end of file.
 void flushLine()
          Causes the next I/O operation to start at the beginning of the next input line.
 boolean hadEof()
          Determines if reach end of file in a previous read.
 int read(byte[] b)
          Reads data into an array of bytes.
 int read(byte[] b, int off, int len)
          Reads data into an array of bytes.
 boolean readBoolean()
          Reads an ASCII boolean value.
 byte readByte()
          Reads a single byte.
 char readChar()
          Read an ASCII character and convert it into the internal char format.
 double readDouble()
          Reads an ASCII decimal floating point number.
 float readFloat()
          Reads an ASCII decimal floating point number.
 void readFully(byte[] b)
          Reads bytes, blocking until all bytes are read.
 void readFully(byte[] b, int off, int len)
          Reads bytes, blocking until all bytes are read.
 int readInt()
          Reads an ASCII decimal integer.
 java.lang.String readLine()
          Reads in a line that has been terminated by a \n, \r, \r\n or EOF.
 long readLong()
          Reads an ASCII decimal long.
 short readShort()
          Reads an ASCII decimal short.
 int readUnsignedByte()
          Reads a single unsigned byte.
 int readUnsignedShort()
          Reads an ASCII decimal unsigned short.
 java.lang.String readUTF()
          Does nothing.
 java.lang.String readWord()
          Reads a word.
 int skipBytes(int n)
          Skips bytes, block until all bytes are skipped.
 
Methods inherited from class java.io.FilterInputStream
available, close, mark, markSupported, read, reset, skip
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ASCIIInputStream

public ASCIIInputStream(java.io.InputStream stream)
Creates a new ASCIIInputStream.
Parameters:
stream - the input stream
Method Detail

bad

public boolean bad()
Deprecated.  
Do not use. This method no longer does anything. Always returns false.

hadEof

public boolean hadEof()
               throws java.io.IOException
Determines if reach end of file in a previous read. This differs from eof() in in that eof() will indicate end of file if only white space remains. This function only indicates if you actually reached end of file an a prevoius read. If only whitespace remains and you use readInt (readDouble, etc.) an exception will be thrown. Use this method to detect end of file only if direct access to the white space between tokens is important to your program and you handle the case of a file ending in white space properly.
Returns:
true if end of file was reached.
See Also:
eof()

eof

public boolean eof()
            throws java.io.IOException
Determines if stream is has reach end of file. Consumes white space up to next token. If your program needs that white space between tokens use hadEof() instead. eof() is the prefered way to detect end of file.
Returns:
true if end of file was reached or if only white space remained in the stream.
See Also:
hadEof()

read

public int read(byte[] b)
         throws java.io.IOException
Reads data into an array of bytes. This method blocks until some input is available.
Parameters:
b - the buffer into which the data is read.
Returns:
the actual number of bytes read. -1 is returned when the end of the stream is reached. (In this case, the eof() member will return true.)
Throws:
java.io.IOException - If an I/O error has occurred.
Overrides:
read in class java.io.FilterInputStream

read

public int read(byte[] b,
                int off,
                int len)
         throws java.io.IOException
Reads data into an array of bytes. This method blocks until some input is available.
Parameters:
b - the buffer into which the data is read.
off - the start offset of the data
len - the maximum number of bytes read
Returns:
the actual number of bytes read. -1 is returned when the end of the stream is reached. (In this case, the eof() member will return true.)
Throws:
java.io.IOException - If an I/O error has occurred.
Overrides:
read in class java.io.FilterInputStream

readFully

public void readFully(byte[] b)
               throws java.io.IOException
Reads bytes, blocking until all bytes are read. If EOF is reached, the eof() member will return true.
Specified by:
readFully in interface java.io.DataInput
Parameters:
b - the buffer into which the data is read
Throws:
java.io.IOException - If an I/O error has occurred.

readFully

public void readFully(byte[] b,
                      int off,
                      int len)
               throws java.io.IOException
Reads bytes, blocking until all bytes are read. If EOF is reached, the eof() member will return true.
Specified by:
readFully in interface java.io.DataInput
Parameters:
b - the buffer into which the data is read
off - the start offset of the data
len - the maximum number of bytes read
Throws:
java.io.IOException - If an I/O error has occurred.

skipBytes

public int skipBytes(int n)
              throws java.io.IOException
Skips bytes, block until all bytes are skipped.
Specified by:
skipBytes in interface java.io.DataInput
Parameters:
n - the number of bytes to be skipped
Returns:
the actual number of bytes skipped.
Throws:
java.io.IOException - If an I/O error has occurred.

readBoolean

public boolean readBoolean()
                    throws java.io.IOException
Reads an ASCII boolean value. It reads a word and determines if it represents true or false. Possible values are: true and false. The comparison is case insensitive. The eof() method will return true after this method has attempted to read beyond the end of file.
Specified by:
readBoolean in interface java.io.DataInput
Returns:
the boolean.
Throws:
java.io.IOException - If an I/O error has occurred.
java.lang.NumberFormatException - If the next token in the input stream is not a valid boolean.

readByte

public byte readByte()
              throws java.io.IOException
Reads a single byte. The eof() method will return true after this method has attempted to read beyond the end of file.
Specified by:
readByte in interface java.io.DataInput
Returns:
the byte.
Throws:
java.io.IOException - If an I/O error has occurred.

readUnsignedByte

public int readUnsignedByte()
                     throws java.io.IOException
Reads a single unsigned byte. This is virually the same as readChar(), except that the return value is an int.
Specified by:
readUnsignedByte in interface java.io.DataInput
Returns:
an int representing the unsigned byte.
Throws:
java.io.IOException - If an I/O error has occurred.
See Also:
readChar()

readShort

public short readShort()
                throws java.io.IOException
Reads an ASCII decimal short. Shorts can be preceded by optional whitespace. whitespace is defined as SPACE, TAB, CR, or NL. The eof() method will return true after this method has attempted to read beyond the end of file.
Specified by:
readShort in interface java.io.DataInput
Returns:
the short.
Throws:
java.io.IOException - If an I/O error has occurred.
java.lang.NumberFormatException - If next token is not a valid short. This is a runtime exception, so the compiler does not force you to catch it.
See Also:

readUnsignedShort

public int readUnsignedShort()
                      throws java.io.IOException
Reads an ASCII decimal unsigned short. Unsigned shorts can be preceded by optional whitespace. whitespace is defined as SPACE, TAB, CR, or NL.
Specified by:
readUnsignedShort in interface java.io.DataInput
Returns:
an int representing the unsigned short.
Throws:
java.io.IOException - If an I/O error has occurred.
java.lang.NumberFormatException - If next token is not a valid short. This is a runtime exception, so the compiler does not force you to catch it.
See Also:

readChar

public char readChar()
              throws java.io.IOException
Read an ASCII character and convert it into the internal char format. The eof() method will return true after this method has attempted to read beyond the end of file.
Specified by:
readChar in interface java.io.DataInput
Returns:
the character.
Throws:
java.io.IOException - If an I/O error occurred.

readInt

public int readInt()
            throws java.io.IOException
Reads an ASCII decimal integer. Integers can be preceded by optional whitespace. whitespace is defined as SPACE, TAB, CR, or NL. The eof() method will return true after this method has attempted to read beyond the end of file.
Specified by:
readInt in interface java.io.DataInput
Returns:
the integer.
Throws:
java.io.IOException - If an I/O error has occurred.
java.lang.NumberFormatException - If the next token in the input stream is not a valid int. This is a runtime exception, so the compiler does not force you to catch it.

readLong

public long readLong()
              throws java.io.IOException
Reads an ASCII decimal long. Longs can be preceded by optional whitespace. whitespace is defined as SPACE, TAB, CR, or NL. The eof() method will return true after this method has attempted to read beyond the end of file.
Specified by:
readLong in interface java.io.DataInput
Returns:
the long.
Throws:
java.io.IOException - If an I/O error has occurred.
java.lang.NumberFormatException - If the next token in the input stream is not a valid long. This is a runtime exception, so the compiler does not force you to catch it.

readFloat

public float readFloat()
                throws java.io.IOException
Reads an ASCII decimal floating point number. A floating point number is defined as follows: Floats can be preceded by optional whitespace. whitespace is defined as SPACE, TAB, CR, or NL.
Specified by:
readFloat in interface java.io.DataInput
Returns:
the float.
Throws:
java.io.IOException - If an I/O error has occurred.
java.lang.NumberFormatException - If the next token in the input stream is not a valid float format. Note NumberFormatException is a runtime exception.

readDouble

public double readDouble()
                  throws java.io.IOException
Reads an ASCII decimal floating point number. A floating point number is defined as follows: Doubles can be preceded by optional whitespace. whitespace is defined as SPACE, TAB, CR, or NL.
Specified by:
readDouble in interface java.io.DataInput
Returns:
the double.
Throws:
java.io.IOException - If an I/O error has occurred.
java.lang.NumberFormatException - If the next token in the input stream is not a valid double. This is a runtime exception, so the compiler does not force you to catch it.

readLine

public java.lang.String readLine()
                          throws java.io.IOException
Reads in a line that has been terminated by a \n, \r, \r\n or EOF. The eof() method will return true if the read attempted to go beyond the end of file. The terminating line characters will not be part of the String that is returned.
Specified by:
readLine in interface java.io.DataInput
Returns:
a String copy of the line or null if nothing more could be read.
Throws:
java.io.IOException - If an I/O error has occurred.

readUTF

public java.lang.String readUTF()
                         throws java.io.IOException
Does nothing. Do not use.
Specified by:
readUTF in interface java.io.DataInput

readWord

public java.lang.String readWord()
                          throws java.io.IOException
Reads a word. A word is a string of characters deliminated by whitespace or EOF. A word can be preceded by optional whitespace characters. Whitespace is defined as SPACE, TAB, CR, or NL.
Returns:
the word or null if EOF reached.
Throws:
java.io.IOException - If an I/O error has occurred.

flushLine

public void flushLine()
               throws java.io.IOException
Causes the next I/O operation to start at the beginning of the next input line. Lines are delimited by either cr, cr lf, or lf.
Throws:
java.io.IOException - If an I/O error has occurred.