Index: java/io/DataOutputStream.java =================================================================== RCS file: /cvsroot/classpath/classpath/java/io/DataOutputStream.java,v retrieving revision 1.17 diff -u -r1.17 DataOutputStream.java --- java/io/DataOutputStream.java 11 Oct 2004 09:45:20 -0000 1.17 +++ java/io/DataOutputStream.java 31 Dec 2004 15:03:17 -0000 @@ -1,5 +1,5 @@ /* DataOutputStream.java -- Writes primitive Java datatypes to streams - Copyright (C) 1998, 2001, 2003 Free Software Foundation, Inc. + Copyright (C) 1998, 2001, 2003, 2004 Free Software Foundation, Inc. This file is part of GNU Classpath. @@ -51,12 +51,17 @@ * * @see DataInputStream * - * @author Aaron M. Renn - * @author Tom Tromey + * @author Aaron M. Renn (address@hidden) + * @author Tom Tromey (address@hidden) */ public class DataOutputStream extends FilterOutputStream implements DataOutput { /** + * Size of buffer used to cache bytes to write in an operation. + */ + private static final int BUFFER_SIZE = 8192; + + /** * This is the total number of bytes that have been written to the * stream by this object instance. */ @@ -176,8 +181,8 @@ * value passed. These bytes will be written "big endian". That is, * with the high byte written first in the following manner: *

- * byte0 = (byte)((value & 0xFF00) >> 8);
- * byte1 = (byte)(value & 0x00FF);
+ * byte0 = (byte)((value & 0xFF00) >> 8);
+ * byte1 = (byte)(value & 0x00FF);
*

* * The value written can be read using the readShort and @@ -203,8 +208,8 @@ * value passed. These bytes will be written "big endian". That is, * with the high byte written first in the following manner: *

- * byte0 = (byte)((value & 0xFF00) >> 8);
- * byte1 = (byte)(value & 0x00FF);
+ * byte0 = (byte)((value & 0xFF00) >> 8);
+ * byte1 = (byte)(value & 0x00FF);
*

* * The value written can be read using the readChar @@ -228,10 +233,10 @@ * of the passed value will be written "big endian". That is, with * the high byte written first in the following manner: *

- * byte0 = (byte)((value & 0xFF000000) >> 24);
- * byte1 = (byte)((value & 0x00FF0000) >> 16);
- * byte2 = (byte)((value & 0x0000FF00) >> 8);
- * byte3 = (byte)(value & 0x000000FF);
+ * byte0 = (byte)((value & 0xFF000000) >> 24);
+ * byte1 = (byte)((value & 0x00FF0000) >> 16);
+ * byte2 = (byte)((value & 0x0000FF00) >> 8);
+ * byte3 = (byte)(value & 0x000000FF);
*

* * The value written can be read using the readInt @@ -256,14 +261,14 @@ * of the passed value will be written "big endian". That is, with * the high byte written first in the following manner: *

- * byte0 = (byte)((value & 0xFF00000000000000L) >> 56);
- * byte1 = (byte)((value & 0x00FF000000000000L) >> 48);
- * byte2 = (byte)((value & 0x0000FF0000000000L) >> 40);
- * byte3 = (byte)((value & 0x000000FF00000000L) >> 32);
- * byte4 = (byte)((value & 0x00000000FF000000L) >> 24);
- * byte5 = (byte)((value & 0x0000000000FF0000L) >> 16);
- * byte6 = (byte)((value & 0x000000000000FF00L) >> 8);
- * byte7 = (byte)(value & 0x00000000000000FFL);
+ * byte0 = (byte)((value & 0xFF00000000000000L) >> 56);
+ * byte1 = (byte)((value & 0x00FF000000000000L) >> 48);
+ * byte2 = (byte)((value & 0x0000FF0000000000L) >> 40);
+ * byte3 = (byte)((value & 0x000000FF00000000L) >> 32);
+ * byte4 = (byte)((value & 0x00000000FF000000L) >> 24);
+ * byte5 = (byte)((value & 0x0000000000FF0000L) >> 16);
+ * byte6 = (byte)((value & 0x000000000000FF00L) >> 8);
+ * byte7 = (byte)(value & 0x00000000000000FFL);
*

* * The value written can be read using the readLong @@ -349,8 +354,21 @@ public final void writeBytes (String value) throws IOException { int len = value.length(); + int index = 0; + byte[] buffer = new byte[len > BUFFER_SIZE ? BUFFER_SIZE : len]; for (int i = 0; i < len; ++i) - writeByte (value.charAt(i)); + { + buffer[index] = (byte) (value.charAt(i) & 0xff); + ++index; + + if (index == BUFFER_SIZE) + [ + write(buffer, 0, index); + index = 0; + } + } + if (index > 0) + write(buffer, 0, index); } /** @@ -368,8 +386,23 @@ public final void writeChars (String value) throws IOException { int len = value.length(); + int index = 0; + byte[] buffer = new byte[(len * 2) > BUFFER_SIZE ? BUFFER_SIZE : len * 2]; for (int i = 0; i < len; ++i) - writeChar (value.charAt(i)); + { + char ch = value.charAt(i); + buffer[index] = (byte) (0xff & (ch >> 8)); + buffer[index + 1] = (byte) (0xff & ch); + index += 2; + + if ((index + 2) >= BUFFER_SIZE) + { + write(buffer, 0, index); + index = 0; + } + } + if (index > 0) + write(buffer, 0, index); } /** @@ -421,31 +454,48 @@ } if (sum > 65535) - throw new UTFDataFormatException (); + throw new UTFDataFormatException(); - writeShort (sum); + writeShort(sum); + int index = 0; + byte[] buffer = new byte[BUFFER_SIZE]; + for (int i = 0; i < len; ++i) { char c = value.charAt(i); if (c >= '\u0001' && c <= '\u007f') - write (c); + { + buffer[index] = (byte) c; + ++index; + } else if (c == '\u0000' || (c >= '\u0080' && c <= '\u07ff')) { - write (0xc0 | (0x1f & (c >> 6))); - write (0x80 | (0x3f & c)); + buffer [index] = (byte) (0xc0 | (0x1f & (c >> 6))); + buffer [index + 1] = (byte) (0x80 | (0x3f & c)); + index += 2; } else { // JSL says the first byte should be or'd with 0xc0, but // that is a typo. Unicode says 0xe0, and that is what is // consistent with DataInputStream. - write (0xe0 | (0x0f & (c >> 12))); - write (0x80 | (0x3f & (c >> 6))); - write (0x80 | (0x3f & c)); + buffer [index] = (byte) (0xe0 | (0x0f & (c >> 12))); + buffer [index + 1] = (byte) (0x80 | (0x3f & (c >> 6))); + buffer [index + 2] = (byte) (0x80 | (0x3f & c)); + index += 3; + } + + // If the buffer is full write it to the underlaying stream. + if ((index + 3) > BUFFER_SIZE) + { + write(buffer, 0, index); + index = 0; } } - } + // If there are some bytes left write them to the underlying stream. + if (index > 0) + write(buffer, 0, index); + } } // class DataOutputStream -