home *** CD-ROM | disk | FTP | other *** search
/ BURKS 2 / BURKS_AUG97.ISO / BURKS / LANGUAGE / JAVA / NOTES / SOURCE / ascinstr.jav next >
Text File  |  1996-12-20  |  17KB  |  462 lines

  1.  
  2. /*   
  3.     This file defines the Class AsciiInputStream as a subclass of FileterInputStream.
  4.     The AsciiInputStream provides methods for reading data expressed in human-readable
  5.     ASCII text format.
  6.     
  7.     This file also defines three public subclasses of RuntimeException to represent
  8.     errors that can occur during input.
  9.     
  10.     NOTE:  One file is really not supposed to define more than one public class.
  11.            This works with the CodeWarrior version of Java on my Macintosh, but 
  12.            it might be rejected on other platforms.  If you run into this problem,
  13.            you need to put each public class in a separate file.
  14.            
  15.     NOTE:  You should consider this an early, experimental version of this class.
  16.            Don't assume that you can rely on it for anything important.  Also,
  17.            don't expect it to work well if used as one of a number of nested
  18.            FilterInputStreams.
  19.            
  20.     David Eck, November 7, 1996
  21.     
  22. */
  23.  
  24.    
  25. import java.io.*;
  26.  
  27. // First, define the exceptions that can be thrown by this class.  These exceptions
  28. // are subclasses of Runtime exception, so they do not require mandatory error-handling.
  29. // Also, if you prefer, you can turn off excpetions by calling the IOCheck() method.
  30. // In that case, when an exception occurs during processing by one of the methods
  31. // of the AsciiInputStream class, an errorFlag is set but no exception is thrown.
  32. // If you use this alternative error-handling strategy, then you should call the
  33. // checkError() method after each input operation to see whether the operation
  34. // completed successfully.
  35.  
  36. public class AsciiInputException extends RuntimeException {
  37.       // Represents any excpetion that occurs in the AsciiInputStream Class.
  38.       // In fact, only general IOExceptions are translated directly into
  39.       // AsciiInputExcpetions.  Other exceptions throw objects belonging
  40.       // to one of the following subclasses of AsciiInputExcpetion.
  41.    AsciiInputException(String errorMessage) {
  42.       super(errorMessage);
  43.    }
  44. }
  45.  
  46. public class AsciiFormatException extends AsciiInputException {
  47.       // Illegal data: an illegal number or an illegal boolean value
  48.    AsciiFormatException(String errorMessage) {
  49.       super(errorMessage);
  50.    }
  51. }
  52.  
  53. public class AsciiEOFException extends AsciiInputException {
  54.      // attempt to read past end of stream
  55.    AsciiEOFException(String errorMessage) {
  56.       super(errorMessage);
  57.    }
  58. }
  59.  
  60.  
  61. public class AsciiInputStream extends FilterInputStream {
  62.  
  63.    // ***************************** Constructor ********************************
  64.    
  65.    public AsciiInputStream(InputStream s) {  // creates an AsciiInputStream for
  66.       super(s);                              //  reading from the stream s.
  67.    }
  68.    
  69.    // ***************************** Error Checking *****************************
  70.  
  71.    public void IOCheck(boolean throwExceptions) {  // call IOCheck(false) to turn
  72.       throwExceptionOnError = throwExceptions;     //   off exceptions, then check
  73.    }                                               //   for errors by calling checkError()
  74.    
  75.    public boolean error() {   // returns true if the most recent input operation on
  76.       return errorFlag;       // this AsciiInputStream produced an error.  An error
  77.    }                          // message can be retrieved by calling getErrorMessage()
  78.    
  79.    public String getErrorMessage() {            // if the most recent operation on
  80.       return errorFlag ? errorMessage : null;   // this stream produced an error, this
  81.    }                                            // gets an error message for that error
  82.  
  83.    // *************************** Input Methods ********************************
  84.    
  85.       // peek()  -- returns the next character about to be read from the stream,
  86.       //     without removing it from the stream.  '\n' is returned if the next
  87.       //     thing in the stream is end-of-line.  '\0' is returned if the next
  88.       //     thing is end-of-file.  (But this has to be changed to use some non-ASCII
  89.       //     character to indicate end-of-file.)
  90.       
  91.       // getAnyChar() -- reads and returns the next character in the stream, even if it
  92.       //     is a whitespace character.  It is an error to read past end-of-file.
  93.       //     (Note that getChar() skips over whitespace characters before reading.)
  94.       
  95.       // getChar(), getByte(), etc. -- skip over whitespace characters, then try to read a
  96.       //     value of the specified type.  An error can occur if the right type of data
  97.       //     is not found.  A "word" is any sequence of non-whitespace characters.
  98.       //     A "boolean" is one of the words (ignoring case) "true", "false", "yes", "no"
  99.       //     "t", "f", "y", "n", "0", "1".  An "Alpha" is a sequence of letters.  getAlpha()
  100.       //     is a special case in that it will skip over all non-letters before reading,
  101.       //     rather than just skipping whitespace characters.
  102.       
  103.       // getln() -- reads all characters up to the next end-of-line and returns them
  104.       //     as a String.  The end-of-line is then read and discarded.
  105.       
  106.       // getlnChar(), getlnByte() etc. -- Work the same as getChar(), etc., except that
  107.       //     after the value is read, any other characters on the same line, including the
  108.       //     end-of-line, are read and discarded.
  109.       
  110.       // eoln() -- this first reads and discards any spaces and tabs.  Then tests whether
  111.       //     the next thing in the stream is an end-of-line.  The end-of-file also counts
  112.       //     as an end of line.  If you want to test for eoln without discarding spaces
  113.       //     and tabs, check whether peek() == '\n' or '\0'.
  114.    
  115.       // eof() -- this first reads and discards any spaces, ends-of-line and tabs.  Then tests
  116.       //     whether the next thing in the stream is the end-of-file.  If you want to test for
  117.       //     eof without discarding spaces, ends-of-line, and tabs, check whether peek() == '\0'.
  118.       
  119.       // skipWhiteSpace() -- reads and discards any whitespace characters (spaces, tabs, and
  120.       //     end-of-lines).  Stops when the next character is a non-whitespace character or is eof.
  121.       
  122.       // skipNonLetters() -- reads and discards any non-letters, stopping when the next character
  123.       //     is a letter or the end-of-file.
  124.    
  125.    public char peek()          { errorFlag = false; return lookChar(); }
  126.    
  127.    public char getAnyChar()    { errorFlag = false; return readChar(); }
  128.  
  129.    public char getChar()       { errorFlag = false; skipWhiteSpace(); return readChar(); }
  130.    public byte getByte()       { errorFlag = false; return (byte)readInteger(-128L,127L); }
  131.    public short getShort()     { errorFlag = false; return (short)readInteger(-32768L,32767L); }   
  132.    public int getInt()         { errorFlag = false; return (int)readInteger((long)Integer.MIN_VALUE, (long)Integer.MAX_VALUE); }
  133.    public long getLong()       { errorFlag = false; return readInteger(Long.MIN_VALUE, Long.MAX_VALUE); }
  134.    public float getFloat()     { errorFlag = false; return readFloat(); }
  135.    public double getDouble()   { errorFlag = false; return readDouble(); }
  136.    public boolean getBoolean() { errorFlag = false; return readBoolean(); }
  137.    public String getWord()     { errorFlag = false; return readWord(); }
  138.    public String getAlpha()    { errorFlag = false; return readAlpha(); }
  139.    
  140.    public String getln()         { errorFlag = false; return readLine(); }
  141.  
  142.    public char getlnChar()       { char x=getChar();       dropLine();  return x; }
  143.    public byte getlnByte()       { byte x=getByte();       dropLine();  return x; }
  144.    public short getlnShort()     { short x=getShort();     dropLine();  return x; }
  145.    public int getlnInt()         { int x=getInt();         dropLine();  return x; }
  146.    public long getlnLong()       { long x=getLong();       dropLine();  return x; }
  147.    public float getlnFloat()     { float x=getFloat();     dropLine();  return x; }
  148.    public double getlnDouble()   { double x=getDouble();   dropLine();  return x; }
  149.    public boolean getlnBoolean() { boolean x=getBoolean(); dropLine();  return x; }
  150.    public String getlnWord()     { String x=getWord();     dropLine();  return x