You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by nd...@apache.org on 2006/08/18 03:45:39 UTC
svn commit: r432462 [3/21] -
/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/
Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/DataOutputStream.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/DataOutputStream.java?rev=432462&r1=432461&r2=432462&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/DataOutputStream.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/DataOutputStream.java Thu Aug 17 18:45:35 2006
@@ -1,457 +1,457 @@
-/* Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package java.io;
-
-import org.apache.harmony.luni.util.Msg;
-
-/**
- * DataOutputStream is a filter class which can write typed data to a Stream.
- * Typically, this stream can be read in by a DataInputStream. Types that can be
- * written include byte, 16-bit short, 32-bit int, 32-bit float, 64-bit long,
- * 64-bit double, byte strings, and UTF Strings.
- *
- * @see DataInputStream
- */
-public class DataOutputStream extends FilterOutputStream implements DataOutput {
- /** The number of bytes written out so far */
- protected int written;
-
- /**
- * Constructs a new DataOutputStream on the OutputStream <code>out</code>.
- * All writes can now be filtered through this stream. Note that data
- * written by this Stream is not in a human readable format but can be
- * reconstructed by using a DataInputStream on the resulting output.
- *
- * @param out
- * the target OutputStream to filter writes on.
- */
- public DataOutputStream(OutputStream out) {
- super(out);
- }
-
- /**
- * Flush this DataOutputStream to ensure all pending data is sent out to the
- * target OutputStream. This implementation flushes the target OutputStream.
- *
- * @throws IOException
- * If an error occurs attempting to flush this DataOutputStream.
- */
- @Override
- public void flush() throws IOException {
- super.flush();
- }
-
- /**
- * Answers the total number of bytes written to this stream thus far.
- *
- * @return the number of bytes written to this DataOutputStream.
- */
- public final int size() {
- if (written < 0) {
- written = Integer.MAX_VALUE;
- }
- return written;
- }
-
- /**
- * Writes <code>count</code> <code>bytes</code> from the byte array
- * <code>buffer</code> starting at offset <code>index</code> to the
- * OutputStream.
- *
- * @param buffer
- * the buffer to be written
- * @param offset
- * offset in buffer to get bytes
- * @param count
- * number of bytes in buffer to write
- *
- * @throws IOException
- * If an error occurs attempting to write to this
- * DataOutputStream.
- *
- * @see DataInput#readFully(byte[])
- * @see DataInput#readFully(byte[], int, int)
- */
- @Override
- public void write(byte buffer[], int offset, int count) throws IOException {
- if (buffer == null) {
- throw new NullPointerException(Msg.getString("K0047")); //$NON-NLS-1$
- }
- out.write(buffer, offset, count);
- written += count;
- }
-
- /**
- * Writes the specified <code>byte</code> to the OutputStream.
- *
- * @param oneByte
- * the byte to be written
- *
- * @throws IOException
- * If an error occurs attempting to write to this
- * DataOutputStream.
- *
- * @see DataInput#readByte()
- */
- @Override
- public void write(int oneByte) throws IOException {
- out.write(oneByte);
- written++;
- }
-
- /**
- * Writes a boolean to this output stream.
- *
- * @param val
- * the boolean value to write to the OutputStream
- *
- * @throws IOException
- * If an error occurs attempting to write to this
- * DataOutputStream.
- *
- * @see DataInput#readBoolean()
- */
- public final void writeBoolean(boolean val) throws IOException {
- out.write(val ? 1 : 0);
- written++;
- }
-
- /**
- * Writes a 8-bit byte to this output stream.
- *
- * @param val
- * the byte value to write to the OutputStream
- *
- * @throws IOException
- * If an error occurs attempting to write to this
- * DataOutputStream.
- *
- * @see DataInput#readByte()
- * @see DataInput#readUnsignedByte()
- */
- public final void writeByte(int val) throws IOException {
- out.write(val);
- written++;
- }
-
- /**
- * Writes the low order 8-bit bytes from a String to this output stream.
- *
- * @param str
- * the String containing the bytes to write to the OutputStream
- *
- * @throws IOException
- * If an error occurs attempting to write to this
- * DataOutputStream.
- *
- * @see DataInput#readFully(byte[])
- * @see DataInput#readFully(byte[],int,int)
- */
- public final void writeBytes(String str) throws IOException {
- if (str.length() == 0) {
- return;
- }
- byte bytes[] = new byte[str.length()];
- for (int index = 0; index < str.length(); index++) {
- bytes[index] = (byte) str.charAt(index);
- }
- out.write(bytes);
- written += bytes.length;
- }
-
- /**
- * Writes the specified 16-bit character to the OutputStream. Only the lower
- * 2 bytes are written with the higher of the 2 bytes written first. This
- * represents the Unicode value of val.
- *
- * @param val
- * the character to be written
- *
- * @throws IOException
- * If an error occurs attempting to write to this
- * DataOutputStream.
- *
- * @see DataInput#readChar()
- */
- public final void writeChar(int val) throws IOException {
- out.write(val >> 8);
- out.write(val);
- written += 2;
- }
-
- /**
- * Writes the specified 16-bit characters contained in str to the
- * OutputStream. Only the lower 2 bytes of each character are written with
- * the higher of the 2 bytes written first. This represents the Unicode
- * value of each character in str.
- *
- * @param str
- * the String whose characters are to be written.
- *
- * @throws IOException
- * If an error occurs attempting to write to this
- * DataOutputStream.
- *
- * @see DataInput#readChar()
- */
- public final void writeChars(String str) throws IOException {
- byte newBytes[] = new byte[str.length() * 2];
- for (int index = 0; index < str.length(); index++) {
- int newIndex = index == 0 ? index : index * 2;
- newBytes[newIndex] = (byte) (str.charAt(index) >> 8);
- newBytes[newIndex + 1] = (byte) str.charAt(index);
- }
- out.write(newBytes);
- written += newBytes.length;
- }
-
- /**
- * Writes a 64-bit double to this output stream. The resulting output is the
- * 8 bytes resulting from calling Double.doubleToLongBits().
- *
- * @param val
- * the double to be written.
- *
- * @throws IOException
- * If an error occurs attempting to write to this
- * DataOutputStream.
- *
- * @see DataInput#readDouble()
- */
- public final void writeDouble(double val) throws IOException {
- writeLong(Double.doubleToLongBits(val));
- }
-
- /**
- * Writes a 32-bit float to this output stream. The resulting output is the
- * 4 bytes resulting from calling Float.floatToIntBits().
- *
- * @param val
- * the float to be written.
- *
- * @throws IOException
- * If an error occurs attempting to write to this
- * DataOutputStream.
- *
- * @see DataInput#readFloat()
- */
- public final void writeFloat(float val) throws IOException {
- writeInt(Float.floatToIntBits(val));
- }
-
- /**
- * Writes a 32-bit int to this output stream. The resulting output is the 4
- * bytes, highest order first, of val.
- *
- * @param val
- * the int to be written.
- *
- * @throws IOException
- * If an error occurs attempting to write to this
- * DataOutputStream.
- *
- * @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;
- }
-
- /**
- * Writes a 64-bit long to this output stream. The resulting output is the 8
- * bytes, highest order first, of val.
- *
- * @param val
- * the long to be written.
- *
- * @throws IOException
- * If an error occurs attempting to write to this
- * DataOutputStream.
- *
- * @see DataInput#readLong()
- */
- public final void writeLong(long val) throws IOException {
- writeInt((int) (val >> 32));
- writeInt((int) val);
- }
-
- /**
- * Writes the specified 16-bit short to the OutputStream. Only the lower 2
- * bytes are written with the higher of the 2 bytes written first.
- *
- * @param val
- * the short to be written
- *
- * @throws IOException
- * If an error occurs attempting to write to this
- * DataOutputStream.
- *
- * @see DataInput#readShort()
- * @see DataInput#readUnsignedShort()
- */
- public final void writeShort(int val) throws IOException {
- writeChar(val);
- }
-
- /**
- * Writes the specified String out in UTF format.
- *
- * @param str
- * the String to be written in UTF format.
- *
- * @throws IOException
- * If an error occurs attempting to write to this
- * DataOutputStream.
- *
- * @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(
- org.apache.harmony.luni.util.Msg.getString("K0068")); //$NON-NLS-1$
- }
- }
- }
-
- long countUTFBytes(String str) {
- int utfCount = 0, length = str.length();
- for (int i = 0; i < length; i++) {
- int charValue = str.charAt(i);
- if (charValue > 0 && charValue <= 127) {
- utfCount++;
- } else if (charValue <= 2047) {
- utfCount += 2;
- } else {
- utfCount += 3;
- }
- }
- return utfCount;
- }
-
- 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;
- }
- 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;
- }
- }
-}
+/* Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.io;
+
+import org.apache.harmony.luni.util.Msg;
+
+/**
+ * DataOutputStream is a filter class which can write typed data to a Stream.
+ * Typically, this stream can be read in by a DataInputStream. Types that can be
+ * written include byte, 16-bit short, 32-bit int, 32-bit float, 64-bit long,
+ * 64-bit double, byte strings, and UTF Strings.
+ *
+ * @see DataInputStream
+ */
+public class DataOutputStream extends FilterOutputStream implements DataOutput {
+ /** The number of bytes written out so far */
+ protected int written;
+
+ /**
+ * Constructs a new DataOutputStream on the OutputStream <code>out</code>.
+ * All writes can now be filtered through this stream. Note that data
+ * written by this Stream is not in a human readable format but can be
+ * reconstructed by using a DataInputStream on the resulting output.
+ *
+ * @param out
+ * the target OutputStream to filter writes on.
+ */
+ public DataOutputStream(OutputStream out) {
+ super(out);
+ }
+
+ /**
+ * Flush this DataOutputStream to ensure all pending data is sent out to the
+ * target OutputStream. This implementation flushes the target OutputStream.
+ *
+ * @throws IOException
+ * If an error occurs attempting to flush this DataOutputStream.
+ */
+ @Override
+ public void flush() throws IOException {
+ super.flush();
+ }
+
+ /**
+ * Answers the total number of bytes written to this stream thus far.
+ *
+ * @return the number of bytes written to this DataOutputStream.
+ */
+ public final int size() {
+ if (written < 0) {
+ written = Integer.MAX_VALUE;
+ }
+ return written;
+ }
+
+ /**
+ * Writes <code>count</code> <code>bytes</code> from the byte array
+ * <code>buffer</code> starting at offset <code>index</code> to the
+ * OutputStream.
+ *
+ * @param buffer
+ * the buffer to be written
+ * @param offset
+ * offset in buffer to get bytes
+ * @param count
+ * number of bytes in buffer to write
+ *
+ * @throws IOException
+ * If an error occurs attempting to write to this
+ * DataOutputStream.
+ *
+ * @see DataInput#readFully(byte[])
+ * @see DataInput#readFully(byte[], int, int)
+ */
+ @Override
+ public void write(byte buffer[], int offset, int count) throws IOException {
+ if (buffer == null) {
+ throw new NullPointerException(Msg.getString("K0047")); //$NON-NLS-1$
+ }
+ out.write(buffer, offset, count);
+ written += count;
+ }
+
+ /**
+ * Writes the specified <code>byte</code> to the OutputStream.
+ *
+ * @param oneByte
+ * the byte to be written
+ *
+ * @throws IOException
+ * If an error occurs attempting to write to this
+ * DataOutputStream.
+ *
+ * @see DataInput#readByte()
+ */
+ @Override
+ public void write(int oneByte) throws IOException {
+ out.write(oneByte);
+ written++;
+ }
+
+ /**
+ * Writes a boolean to this output stream.
+ *
+ * @param val
+ * the boolean value to write to the OutputStream
+ *
+ * @throws IOException
+ * If an error occurs attempting to write to this
+ * DataOutputStream.
+ *
+ * @see DataInput#readBoolean()
+ */
+ public final void writeBoolean(boolean val) throws IOException {
+ out.write(val ? 1 : 0);
+ written++;
+ }
+
+ /**
+ * Writes a 8-bit byte to this output stream.
+ *
+ * @param val
+ * the byte value to write to the OutputStream
+ *
+ * @throws IOException
+ * If an error occurs attempting to write to this
+ * DataOutputStream.
+ *
+ * @see DataInput#readByte()
+ * @see DataInput#readUnsignedByte()
+ */
+ public final void writeByte(int val) throws IOException {
+ out.write(val);
+ written++;
+ }
+
+ /**
+ * Writes the low order 8-bit bytes from a String to this output stream.
+ *
+ * @param str
+ * the String containing the bytes to write to the OutputStream
+ *
+ * @throws IOException
+ * If an error occurs attempting to write to this
+ * DataOutputStream.
+ *
+ * @see DataInput#readFully(byte[])
+ * @see DataInput#readFully(byte[],int,int)
+ */
+ public final void writeBytes(String str) throws IOException {
+ if (str.length() == 0) {
+ return;
+ }
+ byte bytes[] = new byte[str.length()];
+ for (int index = 0; index < str.length(); index++) {
+ bytes[index] = (byte) str.charAt(index);
+ }
+ out.write(bytes);
+ written += bytes.length;
+ }
+
+ /**
+ * Writes the specified 16-bit character to the OutputStream. Only the lower
+ * 2 bytes are written with the higher of the 2 bytes written first. This
+ * represents the Unicode value of val.
+ *
+ * @param val
+ * the character to be written
+ *
+ * @throws IOException
+ * If an error occurs attempting to write to this
+ * DataOutputStream.
+ *
+ * @see DataInput#readChar()
+ */
+ public final void writeChar(int val) throws IOException {
+ out.write(val >> 8);
+ out.write(val);
+ written += 2;
+ }
+
+ /**
+ * Writes the specified 16-bit characters contained in str to the
+ * OutputStream. Only the lower 2 bytes of each character are written with
+ * the higher of the 2 bytes written first. This represents the Unicode
+ * value of each character in str.
+ *
+ * @param str
+ * the String whose characters are to be written.
+ *
+ * @throws IOException
+ * If an error occurs attempting to write to this
+ * DataOutputStream.
+ *
+ * @see DataInput#readChar()
+ */
+ public final void writeChars(String str) throws IOException {
+ byte newBytes[] = new byte[str.length() * 2];
+ for (int index = 0; index < str.length(); index++) {
+ int newIndex = index == 0 ? index : index * 2;
+ newBytes[newIndex] = (byte) (str.charAt(index) >> 8);
+ newBytes[newIndex + 1] = (byte) str.charAt(index);
+ }
+ out.write(newBytes);
+ written += newBytes.length;
+ }
+
+ /**
+ * Writes a 64-bit double to this output stream. The resulting output is the
+ * 8 bytes resulting from calling Double.doubleToLongBits().
+ *
+ * @param val
+ * the double to be written.
+ *
+ * @throws IOException
+ * If an error occurs attempting to write to this
+ * DataOutputStream.
+ *
+ * @see DataInput#readDouble()
+ */
+ public final void writeDouble(double val) throws IOException {
+ writeLong(Double.doubleToLongBits(val));
+ }
+
+ /**
+ * Writes a 32-bit float to this output stream. The resulting output is the
+ * 4 bytes resulting from calling Float.floatToIntBits().
+ *
+ * @param val
+ * the float to be written.
+ *
+ * @throws IOException
+ * If an error occurs attempting to write to this
+ * DataOutputStream.
+ *
+ * @see DataInput#readFloat()
+ */
+ public final void writeFloat(float val) throws IOException {
+ writeInt(Float.floatToIntBits(val));
+ }
+
+ /**
+ * Writes a 32-bit int to this output stream. The resulting output is the 4
+ * bytes, highest order first, of val.
+ *
+ * @param val
+ * the int to be written.
+ *
+ * @throws IOException
+ * If an error occurs attempting to write to this
+ * DataOutputStream.
+ *
+ * @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;
+ }
+
+ /**
+ * Writes a 64-bit long to this output stream. The resulting output is the 8
+ * bytes, highest order first, of val.
+ *
+ * @param val
+ * the long to be written.
+ *
+ * @throws IOException
+ * If an error occurs attempting to write to this
+ * DataOutputStream.
+ *
+ * @see DataInput#readLong()
+ */
+ public final void writeLong(long val) throws IOException {
+ writeInt((int) (val >> 32));
+ writeInt((int) val);
+ }
+
+ /**
+ * Writes the specified 16-bit short to the OutputStream. Only the lower 2
+ * bytes are written with the higher of the 2 bytes written first.
+ *
+ * @param val
+ * the short to be written
+ *
+ * @throws IOException
+ * If an error occurs attempting to write to this
+ * DataOutputStream.
+ *
+ * @see DataInput#readShort()
+ * @see DataInput#readUnsignedShort()
+ */
+ public final void writeShort(int val) throws IOException {
+ writeChar(val);
+ }
+
+ /**
+ * Writes the specified String out in UTF format.
+ *
+ * @param str
+ * the String to be written in UTF format.
+ *
+ * @throws IOException
+ * If an error occurs attempting to write to this
+ * DataOutputStream.
+ *
+ * @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(
+ org.apache.harmony.luni.util.Msg.getString("K0068")); //$NON-NLS-1$
+ }
+ }
+ }
+
+ long countUTFBytes(String str) {
+ int utfCount = 0, length = str.length();
+ for (int i = 0; i < length; i++) {
+ int charValue = str.charAt(i);
+ if (charValue > 0 && charValue <= 127) {
+ utfCount++;
+ } else if (charValue <= 2047) {
+ utfCount += 2;
+ } else {
+ utfCount += 3;
+ }
+ }
+ return utfCount;
+ }
+
+ 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;
+ }
+ 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;
+ }
+ }
+}
Propchange: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/DataOutputStream.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/EOFException.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/EOFException.java?rev=432462&r1=432461&r2=432462&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/EOFException.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/EOFException.java Thu Aug 17 18:45:35 2006
@@ -1,48 +1,48 @@
-/* Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-
-package java.io;
-
-
-/**
- * This End Of File (EOF) exception is thrown when a program encounters the end
- * of a file or stream during an operation.
- *
- */
-public class EOFException extends IOException {
-
- private static final long serialVersionUID = 6433858223774886977L;
-
- /**
- * Constructs a new instance of this class with its walkback filled in.
- *
- */
- public EOFException() {
- super();
- }
-
- /**
- * Constructs a new instance of this class with its walkback and message
- * filled in.
- *
- * @param detailMessage
- * The detail message for the exception.
- */
- public EOFException(String detailMessage) {
- super(detailMessage);
- }
-
-}
+/* Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+package java.io;
+
+
+/**
+ * This End Of File (EOF) exception is thrown when a program encounters the end
+ * of a file or stream during an operation.
+ *
+ */
+public class EOFException extends IOException {
+
+ private static final long serialVersionUID = 6433858223774886977L;
+
+ /**
+ * Constructs a new instance of this class with its walkback filled in.
+ *
+ */
+ public EOFException() {
+ super();
+ }
+
+ /**
+ * Constructs a new instance of this class with its walkback and message
+ * filled in.
+ *
+ * @param detailMessage
+ * The detail message for the exception.
+ */
+ public EOFException(String detailMessage) {
+ super(detailMessage);
+ }
+
+}
Propchange: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/EOFException.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/EmulatedFields.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/EmulatedFields.java?rev=432462&r1=432461&r2=432462&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/EmulatedFields.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/EmulatedFields.java Thu Aug 17 18:45:35 2006
@@ -1,600 +1,600 @@
-/* Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package java.io;
-
-
-/**
- * An EmulatedFields is an object that represents a set of emulated fields for
- * an object being dumped or loaded. It allows objects to be dumped with a shape
- * different than the fields they were declared to have.
- *
- * @see ObjectInputStream.GetField
- * @see ObjectOutputStream.PutField
- * @see EmulatedFieldsForLoading
- * @see EmulatedFieldsForDumping
- */
-
-class EmulatedFields {
- static class ObjectSlot {
- // A slot is a field plus its value
- ObjectStreamField field; // Field descriptor
-
- Object fieldValue; // Actual value this emulated field holds
-
- boolean defaulted = true; // If this field has a default value (true)
- // or something has been assigned (false)
-
- /**
- * Returns the descriptor for this emulated field.
- *
- * @return the field descriptor
- */
- public ObjectStreamField getField() {
- return field;
- }
-
- /**
- * Returns the value held by this emulated field.
- *
- * @return the field value
- */
- public Object getFieldValue() {
- return fieldValue;
- }
- }
-
- private ObjectSlot[] slotsToSerialize; // The collection of slots the
- // receiver represents
-
- private ObjectStreamField[] declaredFields;
-
- /**
- * Constructs a new instance of EmulatedFields.
- *
- * @param fields
- * an array of ObjectStreamFields, which describe the fields to
- * be emulated (names, types, etc).
- * @param declared
- * an array of ObjectStreamFields, which describe the declared
- * fields.
- */
- public EmulatedFields(ObjectStreamField[] fields,
- ObjectStreamField[] declared) {
- super();
- // We assume the slots are already sorted in the right shape for dumping
- buildSlots(fields);
- declaredFields = declared;
- }
-
- /**
- * Build emulated slots that correspond to emulated fields. A slot is a
- * field descriptor (ObjectStreamField) plus the actual value it holds.
- *
- * @param fields
- * an array of ObjectStreamField, which describe the fields to be
- * emulated (names, types, etc).
- */
- private void buildSlots(ObjectStreamField[] fields) {
- slotsToSerialize = new ObjectSlot[fields.length];
- for (int i = 0; i < fields.length; i++) {
- ObjectSlot s = new ObjectSlot();
- slotsToSerialize[i] = s;
- s.field = fields[i];
- }
- // We assume the slots are already sorted in the right shape for dumping
- }
-
- /**
- * Return a boolean indicating if the field named <code>name</code> has
- * been assigned a value explicitly (false) or if it still holds a default
- * value for the type (true) because it hasn't been assigned to yet.
- *
- * @param name
- * a String, the name of the field to test
- * @return <code>true</code> if <code>name</code> still holds its
- * default value, <code>false</code> otherwise
- *
- * @throws IllegalArgumentException
- * If <code>name</code> is null
- */
- public boolean defaulted(String name) throws IllegalArgumentException {
- ObjectSlot slot = findSlot(name, null);
- if (slot != null)
- return slot.defaulted;
- throw new IllegalArgumentException();
- }
-
- /**
- * Find and return an ObjectSlot that corresponds to a field named
- * <code>fieldName</code> and type <code>fieldType</code>. If the field
- * type <code>fieldType</code> corresponds to a primitive type, the field
- * type has to match exactly or <code>null</code> is returned. If the
- * field type <code>fieldType</code> corresponds to an object type, the
- * field type has to be compatible in terms of assignment, or null is
- * returned. If <code>fieldType</code> is <code>null</code>, no such
- * compatibility checking is performed and the slot is returned.
- *
- * @param fieldName
- * A String, the name of the field to find
- * @param fieldType
- * A Class, the type of the field. This will be used to test
- * compatibility. If null, no testing is done, the corresponding
- * slot is returned.
- * @return If there is no field with that name, or no compatible field
- * (relative to <code>fieldType</code>)
- */
- private ObjectSlot findSlot(String fieldName, Class<?> fieldType) {
- boolean isPrimitive = fieldType != null && fieldType.isPrimitive();
-
- for (int i = 0; i < slotsToSerialize.length; i++) {
- ObjectSlot slot = slotsToSerialize[i];
- if (slot.field.getName().equals(fieldName))
- if (isPrimitive) {
- // Looking for a primitive type field. Types must match
- // *exactly*
- if (slot.field.getType() == fieldType)
- return slot;
- } else {
- // Looking for a non-primitive type field.
- if (fieldType == null)
- return slot; // Null means we take anything
- // Types must be compatible (assignment)
- if (slot.field.getType().isAssignableFrom(fieldType))
- return slot;
- }
- }
-
- if (declaredFields != null) {
- for (int i = 0; i < declaredFields.length; i++) {
- ObjectStreamField field = declaredFields[i];
- if (field.getName().equals(fieldName)) {
- if (isPrimitive ? field.getType() == fieldType
- : fieldType == null
- || field.getType().isAssignableFrom(
- fieldType)) {
- ObjectSlot slot = new ObjectSlot();
- slot.field = field;
- slot.defaulted = true;
- return slot;
- }
- }
- }
- }
- return null;
- }
-
- /**
- * Find and return the byte value of a given field named <code>name</code>
- * in the receiver. If the field has not been assigned any value yet, the
- * default value <code>defaultValue</code> is returned instead.
- *
- * @param name
- * A String, the name of the field to find
- * @param defaultValue
- * Return value in case the field has not been assigned to yet.
- * @return the value of the given field if it has been assigned, the default
- * value otherwise
- *
- * @throws IllegalArgumentException
- * If the corresponding field can not be found.
- */
- public byte get(String name, byte defaultValue)
- throws IllegalArgumentException {
- ObjectSlot slot = findSlot(name, Byte.TYPE);
- if (slot != null) // if not initialized yet, we give the default value
- return slot.defaulted ? defaultValue : ((Byte) slot.fieldValue)
- .byteValue();
- throw new IllegalArgumentException();
- }
-
- /**
- * Find and return the char value of a given field named <code>name</code>
- * in the receiver. If the field has not been assigned any value yet, the
- * default value <code>defaultValue</code> is returned instead.
- *
- * @param name
- * A String, the name of the field to find
- * @param defaultValue
- * Return value in case the field has not been assigned to yet.
- * @return the value of the given field if it has been assigned, the default
- * value otherwise
- *
- * @throws IllegalArgumentException
- * If the corresponding field can not be found.
- */
- public char get(String name, char defaultValue)
- throws IllegalArgumentException {
- ObjectSlot slot = findSlot(name, Character.TYPE);
- if (slot != null) // if not initialized yet, we give the default value
- return slot.defaulted ? defaultValue
- : ((Character) slot.fieldValue).charValue();
- throw new IllegalArgumentException();
- }
-
- /**
- * Find and return the double value of a given field named <code>name</code>
- * in the receiver. If the field has not been assigned any value yet, the
- * default value <code>defaultValue</code> is returned instead.
- *
- * @param name
- * A String, the name of the field to find
- * @param defaultValue
- * Return value in case the field has not been assigned to yet.
- * @return the value of the given field if it has been assigned, the default
- * value otherwise
- *
- * @throws IllegalArgumentException
- * If the corresponding field can not be found.
- */
- public double get(String name, double defaultValue)
- throws IllegalArgumentException {
- ObjectSlot slot = findSlot(name, Double.TYPE);
- if (slot != null) // if not initialized yet, we give the default value
- return slot.defaulted ? defaultValue : ((Double) slot.fieldValue)
- .doubleValue();
- throw new IllegalArgumentException();
- }
-
- /**
- * Find and return the float value of a given field named <code>name</code>
- * in the receiver. If the field has not been assigned any value yet, the
- * default value <code>defaultValue</code> is returned instead.
- *
- * @param name
- * A String, the name of the field to find
- * @param defaultValue
- * Return value in case the field has not been assigned to yet.
- * @return the value of the given field if it has been assigned, the default
- * value otherwise
- *
- * @throws IllegalArgumentException
- * If the corresponding field can not be found.
- */
- public float get(String name, float defaultValue)
- throws IllegalArgumentException {
- ObjectSlot slot = findSlot(name, Float.TYPE);
- if (slot != null) // if not initialized yet, we give the default value
- return slot.defaulted ? defaultValue : ((Float) slot.fieldValue)
- .floatValue();
- throw new IllegalArgumentException();
- }
-
- /**
- * Find and return the int value of a given field named <code>name</code>
- * in the receiver. If the field has not been assigned any value yet, the
- * default value <code>defaultValue</code> is returned instead.
- *
- * @param name
- * A String, the name of the field to find
- * @param defaultValue
- * Return value in case the field has not been assigned to yet.
- * @return the value of the given field if it has been assigned, the default
- * value otherwise
- *
- * @throws IllegalArgumentException
- * If the corresponding field can not be found.
- */
- public int get(String name, int defaultValue)
- throws IllegalArgumentException {
- ObjectSlot slot = findSlot(name, Integer.TYPE);
- if (slot != null) // if not initialized yet, we give the default value
- return slot.defaulted ? defaultValue : ((Integer) slot.fieldValue)
- .intValue();
- throw new IllegalArgumentException();
- }
-
- /**
- * Find and return the long value of a given field named <code>name</code>
- * in the receiver. If the field has not been assigned any value yet, the
- * default value <code>defaultValue</code> is returned instead.
- *
- * @param name
- * A String, the name of the field to find
- * @param defaultValue
- * Return value in case the field has not been assigned to yet.
- * @return the value of the given field if it has been assigned, the default
- * value otherwise
- *
- * @throws IllegalArgumentException
- * If the corresponding field can not be found.
- */
- public long get(String name, long defaultValue)
- throws IllegalArgumentException {
- ObjectSlot slot = findSlot(name, Long.TYPE);
- if (slot != null) // if not initialized yet, we give the default value
- return slot.defaulted ? defaultValue : ((Long) slot.fieldValue)
- .longValue();
- throw new IllegalArgumentException();
- }
-
- /**
- * Find and return the Object value of a given field named <code>name</code>
- * in the receiver. If the field has not been assigned any value yet, the
- * default value <code>defaultValue</code> is returned instead.
- *
- * @param name
- * A String, the name of the field to find
- * @param defaultValue
- * Return value in case the field has not been assigned to yet.
- * @return the value of the given field if it has been assigned, the default
- * value otherwise
- *
- * @throws IllegalArgumentException
- * If the corresponding field can not be found.
- */
- public Object get(String name, Object defaultValue)
- throws IllegalArgumentException {
- ObjectSlot slot = findSlot(name, null);
- if (slot != null && !slot.field.getType().isPrimitive()) // if not
- // initialized
- // yet, we
- // give the
- // default
- // value
- return slot.defaulted ? defaultValue : slot.fieldValue;
- throw new IllegalArgumentException();
- }
-
- /**
- * Find and return the short value of a given field named <code>name</code>
- * in the receiver. If the field has not been assigned any value yet, the
- * default value <code>defaultValue</code> is returned instead.
- *
- * @param name
- * A String, the name of the field to find
- * @param defaultValue
- * Return value in case the field has not been assigned to yet.
- * @return the value of the given field if it has been assigned, the default
- * value otherwise
- *
- * @throws IllegalArgumentException
- * If the corresponding field can not be found.
- */
- public short get(String name, short defaultValue)
- throws IllegalArgumentException {
- ObjectSlot slot = findSlot(name, Short.TYPE);
- if (slot != null) // if not initialized yet, we give the default value
- return slot.defaulted ? defaultValue : ((Short) slot.fieldValue)
- .shortValue();
- throw new IllegalArgumentException();
- }
-
- /**
- * Find and return the boolean value of a given field named
- * <code>name</code> in the receiver. If the field has not been assigned
- * any value yet, the default value <code>defaultValue</code> is returned
- * instead.
- *
- * @param name
- * A String, the name of the field to find
- * @param defaultValue
- * Return value in case the field has not been assigned to yet.
- * @return the value of the given field if it has been assigned, the default
- * value otherwise
- *
- * @throws IllegalArgumentException
- * If the corresponding field can not be found.
- */
- public boolean get(String name, boolean defaultValue)
- throws IllegalArgumentException {
- ObjectSlot slot = findSlot(name, Boolean.TYPE);
- if (slot != null) // if not initialized yet, we give the default value
- return slot.defaulted ? defaultValue : ((Boolean) slot.fieldValue)
- .booleanValue();
- throw new IllegalArgumentException();
- }
-
- /**
- * Find and set the byte value of a given field named <code>name</code> in
- * the receiver.
- *
- * @param name
- * A String, the name of the field to set
- * @param value
- * New value for the field.
- *
- * @throws IllegalArgumentException
- * If the corresponding field can not be found.
- */
- public void put(String name, byte value) throws IllegalArgumentException {
- ObjectSlot slot = findSlot(name, Byte.TYPE);
- if (slot != null) {
- slot.fieldValue = new Byte(value);
- slot.defaulted = false; // No longer default value
- } else
- throw new IllegalArgumentException();
- }
-
- /**
- * Find and set the char value of a given field named <code>name</code> in
- * the receiver.
- *
- * @param name
- * A String, the name of the field to set
- * @param value
- * New value for the field.
- *
- * @throws IllegalArgumentException
- * If the corresponding field can not be found.
- */
- public void put(String name, char value) throws IllegalArgumentException {
- ObjectSlot slot = findSlot(name, Character.TYPE);
- if (slot != null) {
- slot.fieldValue = new Character(value);
- slot.defaulted = false; // No longer default value
- } else
- throw new IllegalArgumentException();
- }
-
- /**
- * Find and set the double value of a given field named <code>name</code>
- * in the receiver.
- *
- * @param name
- * A String, the name of the field to set
- * @param value
- * New value for the field.
- *
- * @throws IllegalArgumentException
- * If the corresponding field can not be found.
- */
- public void put(String name, double value) throws IllegalArgumentException {
- ObjectSlot slot = findSlot(name, Double.TYPE);
- if (slot != null) {
- slot.fieldValue = new Double(value);
- slot.defaulted = false; // No longer default value
- } else
- throw new IllegalArgumentException();
- }
-
- /**
- * Find and set the float value of a given field named <code>name</code>
- * in the receiver.
- *
- * @param name
- * A String, the name of the field to set
- * @param value
- * New value for the field.
- *
- * @throws IllegalArgumentException
- * If the corresponding field can not be found.
- */
- public void put(String name, float value) throws IllegalArgumentException {
- ObjectSlot slot = findSlot(name, Float.TYPE);
- if (slot != null) {
- slot.fieldValue = new Float(value);
- slot.defaulted = false; // No longer default value
- } else
- throw new IllegalArgumentException();
- }
-
- /**
- * Find and set the int value of a given field named <code>name</code> in
- * the receiver.
- *
- * @param name
- * A String, the name of the field to set
- * @param value
- * New value for the field.
- *
- * @throws IllegalArgumentException
- * If the corresponding field can not be found.
- */
- public void put(String name, int value) throws IllegalArgumentException {
- ObjectSlot slot = findSlot(name, Integer.TYPE);
- if (slot != null) {
- slot.fieldValue = new Integer(value);
- slot.defaulted = false; // No longer default value
- } else
- throw new IllegalArgumentException();
- }
-
- /**
- * Find and set the long value of a given field named <code>name</code> in
- * the receiver.
- *
- * @param name
- * A String, the name of the field to set
- * @param value
- * New value for the field.
- *
- * @throws IllegalArgumentException
- * If the corresponding field can not be found.
- */
- public void put(String name, long value) throws IllegalArgumentException {
- ObjectSlot slot = findSlot(name, Long.TYPE);
- if (slot != null) {
- slot.fieldValue = new Long(value);
- slot.defaulted = false; // No longer default value
- } else
- throw new IllegalArgumentException();
- }
-
- /**
- * Find and set the Object value of a given field named <code>name</code>
- * in the receiver.
- *
- * @param name
- * A String, the name of the field to set
- * @param value
- * New value for the field.
- *
- * @throws IllegalArgumentException
- * If the corresponding field can not be found.
- */
- public void put(String name, Object value) throws IllegalArgumentException {
- Class<?> valueClass = null;
- if (value != null)
- valueClass = value.getClass();
- ObjectSlot slot = findSlot(name, valueClass);
- if (slot != null) {
- slot.fieldValue = value;
- slot.defaulted = false; // No longer default value
- } else
- throw new IllegalArgumentException();
- }
-
- /**
- * Find and set the short value of a given field named <code>name</code>
- * in the receiver.
- *
- * @param name
- * A String, the name of the field to set
- * @param value
- * New value for the field.
- *
- * @throws IllegalArgumentException
- * If the corresponding field can not be found.
- */
- public void put(String name, short value) throws IllegalArgumentException {
- ObjectSlot slot = findSlot(name, Short.TYPE);
- if (slot != null) {
- slot.fieldValue = new Short(value);
- slot.defaulted = false; // No longer default value
- } else
- throw new IllegalArgumentException();
- }
-
- /**
- * Find and set the boolean value of a given field named <code>name</code>
- * in the receiver.
- *
- * @param name
- * A String, the name of the field to set
- * @param value
- * New value for the field.
- *
- * @throws IllegalArgumentException
- * If the corresponding field can not be found.
- */
- public void put(String name, boolean value) throws IllegalArgumentException {
- ObjectSlot slot = findSlot(name, Boolean.TYPE);
- if (slot != null) {
- slot.fieldValue = new Boolean(value);
- slot.defaulted = false; // No longer default value
- } else
- throw new IllegalArgumentException();
- }
-
- /**
- * Return the array of ObjectSlot the receiver represents.
- *
- * @return array of ObjectSlot the receiver represents.
- */
- public ObjectSlot[] slots() {
- return slotsToSerialize;
- }
-}
+/* Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.io;
+
+
+/**
+ * An EmulatedFields is an object that represents a set of emulated fields for
+ * an object being dumped or loaded. It allows objects to be dumped with a shape
+ * different than the fields they were declared to have.
+ *
+ * @see ObjectInputStream.GetField
+ * @see ObjectOutputStream.PutField
+ * @see EmulatedFieldsForLoading
+ * @see EmulatedFieldsForDumping
+ */
+
+class EmulatedFields {
+ static class ObjectSlot {
+ // A slot is a field plus its value
+ ObjectStreamField field; // Field descriptor
+
+ Object fieldValue; // Actual value this emulated field holds
+
+ boolean defaulted = true; // If this field has a default value (true)
+ // or something has been assigned (false)
+
+ /**
+ * Returns the descriptor for this emulated field.
+ *
+ * @return the field descriptor
+ */
+ public ObjectStreamField getField() {
+ return field;
+ }
+
+ /**
+ * Returns the value held by this emulated field.
+ *
+ * @return the field value
+ */
+ public Object getFieldValue() {
+ return fieldValue;
+ }
+ }
+
+ private ObjectSlot[] slotsToSerialize; // The collection of slots the
+ // receiver represents
+
+ private ObjectStreamField[] declaredFields;
+
+ /**
+ * Constructs a new instance of EmulatedFields.
+ *
+ * @param fields
+ * an array of ObjectStreamFields, which describe the fields to
+ * be emulated (names, types, etc).
+ * @param declared
+ * an array of ObjectStreamFields, which describe the declared
+ * fields.
+ */
+ public EmulatedFields(ObjectStreamField[] fields,
+ ObjectStreamField[] declared) {
+ super();
+ // We assume the slots are already sorted in the right shape for dumping
+ buildSlots(fields);
+ declaredFields = declared;
+ }
+
+ /**
+ * Build emulated slots that correspond to emulated fields. A slot is a
+ * field descriptor (ObjectStreamField) plus the actual value it holds.
+ *
+ * @param fields
+ * an array of ObjectStreamField, which describe the fields to be
+ * emulated (names, types, etc).
+ */
+ private void buildSlots(ObjectStreamField[] fields) {
+ slotsToSerialize = new ObjectSlot[fields.length];
+ for (int i = 0; i < fields.length; i++) {
+ ObjectSlot s = new ObjectSlot();
+ slotsToSerialize[i] = s;
+ s.field = fields[i];
+ }
+ // We assume the slots are already sorted in the right shape for dumping
+ }
+
+ /**
+ * Return a boolean indicating if the field named <code>name</code> has
+ * been assigned a value explicitly (false) or if it still holds a default
+ * value for the type (true) because it hasn't been assigned to yet.
+ *
+ * @param name
+ * a String, the name of the field to test
+ * @return <code>true</code> if <code>name</code> still holds its
+ * default value, <code>false</code> otherwise
+ *
+ * @throws IllegalArgumentException
+ * If <code>name</code> is null
+ */
+ public boolean defaulted(String name) throws IllegalArgumentException {
+ ObjectSlot slot = findSlot(name, null);
+ if (slot != null)
+ return slot.defaulted;
+ throw new IllegalArgumentException();
+ }
+
+ /**
+ * Find and return an ObjectSlot that corresponds to a field named
+ * <code>fieldName</code> and type <code>fieldType</code>. If the field
+ * type <code>fieldType</code> corresponds to a primitive type, the field
+ * type has to match exactly or <code>null</code> is returned. If the
+ * field type <code>fieldType</code> corresponds to an object type, the
+ * field type has to be compatible in terms of assignment, or null is
+ * returned. If <code>fieldType</code> is <code>null</code>, no such
+ * compatibility checking is performed and the slot is returned.
+ *
+ * @param fieldName
+ * A String, the name of the field to find
+ * @param fieldType
+ * A Class, the type of the field. This will be used to test
+ * compatibility. If null, no testing is done, the corresponding
+ * slot is returned.
+ * @return If there is no field with that name, or no compatible field
+ * (relative to <code>fieldType</code>)
+ */
+ private ObjectSlot findSlot(String fieldName, Class<?> fieldType) {
+ boolean isPrimitive = fieldType != null && fieldType.isPrimitive();
+
+ for (int i = 0; i < slotsToSerialize.length; i++) {
+ ObjectSlot slot = slotsToSerialize[i];
+ if (slot.field.getName().equals(fieldName))
+ if (isPrimitive) {
+ // Looking for a primitive type field. Types must match
+ // *exactly*
+ if (slot.field.getType() == fieldType)
+ return slot;
+ } else {
+ // Looking for a non-primitive type field.
+ if (fieldType == null)
+ return slot; // Null means we take anything
+ // Types must be compatible (assignment)
+ if (slot.field.getType().isAssignableFrom(fieldType))
+ return slot;
+ }
+ }
+
+ if (declaredFields != null) {
+ for (int i = 0; i < declaredFields.length; i++) {
+ ObjectStreamField field = declaredFields[i];
+ if (field.getName().equals(fieldName)) {
+ if (isPrimitive ? field.getType() == fieldType
+ : fieldType == null
+ || field.getType().isAssignableFrom(
+ fieldType)) {
+ ObjectSlot slot = new ObjectSlot();
+ slot.field = field;
+ slot.defaulted = true;
+ return slot;
+ }
+ }
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Find and return the byte value of a given field named <code>name</code>
+ * in the receiver. If the field has not been assigned any value yet, the
+ * default value <code>defaultValue</code> is returned instead.
+ *
+ * @param name
+ * A String, the name of the field to find
+ * @param defaultValue
+ * Return value in case the field has not been assigned to yet.
+ * @return the value of the given field if it has been assigned, the default
+ * value otherwise
+ *
+ * @throws IllegalArgumentException
+ * If the corresponding field can not be found.
+ */
+ public byte get(String name, byte defaultValue)
+ throws IllegalArgumentException {
+ ObjectSlot slot = findSlot(name, Byte.TYPE);
+ if (slot != null) // if not initialized yet, we give the default value
+ return slot.defaulted ? defaultValue : ((Byte) slot.fieldValue)
+ .byteValue();
+ throw new IllegalArgumentException();
+ }
+
+ /**
+ * Find and return the char value of a given field named <code>name</code>
+ * in the receiver. If the field has not been assigned any value yet, the
+ * default value <code>defaultValue</code> is returned instead.
+ *
+ * @param name
+ * A String, the name of the field to find
+ * @param defaultValue
+ * Return value in case the field has not been assigned to yet.
+ * @return the value of the given field if it has been assigned, the default
+ * value otherwise
+ *
+ * @throws IllegalArgumentException
+ * If the corresponding field can not be found.
+ */
+ public char get(String name, char defaultValue)
+ throws IllegalArgumentException {
+ ObjectSlot slot = findSlot(name, Character.TYPE);
+ if (slot != null) // if not initialized yet, we give the default value
+ return slot.defaulted ? defaultValue
+ : ((Character) slot.fieldValue).charValue();
+ throw new IllegalArgumentException();
+ }
+
+ /**
+ * Find and return the double value of a given field named <code>name</code>
+ * in the receiver. If the field has not been assigned any value yet, the
+ * default value <code>defaultValue</code> is returned instead.
+ *
+ * @param name
+ * A String, the name of the field to find
+ * @param defaultValue
+ * Return value in case the field has not been assigned to yet.
+ * @return the value of the given field if it has been assigned, the default
+ * value otherwise
+ *
+ * @throws IllegalArgumentException
+ * If the corresponding field can not be found.
+ */
+ public double get(String name, double defaultValue)
+ throws IllegalArgumentException {
+ ObjectSlot slot = findSlot(name, Double.TYPE);
+ if (slot != null) // if not initialized yet, we give the default value
+ return slot.defaulted ? defaultValue : ((Double) slot.fieldValue)
+ .doubleValue();
+ throw new IllegalArgumentException();
+ }
+
+ /**
+ * Find and return the float value of a given field named <code>name</code>
+ * in the receiver. If the field has not been assigned any value yet, the
+ * default value <code>defaultValue</code> is returned instead.
+ *
+ * @param name
+ * A String, the name of the field to find
+ * @param defaultValue
+ * Return value in case the field has not been assigned to yet.
+ * @return the value of the given field if it has been assigned, the default
+ * value otherwise
+ *
+ * @throws IllegalArgumentException
+ * If the corresponding field can not be found.
+ */
+ public float get(String name, float defaultValue)
+ throws IllegalArgumentException {
+ ObjectSlot slot = findSlot(name, Float.TYPE);
+ if (slot != null) // if not initialized yet, we give the default value
+ return slot.defaulted ? defaultValue : ((Float) slot.fieldValue)
+ .floatValue();
+ throw new IllegalArgumentException();
+ }
+
+ /**
+ * Find and return the int value of a given field named <code>name</code>
+ * in the receiver. If the field has not been assigned any value yet, the
+ * default value <code>defaultValue</code> is returned instead.
+ *
+ * @param name
+ * A String, the name of the field to find
+ * @param defaultValue
+ * Return value in case the field has not been assigned to yet.
+ * @return the value of the given field if it has been assigned, the default
+ * value otherwise
+ *
+ * @throws IllegalArgumentException
+ * If the corresponding field can not be found.
+ */
+ public int get(String name, int defaultValue)
+ throws IllegalArgumentException {
+ ObjectSlot slot = findSlot(name, Integer.TYPE);
+ if (slot != null) // if not initialized yet, we give the default value
+ return slot.defaulted ? defaultValue : ((Integer) slot.fieldValue)
+ .intValue();
+ throw new IllegalArgumentException();
+ }
+
+ /**
+ * Find and return the long value of a given field named <code>name</code>
+ * in the receiver. If the field has not been assigned any value yet, the
+ * default value <code>defaultValue</code> is returned instead.
+ *
+ * @param name
+ * A String, the name of the field to find
+ * @param defaultValue
+ * Return value in case the field has not been assigned to yet.
+ * @return the value of the given field if it has been assigned, the default
+ * value otherwise
+ *
+ * @throws IllegalArgumentException
+ * If the corresponding field can not be found.
+ */
+ public long get(String name, long defaultValue)
+ throws IllegalArgumentException {
+ ObjectSlot slot = findSlot(name, Long.TYPE);
+ if (slot != null) // if not initialized yet, we give the default value
+ return slot.defaulted ? defaultValue : ((Long) slot.fieldValue)
+ .longValue();
+ throw new IllegalArgumentException();
+ }
+
+ /**
+ * Find and return the Object value of a given field named <code>name</code>
+ * in the receiver. If the field has not been assigned any value yet, the
+ * default value <code>defaultValue</code> is returned instead.
+ *
+ * @param name
+ * A String, the name of the field to find
+ * @param defaultValue
+ * Return value in case the field has not been assigned to yet.
+ * @return the value of the given field if it has been assigned, the default
+ * value otherwise
+ *
+ * @throws IllegalArgumentException
+ * If the corresponding field can not be found.
+ */
+ public Object get(String name, Object defaultValue)
+ throws IllegalArgumentException {
+ ObjectSlot slot = findSlot(name, null);
+ if (slot != null && !slot.field.getType().isPrimitive()) // if not
+ // initialized
+ // yet, we
+ // give the
+ // default
+ // value
+ return slot.defaulted ? defaultValue : slot.fieldValue;
+ throw new IllegalArgumentException();
+ }
+
+ /**
+ * Find and return the short value of a given field named <code>name</code>
+ * in the receiver. If the field has not been assigned any value yet, the
+ * default value <code>defaultValue</code> is returned instead.
+ *
+ * @param name
+ * A String, the name of the field to find
+ * @param defaultValue
+ * Return value in case the field has not been assigned to yet.
+ * @return the value of the given field if it has been assigned, the default
+ * value otherwise
+ *
+ * @throws IllegalArgumentException
+ * If the corresponding field can not be found.
+ */
+ public short get(String name, short defaultValue)
+ throws IllegalArgumentException {
+ ObjectSlot slot = findSlot(name, Short.TYPE);
+ if (slot != null) // if not initialized yet, we give the default value
+ return slot.defaulted ? defaultValue : ((Short) slot.fieldValue)
+ .shortValue();
+ throw new IllegalArgumentException();
+ }
+
+ /**
+ * Find and return the boolean value of a given field named
+ * <code>name</code> in the receiver. If the field has not been assigned
+ * any value yet, the default value <code>defaultValue</code> is returned
+ * instead.
+ *
+ * @param name
+ * A String, the name of the field to find
+ * @param defaultValue
+ * Return value in case the field has not been assigned to yet.
+ * @return the value of the given field if it has been assigned, the default
+ * value otherwise
+ *
+ * @throws IllegalArgumentException
+ * If the corresponding field can not be found.
+ */
+ public boolean get(String name, boolean defaultValue)
+ throws IllegalArgumentException {
+ ObjectSlot slot = findSlot(name, Boolean.TYPE);
+ if (slot != null) // if not initialized yet, we give the default value
+ return slot.defaulted ? defaultValue : ((Boolean) slot.fieldValue)
+ .booleanValue();
+ throw new IllegalArgumentException();
+ }
+
+ /**
+ * Find and set the byte value of a given field named <code>name</code> in
+ * the receiver.
+ *
+ * @param name
+ * A String, the name of the field to set
+ * @param value
+ * New value for the field.
+ *
+ * @throws IllegalArgumentException
+ * If the corresponding field can not be found.
+ */
+ public void put(String name, byte value) throws IllegalArgumentException {
+ ObjectSlot slot = findSlot(name, Byte.TYPE);
+ if (slot != null) {
+ slot.fieldValue = new Byte(value);
+ slot.defaulted = false; // No longer default value
+ } else
+ throw new IllegalArgumentException();
+ }
+
+ /**
+ * Find and set the char value of a given field named <code>name</code> in
+ * the receiver.
+ *
+ * @param name
+ * A String, the name of the field to set
+ * @param value
+ * New value for the field.
+ *
+ * @throws IllegalArgumentException
+ * If the corresponding field can not be found.
+ */
+ public void put(String name, char value) throws IllegalArgumentException {
+ ObjectSlot slot = findSlot(name, Character.TYPE);
+ if (slot != null) {
+ slot.fieldValue = new Character(value);
+ slot.defaulted = false; // No longer default value
+ } else
+ throw new IllegalArgumentException();
+ }
+
+ /**
+ * Find and set the double value of a given field named <code>name</code>
+ * in the receiver.
+ *
+ * @param name
+ * A String, the name of the field to set
+ * @param value
+ * New value for the field.
+ *
+ * @throws IllegalArgumentException
+ * If the corresponding field can not be found.
+ */
+ public void put(String name, double value) throws IllegalArgumentException {
+ ObjectSlot slot = findSlot(name, Double.TYPE);
+ if (slot != null) {
+ slot.fieldValue = new Double(value);
+ slot.defaulted = false; // No longer default value
+ } else
+ throw new IllegalArgumentException();
+ }
+
+ /**
+ * Find and set the float value of a given field named <code>name</code>
+ * in the receiver.
+ *
+ * @param name
+ * A String, the name of the field to set
+ * @param value
+ * New value for the field.
+ *
+ * @throws IllegalArgumentException
+ * If the corresponding field can not be found.
+ */
+ public void put(String name, float value) throws IllegalArgumentException {
+ ObjectSlot slot = findSlot(name, Float.TYPE);
+ if (slot != null) {
+ slot.fieldValue = new Float(value);
+ slot.defaulted = false; // No longer default value
+ } else
+ throw new IllegalArgumentException();
+ }
+
+ /**
+ * Find and set the int value of a given field named <code>name</code> in
+ * the receiver.
+ *
+ * @param name
+ * A String, the name of the field to set
+ * @param value
+ * New value for the field.
+ *
+ * @throws IllegalArgumentException
+ * If the corresponding field can not be found.
+ */
+ public void put(String name, int value) throws IllegalArgumentException {
+ ObjectSlot slot = findSlot(name, Integer.TYPE);
+ if (slot != null) {
+ slot.fieldValue = new Integer(value);
+ slot.defaulted = false; // No longer default value
+ } else
+ throw new IllegalArgumentException();
+ }
+
+ /**
+ * Find and set the long value of a given field named <code>name</code> in
+ * the receiver.
+ *
+ * @param name
+ * A String, the name of the field to set
+ * @param value
+ * New value for the field.
+ *
+ * @throws IllegalArgumentException
+ * If the corresponding field can not be found.
+ */
+ public void put(String name, long value) throws IllegalArgumentException {
+ ObjectSlot slot = findSlot(name, Long.TYPE);
+ if (slot != null) {
+ slot.fieldValue = new Long(value);
+ slot.defaulted = false; // No longer default value
+ } else
+ throw new IllegalArgumentException();
+ }
+
+ /**
+ * Find and set the Object value of a given field named <code>name</code>
+ * in the receiver.
+ *
+ * @param name
+ * A String, the name of the field to set
+ * @param value
+ * New value for the field.
+ *
+ * @throws IllegalArgumentException
+ * If the corresponding field can not be found.
+ */
+ public void put(String name, Object value) throws IllegalArgumentException {
+ Class<?> valueClass = null;
+ if (value != null)
+ valueClass = value.getClass();
+ ObjectSlot slot = findSlot(name, valueClass);
+ if (slot != null) {
+ slot.fieldValue = value;
+ slot.defaulted = false; // No longer default value
+ } else
+ throw new IllegalArgumentException();
+ }
+
+ /**
+ * Find and set the short value of a given field named <code>name</code>
+ * in the receiver.
+ *
+ * @param name
+ * A String, the name of the field to set
+ * @param value
+ * New value for the field.
+ *
+ * @throws IllegalArgumentException
+ * If the corresponding field can not be found.
+ */
+ public void put(String name, short value) throws IllegalArgumentException {
+ ObjectSlot slot = findSlot(name, Short.TYPE);
+ if (slot != null) {
+ slot.fieldValue = new Short(value);
+ slot.defaulted = false; // No longer default value
+ } else
+ throw new IllegalArgumentException();
+ }
+
+ /**
+ * Find and set the boolean value of a given field named <code>name</code>
+ * in the receiver.
+ *
+ * @param name
+ * A String, the name of the field to set
+ * @param value
+ * New value for the field.
+ *
+ * @throws IllegalArgumentException
+ * If the corresponding field can not be found.
+ */
+ public void put(String name, boolean value) throws IllegalArgumentException {
+ ObjectSlot slot = findSlot(name, Boolean.TYPE);
+ if (slot != null) {
+ slot.fieldValue = new Boolean(value);
+ slot.defaulted = false; // No longer default value
+ } else
+ throw new IllegalArgumentException();
+ }
+
+ /**
+ * Return the array of ObjectSlot the receiver represents.
+ *
+ * @return array of ObjectSlot the receiver represents.
+ */
+ public ObjectSlot[] slots() {
+ return slotsToSerialize;
+ }
+}
Propchange: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/EmulatedFields.java
------------------------------------------------------------------------------
svn:eol-style = native