From 64089cc9f030d8ef7972adb5d117e0b23f47d62b Mon Sep 17 00:00:00 2001 From: mark Date: Thu, 18 May 2006 17:29:21 +0000 Subject: Imported GNU Classpath 0.90 * scripts/makemake.tcl: LocaleData.java moved to gnu/java/locale. * sources.am: Regenerated. * gcj/javaprims.h: Regenerated. * Makefile.in: Regenerated. * gcj/Makefile.in: Regenerated. * include/Makefile.in: Regenerated. * testsuite/Makefile.in: Regenerated. * gnu/java/lang/VMInstrumentationImpl.java: New override. * gnu/java/net/local/LocalSocketImpl.java: Likewise. * gnu/classpath/jdwp/VMMethod.java: Likewise. * gnu/classpath/jdwp/VMVirtualMachine.java: Update to latest interface. * java/lang/Thread.java: Add UncaughtExceptionHandler. * java/lang/reflect/Method.java: Implements GenericDeclaration and isSynthetic(), * java/lang/reflect/Field.java: Likewise. * java/lang/reflect/Constructor.java * java/lang/Class.java: Implements Type, GenericDeclaration, getSimpleName() and getEnclosing*() methods. * java/lang/Class.h: Add new public methods. * java/lang/Math.java: Add signum(), ulp() and log10(). * java/lang/natMath.cc (log10): New function. * java/security/VMSecureRandom.java: New override. * java/util/logging/Logger.java: Updated to latest classpath version. * java/util/logging/LogManager.java: New override. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@113887 138bc75d-0d04-0410-961f-82ee72b054a4 --- libjava/classpath/java/io/DataOutputStream.java | 105 ++++++++++++++++-------- 1 file changed, 70 insertions(+), 35 deletions(-) (limited to 'libjava/classpath/java/io/DataOutputStream.java') diff --git a/libjava/classpath/java/io/DataOutputStream.java b/libjava/classpath/java/io/DataOutputStream.java index 25178160dc8..6670c2dba13 100644 --- a/libjava/classpath/java/io/DataOutputStream.java +++ b/libjava/classpath/java/io/DataOutputStream.java @@ -62,6 +62,11 @@ public class DataOutputStream extends FilterOutputStream implements DataOutput */ protected int written; + /** + * Utf8 byte buffer, used by writeUTF() + */ + private byte[] buf; + /** * This method initializes an instance of DataOutputStream to * write its data to the specified underlying OutputStream @@ -372,6 +377,37 @@ public class DataOutputStream extends FilterOutputStream implements DataOutput writeChar (value.charAt(i)); } + /** + * Calculate the length, in bytes, of a String in Utf8 format. + * + * @param value The String to measure + * @param start String index at which to begin count + * @param sum Starting Utf8 byte count + * + * @throws UTFDataFormatException if result would exceed 65535 + */ + private int getUTFlength(String value, int start, int sum) + throws IOException + { + int len = value.length(); + + for (int i = start; i < len && sum <= 65535; ++i) + { + char c = value.charAt(i); + if (c >= '\u0001' && c <= '\u007f') + sum += 1; + else if (c == '\u0000' || (c >= '\u0080' && c <= '\u07ff')) + sum += 2; + else + sum += 3; + } + + if (sum > 65535) + throw new UTFDataFormatException (); + + return sum; + } + /** * This method writes a Java String to the stream in a modified * UTF-8 format. First, two bytes are written to the stream indicating the @@ -407,48 +443,47 @@ public class DataOutputStream extends FilterOutputStream implements DataOutput public final synchronized void writeUTF(String value) throws IOException { int len = value.length(); - int sum = 0; - - for (int i = 0; i < len && sum <= 65535; ++i) - { - char c = value.charAt(i); - if (c >= '\u0001' && c <= '\u007f') - sum += 1; - else if (c == '\u0000' || (c >= '\u0080' && c <= '\u07ff')) - sum += 2; - else - sum += 3; - } - - if (sum > 65535) - throw new UTFDataFormatException (); - + int i = 0; int pos = 0; - byte[] buf = new byte[sum]; + boolean lengthWritten = false; - for (int i = 0; i < len; ++i) + if (buf == null) + buf = new byte[512]; + + do { - char c = value.charAt(i); - if (c >= '\u0001' && c <= '\u007f') - buf[pos++] = (byte) c; - else if (c == '\u0000' || (c >= '\u0080' && c <= '\u07ff')) + while (i < len && pos < buf.length - 3) { - buf[pos++] = (byte) (0xc0 | (0x1f & (c >> 6))); - buf[pos++] = (byte) (0x80 | (0x3f & c)); + char c = value.charAt(i++); + if (c >= '\u0001' && c <= '\u007f') + buf[pos++] = (byte) c; + else if (c == '\u0000' || (c >= '\u0080' && c <= '\u07ff')) + { + buf[pos++] = (byte) (0xc0 | (0x1f & (c >> 6))); + buf[pos++] = (byte) (0x80 | (0x3f & c)); + } + 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. + buf[pos++] = (byte) (0xe0 | (0x0f & (c >> 12))); + buf[pos++] = (byte) (0x80 | (0x3f & (c >> 6))); + buf[pos++] = (byte) (0x80 | (0x3f & c)); + } } - else + if (! lengthWritten) { - // 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. - buf[pos++] = (byte) (0xe0 | (0x0f & (c >> 12))); - buf[pos++] = (byte) (0x80 | (0x3f & (c >> 6))); - buf[pos++] = (byte) (0x80 | (0x3f & c)); + if (i == len) + writeShort(pos); + else + writeShort(getUTFlength(value, i, pos)); + lengthWritten = true; } - } - - writeShort (sum); - write(buf, 0, sum); + write(buf, 0, pos); + pos = 0; + } + while (i < len); } } // class DataOutputStream -- cgit v1.2.1