home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 January / PCO0198.ISO / 1&1 / java.z / java_301 / java / io / DataInputStream.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-10-20  |  4.4 KB  |  232 lines

  1. package java.io;
  2.  
  3. public class DataInputStream extends FilterInputStream implements DataInput {
  4.    private char[] lineBuffer;
  5.  
  6.    public DataInputStream(InputStream in) {
  7.       super(in);
  8.    }
  9.  
  10.    public final int read(byte[] b) throws IOException {
  11.       return super.in.read(b, 0, b.length);
  12.    }
  13.  
  14.    public final int read(byte[] b, int off, int len) throws IOException {
  15.       return super.in.read(b, off, len);
  16.    }
  17.  
  18.    public final void readFully(byte[] b) throws IOException {
  19.       this.readFully(b, 0, b.length);
  20.    }
  21.  
  22.    public final void readFully(byte[] b, int off, int len) throws IOException {
  23.       InputStream in = super.in;
  24.  
  25.       int count;
  26.       for(int n = 0; n < len; n += count) {
  27.          count = in.read(b, off + n, len - n);
  28.          if (count < 0) {
  29.             throw new EOFException();
  30.          }
  31.       }
  32.  
  33.    }
  34.  
  35.    public final int skipBytes(int n) throws IOException {
  36.       InputStream in = super.in;
  37.  
  38.       for(int i = 0; i < n; i += (int)in.skip((long)(n - i))) {
  39.       }
  40.  
  41.       return n;
  42.    }
  43.  
  44.    public final boolean readBoolean() throws IOException {
  45.       int ch = super.in.read();
  46.       if (ch < 0) {
  47.          throw new EOFException();
  48.       } else {
  49.          return ch != 0;
  50.       }
  51.    }
  52.  
  53.    public final byte readByte() throws IOException {
  54.       int ch = super.in.read();
  55.       if (ch < 0) {
  56.          throw new EOFException();
  57.       } else {
  58.          return (byte)ch;
  59.       }
  60.    }
  61.  
  62.    public final int readUnsignedByte() throws IOException {
  63.       int ch = super.in.read();
  64.       if (ch < 0) {
  65.          throw new EOFException();
  66.       } else {
  67.          return ch;
  68.       }
  69.    }
  70.  
  71.    public final short readShort() throws IOException {
  72.       InputStream in = super.in;
  73.       int ch1 = in.read();
  74.       int ch2 = in.read();
  75.       if ((ch1 | ch2) < 0) {
  76.          throw new EOFException();
  77.       } else {
  78.          return (short)((ch1 << 8) + ch2);
  79.       }
  80.    }
  81.  
  82.    public final int readUnsignedShort() throws IOException {
  83.       InputStream in = super.in;
  84.       int ch1 = in.read();
  85.       int ch2 = in.read();
  86.       if ((ch1 | ch2) < 0) {
  87.          throw new EOFException();
  88.       } else {
  89.          return (ch1 << 8) + ch2;
  90.       }
  91.    }
  92.  
  93.    public final char readChar() throws IOException {
  94.       InputStream in = super.in;
  95.       int ch1 = in.read();
  96.       int ch2 = in.read();
  97.       if ((ch1 | ch2) < 0) {
  98.          throw new EOFException();
  99.       } else {
  100.          return (char)((ch1 << 8) + ch2);
  101.       }
  102.    }
  103.  
  104.    public final int readInt() throws IOException {
  105.       InputStream in = super.in;
  106.       int ch1 = in.read();
  107.       int ch2 = in.read();
  108.       int ch3 = in.read();
  109.       int ch4 = in.read();
  110.       if ((ch1 | ch2 | ch3 | ch4) < 0) {
  111.          throw new EOFException();
  112.       } else {
  113.          return (ch1 << 24) + (ch2 << 16) + (ch3 << 8) + ch4;
  114.       }
  115.    }
  116.  
  117.    public final long readLong() throws IOException {
  118.       return ((long)this.readInt() << 32) + ((long)this.readInt() & 4294967295L);
  119.    }
  120.  
  121.    public final float readFloat() throws IOException {
  122.       return Float.intBitsToFloat(this.readInt());
  123.    }
  124.  
  125.    public final double readDouble() throws IOException {
  126.       return Double.longBitsToDouble(this.readLong());
  127.    }
  128.  
  129.    public final String readLine() throws IOException {
  130.       InputStream in = super.in;
  131.       char[] buf = this.lineBuffer;
  132.       if (buf == null) {
  133.          buf = this.lineBuffer = new char[128];
  134.       }
  135.  
  136.       int room = buf.length;
  137.       int offset = 0;
  138.  
  139.       while(true) {
  140.          int c;
  141.          switch (c = in.read()) {
  142.             case 13:
  143.                int c2 = in.read();
  144.                if (c2 != 10) {
  145.                   if (!(in instanceof PushbackInputStream)) {
  146.                      in = super.in = new PushbackInputStream(in);
  147.                   }
  148.  
  149.                   ((PushbackInputStream)in).unread(c2);
  150.                }
  151.             case -1:
  152.             case 10:
  153.                if (c == -1 && offset == 0) {
  154.                   return null;
  155.                }
  156.  
  157.                return String.copyValueOf(buf, 0, offset);
  158.          }
  159.  
  160.          --room;
  161.          if (room < 0) {
  162.             buf = new char[offset + 128];
  163.             room = buf.length - offset - 1;
  164.             System.arraycopy(this.lineBuffer, 0, buf, 0, offset);
  165.             this.lineBuffer = buf;
  166.          }
  167.  
  168.          buf[offset++] = (char)c;
  169.       }
  170.    }
  171.  
  172.    public final String readUTF() throws IOException {
  173.       return readUTF(this);
  174.    }
  175.  
  176.    public static final String readUTF(DataInput in) throws IOException {
  177.       int utflen = in.readUnsignedShort();
  178.       char[] str = new char[utflen];
  179.       int count = 0;
  180.       int strlen = 0;
  181.  
  182.       while(count < utflen) {
  183.          int c = in.readUnsignedByte();
  184.          switch (c >> 4) {
  185.             case 14:
  186.                count += 3;
  187.                if (count > utflen) {
  188.                   throw new UTFDataFormatException();
  189.                }
  190.  
  191.                int char2 = in.readUnsignedByte();
  192.                int char3 = in.readUnsignedByte();
  193.                if ((char2 & 192) == 128 && (char3 & 192) == 128) {
  194.                   str[strlen++] = (char)((c & 15) << 12 | (char2 & 63) << 6 | char3 & 63);
  195.                   break;
  196.                }
  197.  
  198.                throw new UTFDataFormatException();
  199.             case 13:
  200.             case 12:
  201.                count += 2;
  202.                if (count > utflen) {
  203.                   throw new UTFDataFormatException();
  204.                }
  205.  
  206.                int char2 = in.readUnsignedByte();
  207.                if ((char2 & 192) != 128) {
  208.                   throw new UTFDataFormatException();
  209.                }
  210.  
  211.                str[strlen++] = (char)((c & 31) << 6 | char2 & 63);
  212.                break;
  213.             case 7:
  214.             case 6:
  215.             case 5:
  216.             case 4:
  217.             case 3:
  218.             case 2:
  219.             case 1:
  220.             case 0:
  221.                ++count;
  222.                str[strlen++] = (char)c;
  223.                break;
  224.             default:
  225.                throw new UTFDataFormatException();
  226.          }
  227.       }
  228.  
  229.       return new String(str, 0, strlen);
  230.    }
  231. }
  232.