Index: luni/src/main/java/java/io/DataInputStream.java =================================================================== --- luni/src/main/java/java/io/DataInputStream.java (revision 532715) +++ luni/src/main/java/java/io/DataInputStream.java (working copy) @@ -41,8 +41,12 @@ * @see DataOutputStream * @see RandomAccessFile */ + + byte[] buff; + public DataInputStream(InputStream in) { super(in); + buff = new byte[8]; } /** @@ -136,13 +140,23 @@ * * @see DataOutput#writeChar(int) */ + private int readToBuff(int count) throws IOException { + int offset = 0; + + while(offset < count) { + int bytesRead = in.read(buff, offset, count - offset); + if(bytesRead == -1) return bytesRead; + offset += bytesRead; + } + return offset; + } + public final char readChar() throws IOException { - int b1 = in.read(); - int b2 = in.read(); - if ((b1 | b2) < 0) { + if (readToBuff(2) < 0){ throw new EOFException(); } - return (char) ((b1 << 8) + b2); + return (char) (((buff[0] & 0xff) << 8) | (buff[1] & 0xff)); + } /** @@ -248,14 +262,11 @@ * @see DataOutput#writeInt(int) */ public final int readInt() throws IOException { - int b1 = in.read(); - int b2 = in.read(); - int b3 = in.read(); - int b4 = in.read(); - if ((b1 | b2 | b3 | b4) < 0) { + if (readToBuff(4) < 0){ throw new EOFException(); } - return ((b1 << 24) + (b2 << 16) + (b3 << 8) + b4); + return ((buff[0] & 0xff) << 24) | ((buff[1] & 0xff) << 16) | + ((buff[2] & 0xff) << 8) | (buff[3] & 0xff); } /** @@ -320,16 +331,15 @@ * @see DataOutput#writeLong(long) */ public final long readLong() throws IOException { - int i1 = readInt(); - int b1 = in.read(); - int b2 = in.read(); - int b3 = in.read(); - int b4 = in.read(); - if ((b1 | b2 | b3 | b4) < 0) { + if (readToBuff(8) < 0){ throw new EOFException(); } - return (((long) i1) << 32) + ((long) b1 << 24) + (b2 << 16) + (b3 << 8) - + b4; + int i1 = ((buff[0] & 0xff) << 24) | ((buff[1] & 0xff) << 16) | + ((buff[2] & 0xff) << 8) | (buff[3] & 0xff); + int i2 = ((buff[4] & 0xff) << 24) | ((buff[5] & 0xff) << 16) | + ((buff[6] & 0xff) << 8) | (buff[7] & 0xff); + + return ((i1 & 0xffffffffL) << 32) | (i2 & 0xffffffffL); } /** @@ -343,12 +353,10 @@ * @see DataOutput#writeShort(int) */ public final short readShort() throws IOException { - int b1 = in.read(); - int b2 = in.read(); - if ((b1 | b2) < 0) { + if (readToBuff(2) < 0){ throw new EOFException(); } - return (short) ((b1 << 8) + b2); + return (short) (((buff[0] & 0xff) << 8) | (buff[1] & 0xff)); } /** @@ -383,12 +391,10 @@ * @see DataOutput#writeShort(int) */ public final int readUnsignedShort() throws IOException { - int b1 = in.read(); - int b2 = in.read(); - if ((b1 | b2) < 0) { + if (readToBuff(2) < 0){ throw new EOFException(); } - return ((b1 << 8) + b2); + return (char) (((buff[0] & 0xff) << 8) | (buff[1] & 0xff)); } /** @@ -402,72 +408,17 @@ * @see DataOutput#writeUTF(java.lang.String) */ public final String readUTF() throws IOException { - int utfSize = readUnsignedShort(); - return decodeUTF(utfSize); + return decodeUTF(readUnsignedShort()); } - static final int MAX_BUF_SIZE = 8192; - private static class CacheLock { - } - - static final Object cacheLock = new CacheLock(); - - static boolean useShared = true; - - static byte[] byteBuf = new byte[0]; - - static char[] charBuf = new char[0]; - String decodeUTF(int utfSize) throws IOException { - byte[] buf; - char[] out = null; - boolean makeBuf = true; - /* - * Try to avoid the synchronization -- if we get a stale value for - * useShared then there is no foul below, but those that sync on the - * lock must see the right value. - */ - if (utfSize <= MAX_BUF_SIZE && useShared) { - synchronized (cacheLock) { - if (useShared) { - useShared = false; - makeBuf = false; - } - } - } - if (makeBuf) { - buf = new byte[utfSize]; - out = new char[utfSize]; - } else { - /* - * Need to 'sample' byteBuf and charBuf before using them because - * they are not protected by the cacheLock. They may get out of sync - * with the static and one another, but that is ok because we - * explicitly check and fix their length after sampling. - */ - buf = byteBuf; - if (buf.length < utfSize) { - buf = byteBuf = new byte[utfSize]; - } - out = charBuf; - if (out.length < utfSize) { - out = charBuf = new char[utfSize]; - } - } - + byte[] buf = new byte[utfSize]; + char[] out = new char[utfSize]; readFully(buf, 0, utfSize); - String result; - result = Util.convertUTF8WithBuf(buf, out, 0, utfSize); - if (!makeBuf) { - /* - * Do not synchronize useShared on cacheLock, it will make it back - * to main storage at some point, and no harm until it does. - */ - useShared = true; - } - return result; + + return Util.convertUTF8WithBuf(buf, out, 0, utfSize); } /** Index: luni/src/main/java/java/io/DataOutputStream.java =================================================================== --- luni/src/main/java/java/io/DataOutputStream.java (revision 532715) +++ luni/src/main/java/java/io/DataOutputStream.java (working copy) @@ -31,6 +31,7 @@ /** The number of bytes written out so far */ protected int written; + byte buff[]; /** * Constructs a new DataOutputStream on the OutputStream out. @@ -43,6 +44,7 @@ */ public DataOutputStream(OutputStream out) { super(out); + buff = new byte[8]; } /** @@ -190,9 +192,9 @@ * @see DataInput#readChar() */ public final void writeChar(int val) throws IOException { - out.write(val >> 8); - out.write(val); - written += 2; + buff[0] = (byte) (val >> 8); + buff[1] = (byte) val; + out.write(buff, 0, 2); } /** @@ -269,11 +271,11 @@ * @see DataInput#readInt() */ public final void writeInt(int val) throws IOException { - out.write(val >> 24); - out.write(val >> 16); - out.write(val >> 8); - out.write(val); - written += 4; + buff[0] = (byte) (val >> 24); + buff[1] = (byte) (val >> 16); + buff[2] = (byte) (val >> 8); + buff[3] = (byte) val; + out.write(buff, 0, 4); } /** @@ -290,8 +292,15 @@ * @see DataInput#readLong() */ public final void writeLong(long val) throws IOException { - writeInt((int) (val >> 32)); - writeInt((int) val); + buff[0] = (byte) (val >> 56); + buff[1] = (byte) (val >> 48); + buff[2] = (byte) (val >> 40); + buff[3] = (byte) (val >> 32); + buff[4] = (byte) (val >> 24); + buff[5] = (byte) (val >> 16); + buff[6] = (byte) (val >> 8); + buff[7] = (byte) val; + out.write(buff, 0, 8); } /** @@ -309,7 +318,9 @@ * @see DataInput#readUnsignedShort() */ public final void writeShort(int val) throws IOException { - writeChar(val); + buff[0] = (byte) (val >> 8); + buff[1] = (byte) val; + out.write(buff, 0, 2); } /** @@ -325,53 +336,12 @@ * @see DataInput#readUTF() */ public final void writeUTF(String str) throws IOException { - int length = str.length(); - if (length <= DataInputStream.MAX_BUF_SIZE / 3) { - int size = length * 3; - byte[] utfBytes; - boolean makeBuf = true; - synchronized (DataInputStream.byteBuf) { - if (DataInputStream.useShared) { - DataInputStream.useShared = false; - makeBuf = false; - } - } - if (makeBuf) { - utfBytes = new byte[size]; - } else { - if (DataInputStream.byteBuf.length < size) { - DataInputStream.byteBuf = new byte[size]; - } - utfBytes = DataInputStream.byteBuf; - } - int utfIndex = 0; - for (int i = 0; i < length; i++) { - int charValue = str.charAt(i); - if (charValue > 0 && charValue <= 127) { - utfBytes[utfIndex++] = (byte) charValue; - } else if (charValue <= 2047) { - utfBytes[utfIndex++] = (byte) (0xc0 | (0x1f & (charValue >> 6))); - utfBytes[utfIndex++] = (byte) (0x80 | (0x3f & charValue)); - } else { - utfBytes[utfIndex++] = (byte) (0xe0 | (0x0f & (charValue >> 12))); - utfBytes[utfIndex++] = (byte) (0x80 | (0x3f & (charValue >> 6))); - utfBytes[utfIndex++] = (byte) (0x80 | (0x3f & charValue)); - } - } - writeShort(utfIndex); - write(utfBytes, 0, utfIndex); - if (!makeBuf) { - DataInputStream.useShared = true; - } - } else { - long utfCount; - if (length <= 65535 && (utfCount = countUTFBytes(str)) <= 65535) { - writeShort((int) utfCount); - writeUTFBytes(str, utfCount); - } else { - throw new UTFDataFormatException(Msg.getString("K0068")); //$NON-NLS-1$ - } + long utfCount = countUTFBytes(str); + if (utfCount > 65535) { + throw new UTFDataFormatException(Msg.getString("K0068")); //$NON-NLS-1$ } + writeShort((int) utfCount); + writeUTFBytes(str, utfCount); } long countUTFBytes(String str) { @@ -390,70 +360,23 @@ } void writeUTFBytes(String str, long count) throws IOException { - boolean single = true; int size = (int) count; - if (count > DataInputStream.MAX_BUF_SIZE) { - single = false; - size = DataInputStream.MAX_BUF_SIZE; + int length = str.length(); + byte[] utfBytes = new byte[size]; + int utfIndex = 0; + for (int i = 0; i < length; i++) { + int charValue = str.charAt(i); + if (charValue > 0 && charValue <= 127) { + utfBytes[utfIndex++] = (byte) charValue; + } else if (charValue <= 2047) { + utfBytes[utfIndex++] = (byte) (0xc0 | (0x1f & (charValue >> 6))); + utfBytes[utfIndex++] = (byte) (0x80 | (0x3f & charValue)); + } else { + utfBytes[utfIndex++] = (byte) (0xe0 | (0x0f & (charValue >> 12))); + utfBytes[utfIndex++] = (byte) (0x80 | (0x3f & (charValue >> 6))); + utfBytes[utfIndex++] = (byte) (0x80 | (0x3f & charValue)); + } } - byte[] utfBytes; - boolean makeBuf = true; - if (DataInputStream.useShared) { - synchronized (DataInputStream.cacheLock) { - if (DataInputStream.useShared) { - DataInputStream.useShared = false; - makeBuf = false; - } - } - } - if (makeBuf) { - utfBytes = new byte[size]; - } else { - // byteBuf is not protected by the cacheLock, so sample it first - utfBytes = DataInputStream.byteBuf; - if (utfBytes.length < size) { - utfBytes = DataInputStream.byteBuf = new byte[size]; - } - } - - int utfIndex = 0, i = 0, length = str.length(); - int end = length; - while (i < length) { - if (!single) { - end = i + ((utfBytes.length - utfIndex) / 3); - if (end > length) { - end = length; - } - } - for (int j = i; j < end; j++) { - int charValue = str.charAt(j); - if (charValue > 0 && charValue <= 127) { - utfBytes[utfIndex++] = (byte) charValue; - } else if (charValue <= 2047) { - utfBytes[utfIndex++] = (byte) (0xc0 | (0x1f & (charValue >> 6))); - utfBytes[utfIndex++] = (byte) (0x80 | (0x3f & charValue)); - } else { - utfBytes[utfIndex++] = (byte) (0xe0 | (0x0f & (charValue >> 12))); - utfBytes[utfIndex++] = (byte) (0x80 | (0x3f & (charValue >> 6))); - utfBytes[utfIndex++] = (byte) (0x80 | (0x3f & charValue)); - } - } - if (single || utfIndex > utfBytes.length - 300) { - write(utfBytes, 0, utfIndex); - if (single) { - return; - } - utfIndex = 0; - } - i = end; - } - if (utfIndex > 0) { - write(utfBytes, 0, utfIndex); - } - if (!makeBuf) { - // Update the useShared flag optimistically (see DataInputStream - // equivalent) - DataInputStream.useShared = true; - } + write(utfBytes, 0, utfIndex); } }