diff options
author | Mark Wielaard <mark@klomp.org> | 2001-02-17 15:09:46 +0000 |
---|---|---|
committer | Mark Wielaard <mark@gcc.gnu.org> | 2001-02-17 15:09:46 +0000 |
commit | 3f57b973f3b6d4b21912d37a263430610e84ce47 (patch) | |
tree | 77f74bc7036fe475d2b489e8cf040d0870be772c /libjava/java/io/DataInputStream.java | |
parent | a7e96ed41a2e1a7878d41e74579ab1b76fa4fbe3 (diff) | |
download | gcc-3f57b973f3b6d4b21912d37a263430610e84ce47.zip gcc-3f57b973f3b6d4b21912d37a263430610e84ce47.tar.gz gcc-3f57b973f3b6d4b21912d37a263430610e84ce47.tar.bz2 |
Remerge with Classpath (changes by Bryce McKinlay <bryce@albatross.co.nz>)
Remerge with Classpath
(changes by Bryce McKinlay <bryce@albatross.co.nz>)
* java/io/DataInputStream.java (readBoolean): Use convertToBoolean().
(readByte): Use convertToByte().
(readChar): Use convertToChar().
(readInt): Use convertToInt().
(readLong): Use convertToLong().
(readShort): Use convertToShort().
(readUnsignedByte): Use convertToUnsignedByte().
(readUnsignedShort): Use convertToUnsignedShort().
(readUTF): Use convertToUTF().
(convertToBoolean): Resurrected.
(convertToByte): Ditto.
(convertToChar): Ditto.
(convertToInt): Ditto.
(convertToLong): Ditto.
(convertToShort): Ditto.
(convertToUnsignedByte): Ditto.
(convertToUnsignedShort): Ditto.
(convertToUTF): Ditto.
From-SVN: r39800
Diffstat (limited to 'libjava/java/io/DataInputStream.java')
-rw-r--r-- | libjava/java/io/DataInputStream.java | 191 |
1 files changed, 111 insertions, 80 deletions
diff --git a/libjava/java/io/DataInputStream.java b/libjava/java/io/DataInputStream.java index f4de0a8..1607967 100644 --- a/libjava/java/io/DataInputStream.java +++ b/libjava/java/io/DataInputStream.java @@ -51,6 +51,9 @@ public class DataInputStream extends FilterInputStream implements DataInput // handled correctly. If set, readLine() will ignore the first char it sees // if that char is a '\n' boolean ignoreInitialNewline = false; + + // Byte buffer, used to make primitive read calls more efficient. + byte[] buf = new byte[8]; /** * This constructor initializes a new <code>DataInputStream</code> @@ -120,10 +123,7 @@ public class DataInputStream extends FilterInputStream implements DataInput */ public final boolean readBoolean() throws IOException { - int b = in.read(); - if (b < 0) - throw new EOFException(); - return (b != 0); + return convertToBoolean(in.read()); } /** @@ -143,11 +143,7 @@ public class DataInputStream extends FilterInputStream implements DataInput */ public final byte readByte() throws IOException { - int i = in.read(); - if (i < 0) - throw new EOFException(); - - return (byte) i; + return convertToByte(in.read()); } /** @@ -177,11 +173,10 @@ public class DataInputStream extends FilterInputStream implements DataInput */ public final char readChar() throws IOException { - int a = in.read(); - int b = in.read(); - if (b < 0) + int count = in.read (buf, 0, 2); + if (count < 2) throw new EOFException(); - return (char) ((a << 8) | (b & 0xff)); + return convertToChar(buf); } /** @@ -308,15 +303,10 @@ public class DataInputStream extends FilterInputStream implements DataInput */ public final int readInt() throws IOException { - int a = in.read(); - int b = in.read(); - int c = in.read(); - int d = in.read(); - if (d < 0) + int count = in.read (buf, 0, 4); + if (count < 4) throw new EOFException(); - - return (((a & 0xff) << 24) | ((b & 0xff) << 16) | - ((c & 0xff) << 8) | (d & 0xff)); + return convertToInt(buf); } /** @@ -463,25 +453,10 @@ public class DataInputStream extends FilterInputStream implements DataInput */ public final long readLong() throws IOException { - int a = in.read(); - int b = in.read(); - int c = in.read(); - int d = in.read(); - int e = in.read(); - int f = in.read(); - int g = in.read(); - int h = in.read(); - if (h < 0) + int count = in.read(buf, 0, 8); + if (count < 8) throw new EOFException(); - - return (((long)(a & 0xff) << 56) | - ((long)(b & 0xff) << 48) | - ((long)(c & 0xff) << 40) | - ((long)(d & 0xff) << 32) | - ((long)(e & 0xff) << 24) | - ((long)(f & 0xff) << 16) | - ((long)(g & 0xff) << 8) | - ((long)(h & 0xff))); + return convertToLong(buf); } /** @@ -513,11 +488,10 @@ public class DataInputStream extends FilterInputStream implements DataInput */ public final short readShort() throws IOException { - int a = in.read(); - int b = in.read(); - if (b < 0) + int count = in.read(buf, 0, 2); + if (count < 2) throw new EOFException(); - return (short) ((a << 8) | (b & 0xff)); + return convertToShort(buf); } /** @@ -538,11 +512,7 @@ public class DataInputStream extends FilterInputStream implements DataInput */ public final int readUnsignedByte() throws IOException { - int i = in.read(); - if (i < 0) - throw new EOFException(); - - return (i & 0xFF); + return convertToUnsignedByte(in.read()); } /** @@ -572,11 +542,10 @@ public class DataInputStream extends FilterInputStream implements DataInput */ public final int readUnsignedShort() throws IOException { - int a = in.read(); - int b = in.read(); - if (b < 0) + int count = in.read(buf, 0, 2); + if (count < 2) throw new EOFException(); - return (((a & 0xff) << 8) | (b & 0xff)); + return convertToUnsignedShort(buf); } /** @@ -668,41 +637,14 @@ public class DataInputStream extends FilterInputStream implements DataInput { final int UTFlen = in.readUnsignedShort(); byte[] buf = new byte[UTFlen]; - StringBuffer strbuf = new StringBuffer(); // This blocks until the entire string is available rather than // doing partial processing on the bytes that are available and then // blocking. An advantage of the latter is that Exceptions // could be thrown earlier. The former is a bit cleaner. in.readFully(buf, 0, UTFlen); - for (int i = 0; i < UTFlen; ) - { - if ((buf[i] & 0x80) == 0) // bit pattern 0xxxxxxx - strbuf.append((char) (buf[i++] & 0xFF)); - else if ((buf[i] & 0xE0) == 0xC0) // bit pattern 110xxxxx - { - if (i + 1 >= UTFlen || (buf[i+1] & 0xC0) != 0x80) - throw new UTFDataFormatException(); - strbuf.append((char) (((buf[i++] & 0x1F) << 6) | - (buf[i++] & 0x3F))); - } - else if ((buf[i] & 0xF0) == 0xE0) // bit pattern 1110xxxx - { - if (i + 2 >= UTFlen || - (buf[i+1] & 0xC0) != 0x80 || (buf[i+2] & 0xC0) != 0x80) - throw new UTFDataFormatException(); - - strbuf.append((char) (((buf[i++] & 0x0F) << 12) | - ((buf[i++] & 0x3F) << 6) | - (buf[i++] & 0x3F))); - } - else // must be ((buf[i] & 0xF0) == 0xF0 || (buf[i] & 0xC0) == 0x80) - throw new UTFDataFormatException(); // bit patterns 1111xxxx or - // 10xxxxxx - } - - return strbuf.toString(); + return convertFromUTF(buf); } /** @@ -733,4 +675,93 @@ public class DataInputStream extends FilterInputStream implements DataInput } return n; } + + static boolean convertToBoolean(int b) throws EOFException + { + if (b < 0) + throw new EOFException(); + return (b != 0); + } + + static byte convertToByte(int i) throws EOFException + { + if (i < 0) + throw new EOFException(); + return (byte) i; + } + + static int convertToUnsignedByte(int i) throws EOFException + { + if (i < 0) + throw new EOFException(); + return (i & 0xFF); + } + + static char convertToChar(byte[] buf) + { + return (char) ((buf[0] << 8) | (buf[1] & 0xff)); + } + + static short convertToShort(byte[] buf) + { + return (short) ((buf[0] << 8) | (buf[1] & 0xff)); + } + + static int convertToUnsignedShort(byte[] buf) + { + return (((buf[0] & 0xff) << 8) | (buf[1] & 0xff)); + } + + static int convertToInt(byte[] buf) + { + return (((buf[0] & 0xff) << 24) | ((buf[1] & 0xff) << 16) | + ((buf[2] & 0xff) << 8) | (buf[3] & 0xff)); + } + + static long convertToLong(byte[] buf) + { + return (((long)(buf[0] & 0xff) << 56) | + ((long)(buf[1] & 0xff) << 48) | + ((long)(buf[2] & 0xff) << 40) | + ((long)(buf[3] & 0xff) << 32) | + ((long)(buf[4] & 0xff) << 24) | + ((long)(buf[5] & 0xff) << 16) | + ((long)(buf[6] & 0xff) << 8) | + ((long)(buf[7] & 0xff))); + } + + static String convertFromUTF(byte[] buf) + throws EOFException, UTFDataFormatException + { + StringBuffer strbuf = new StringBuffer(); + + for (int i = 0; i < buf.length; ) + { + if ((buf[i] & 0x80) == 0) // bit pattern 0xxxxxxx + strbuf.append((char) (buf[i++] & 0xFF)); + else if ((buf[i] & 0xE0) == 0xC0) // bit pattern 110xxxxx + { + if (i + 1 >= buf.length || (buf[i+1] & 0xC0) != 0x80) + throw new UTFDataFormatException(); + + strbuf.append((char) (((buf[i++] & 0x1F) << 6) | + (buf[i++] & 0x3F))); + } + else if ((buf[i] & 0xF0) == 0xE0) // bit pattern 1110xxxx + { + if (i + 2 >= buf.length || + (buf[i+1] & 0xC0) != 0x80 || (buf[i+2] & 0xC0) != 0x80) + throw new UTFDataFormatException(); + + strbuf.append((char) (((buf[i++] & 0x0F) << 12) | + ((buf[i++] & 0x3F) << 6) | + (buf[i++] & 0x3F))); + } + else // must be ((buf[i] & 0xF0) == 0xF0 || (buf[i] & 0xC0) == 0x80) + throw new UTFDataFormatException(); // bit patterns 1111xxxx or + // 10xxxxxx + } + + return strbuf.toString(); + } } |