www.pudn.com > jnp-src.rar > DataReader.java


/*
 * Java Network Programming, Second Edition
 * Merlin Hughes, Michael Shoffner, Derek Hamner
 * Manning Publications Company; ISBN 188477749X
 *
 * http://nitric.com/jnp/
 *
 * Copyright (c) 1997-1999 Merlin Hughes, Michael Shoffner, Derek Hamner;
 * all rights reserved; see license.txt for details.
 */

import java.io.*; 
 
public class DataReader extends Reader { 
  protected PushbackReader pushbackReader; 
 
  public DataReader (Reader reader) { 
    super (reader); 
    pushbackReader = new PushbackReader (reader); 
  } 
 
  protected boolean eof = false; 
 
  public int read () throws IOException { 
    synchronized (lock) { 
      if (!eof) 
        return pushbackReader.read (); 
      else { 
        eof = false; 
        return -1; 
      } 
    } 
  } 
   
  public int read (char[] data, int offset, int length) throws IOException { 
    synchronized (lock) { 
      if (!eof) 
        return pushbackReader.read (data, offset, length); 
      else { 
        eof = false; 
        return -1; 
      } 
    } 
  } 
 
  public long skip (long amount) throws IOException { 
    synchronized (lock) { 
      if (amount <= 0) 
        return 0; 
      else if (!eof) 
        return pushbackReader.skip (amount); 
      else { 
        eof = false; 
        return 0; 
      } 
    } 
  } 
 
  public boolean ready () throws IOException { 
    synchronized (lock) { 
      return pushbackReader.ready (); 
    } 
  } 
 
  public void close () throws IOException { 
    synchronized (lock) { 
      pushbackReader.close (); 
      eof = false; 
    } 
  } 
 
  protected void unread (int chr) throws IOException { 
    synchronized (lock) { 
      if (eof) 
        throw new IOException ("Pushback buffer overflow"); 
      if (chr == -1) 
        eof = true; 
      else 
        pushbackReader.unread (chr); 
    } 
  } 
 
  public String readWord () throws IOException { 
    synchronized (lock) { 
      readExp (WHITESPACE, ZERO_OR_MORE); 
      String word = readExp (NON_WHITESPACE, ZERO_OR_MORE); 
      if (word.equals ("") && eof) 
        throw new EOFException ("EOF reading word"); 
      return word; 
    } 
  } 
 
  public long readLong () throws IOException, NumberFormatException { 
    synchronized (lock) { 
      readExp (WHITESPACE, ZERO_OR_MORE); 
      String value = readExp ('-', ZERO_OR_ONE) + 
        readExp (DIGITS, ZERO_OR_MORE); 
      if ((value.equals ("") || value.equals ("-")) && eof) 
        throw new EOFException ("EOF reading long"); 
      return Long.parseLong (value); 
    } 
  } 
 
  public double readDouble () throws IOException, NumberFormatException { 
    synchronized (lock) { 
      readExp (WHITESPACE, ZERO_OR_MORE); 
      String value = readExp ('-', ZERO_OR_ONE) + 
        readExp (DIGITS, ZERO_OR_MORE) + readExp ('.', ZERO_OR_ONE) + 
        readExp (DIGITS, ZERO_OR_MORE); 
      if (eof && (value.equals ("") || value.equals (".") || 
                  value.equals ("-") || value.equals ("-."))) 
        throw new EOFException ("EOF reading double"); 
      return Double.valueOf (value).doubleValue (); 
    } 
  } 
 
  protected String readExp (int type, int volume) throws IOException { 
    synchronized (lock) { 
      StringBuffer result = new StringBuffer (); 
      boolean done = false; 
      while (!done) { 
        int chr = read (); 
        if (isMatch (chr, type)) { 
          result.append ((char) chr); 
          done = (volume == ZERO_OR_ONE); 
        } else { 
          unread (chr); 
          done = true; 
        } 
      } 
      return result.toString (); 
    } 
  } 
 
  protected static final int WHITESPACE = 0x10000, 
    NON_WHITESPACE = 0x10001, DIGITS = 0x10002; 
  protected static final int ZERO_OR_MORE = 0, 
    ZERO_OR_ONE = 1; 
   
  protected boolean isMatch (int chr, int type) { 
    switch (type) { 
      case WHITESPACE: 
        return Character.isWhitespace ((char) chr); 
      case NON_WHITESPACE: 
        return !Character.isWhitespace ((char) chr); 
      case DIGITS: 
        return Character.isDigit ((char) chr); 
      default: 
        return (type == chr); 
    } 
  } 
}