⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 textinputstream.java

📁 JAVA网络编程技术内幕一书的源代码
💻 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 TextInputStream extends FilterInputStream {
  public TextInputStream (InputStream in) {
    super (in);
  }

  protected int pushback = -2;

  public synchronized int read () throws IOException {
    int datum = pushback;
    if (datum == -2)
      datum = super.read();
    else
      pushback = -2;
    return datum;
  }

  public synchronized int read (byte[] data, int offset, int length)
    throws IOException {
    if (length <= 0) {
      return 0;
    } else if (pushback == -2) {
      return super.read (data, offset, length);
    } else if (pushback == -1) {
      pushback = -2;
      return -1;
    } else {
      data[offset] = (byte) pushback;
      pushback = -2;
      return 1;
    }
  }

  public synchronized long skip (long amount) throws IOException {
    if (amount <= 0) {
      return 0;
    } else if (pushback == -2) {
      return super.skip (amount);
    } else if (pushback == -1) {
      pushback = -2;
      return 0;
    } else {
      pushback = -2;
      return 1;
    }
  }

  public synchronized int available () throws IOException {
    return (pushback == -2) ? super.available () : (pushback >= 0) ? 1 : 0;
  }

  protected synchronized void unread (int chr) throws IOException {
    if (pushback != -2)
      throw new IOException ("Pushback overflow");
    pushback = chr;
  }

  public void skipWhitespace () throws IOException {
    int chr;
    do {
      chr = read ();
    } while ((chr != -1) && Character.isWhitespace ((char) chr));
    unread (chr);
  }

  public int readInt () throws IOException, NumberFormatException {
    StringBuffer result = new StringBuffer ();
    skipWhitespace ();
    int chr = read ();
    if ((chr == '-') || Character.isDigit ((char) chr)) {
      result.append ((char) chr);
      while (((chr = read ()) != -1) && Character.isDigit ((char) chr))
        result.append ((char) chr);
    }
    unread (chr);
    String value = result.toString ();
    if ((value.equals ("") || value.equals ("-")) && (chr == -1))
      throw new EOFException ("EOF reading int");
    return Integer.parseInt (value);
  }

  public String readWord () throws IOException {
    StringBuffer result = new StringBuffer ();
    skipWhitespace ();
    int chr;
    while (((chr = read ()) != -1) && !Character.isWhitespace ((char) chr))
      result.append ((char) chr);
    unread (chr);
    if (result.length () == 0)
      throw new EOFException ("EOF reading word");
    return result.toString ();
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -