diff options
author | mark <mark@138bc75d-0d04-0410-961f-82ee72b054a4> | 2006-05-18 17:29:21 +0000 |
---|---|---|
committer | mark <mark@138bc75d-0d04-0410-961f-82ee72b054a4> | 2006-05-18 17:29:21 +0000 |
commit | 64089cc9f030d8ef7972adb5d117e0b23f47d62b (patch) | |
tree | 9f9c470de62ee62fba1331a396450d728d2b1fad /libjava/classpath/java/io/DataOutputStream.java | |
parent | 96034e28360d660d7a7708807fcbc4b519574d8e (diff) | |
download | gcc-64089cc9f030d8ef7972adb5d117e0b23f47d62b.tar.gz |
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
Diffstat (limited to 'libjava/classpath/java/io/DataOutputStream.java')
-rw-r--r-- | libjava/classpath/java/io/DataOutputStream.java | 105 |
1 files changed, 70 insertions, 35 deletions
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 @@ -63,6 +63,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 <code>DataOutputStream</code> to * write its data to the specified underlying <code>OutputStream</code> * @@ -373,6 +378,37 @@ public class DataOutputStream extends FilterOutputStream implements DataOutput } /** + * Calculate the length, in bytes, of a <code>String</code> in Utf8 format. + * + * @param value The <code>String</code> 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 <code>String</code> to the stream in a modified * UTF-8 format. First, two bytes are written to the stream indicating the * number of bytes to follow. Note that this is the number of bytes in 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 |