FileDocCategorySizeDatePackage
DataOutputStream.javaAPI DocAndroid 1.5 API16398Wed May 06 22:41:04 BST 2009java.io

DataOutputStream

public class DataOutputStream extends FilterOutputStream implements DataOutput
Wraps an existing {@link OutputStream} and writes typed data to it. Typically, this stream can be read in by 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 {@link DataInput MUTF-8} encoded strings.
see
DataInputStream
since
Android 1.0

Fields Summary
protected int
written
The number of bytes written out so far.
static final int
MAX_BUF_SIZE
Constructors Summary
public DataOutputStream(OutputStream out)
Constructs a new {@code DataOutputStream} on the {@code OutputStream} {@code out}. Note that data written by this stream is not in a human readable form but can be reconstructed by using a {@link DataInputStream} on the resulting output.

param
out the target stream for writing.
since
Android 1.0

        super(out);
    
Methods Summary
longcountUTFBytes(java.lang.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;
    
public voidflush()
Flushes this stream to ensure all pending data is sent out to the target stream. This implementation then also flushes the target stream.

throws
IOException if an error occurs attempting to flush this stream.
since
Android 1.0

        super.flush();
    
public final intsize()
Returns the total number of bytes written to the target stream so far.

return
the number of bytes written to the target stream.
since
Android 1.0

        if (written < 0) {
            written = Integer.MAX_VALUE;
        }
        return written;
    
public voidwrite(byte[] buffer, int offset, int count)
Writes {@code count} bytes from the byte array {@code buffer} starting at {@code offset} to the target stream.

param
buffer the buffer to write to the target stream.
param
offset the index of the first byte in {@code buffer} to write.
param
count the number of bytes from the {@code buffer} to write.
throws
IOException if an error occurs while writing to the target stream.
throws
NullPointerException if {@code buffer} is {@code null}.
see
DataInputStream#readFully(byte[])
see
DataInputStream#readFully(byte[], int, int)
since
Android 1.0

        // BEGIN android-note
        // changed array notation to be consistent with the rest of harmony
        // END android-note
        if (buffer == null) {
            throw new NullPointerException(Msg.getString("K0047")); //$NON-NLS-1$
        }
        out.write(buffer, offset, count);
        written += count;
    
public voidwrite(int oneByte)
Writes a byte to the target stream. Only the least significant byte of the integer {@code oneByte} is written.

param
oneByte the byte to write to the target stream.
throws
IOException if an error occurs while writing to the target stream.
see
DataInputStream#readByte()
since
Android 1.0

        out.write(oneByte);
        written++;
    
public final voidwriteBoolean(boolean val)
Writes a boolean to the target stream.

param
val the boolean value to write to the target stream.
throws
IOException if an error occurs while writing to the target stream.
see
DataInputStream#readBoolean()
since
Android 1.0

        out.write(val ? 1 : 0);
        written++;
    
public final voidwriteByte(int val)
Writes an 8-bit byte to the target stream. Only the least significant byte of the integer {@code val} is written.

param
val the byte value to write to the target stream.
throws
IOException if an error occurs while writing to the target stream.
see
DataInputStream#readByte()
see
DataInputStream#readUnsignedByte()
since
Android 1.0

        out.write(val);
        written++;
    
public final voidwriteBytes(java.lang.String str)
Writes the low order bytes from a string to the target stream.

param
str the string containing the bytes to write to the target stream.
throws
IOException if an error occurs while writing to the target stream.
see
DataInputStream#readFully(byte[])
see
DataInputStream#readFully(byte[],int,int)
since
Android 1.0

        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;
    
public final voidwriteChar(int val)
Writes a 16-bit character to the target stream. Only the two lower bytes of the integer {@code val} are written, with the higher one written first. This corresponds to the Unicode value of {@code val}.

param
val the character to write to the target stream
throws
IOException if an error occurs while writing to the target stream.
see
DataInputStream#readChar()
since
Android 1.0

        out.write(val >> 8);
        out.write(val);
        written += 2;
    
public final voidwriteChars(java.lang.String str)
Writes the 16-bit characters contained in {@code str} to the target stream.

param
str the string that contains the characters to write to this stream.
throws
IOException if an error occurs while writing to the target stream.
see
DataInputStream#readChar()
since
Android 1.0

        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;
    
public final voidwriteDouble(double val)
Writes a 64-bit double to the target stream. The resulting output is the eight bytes resulting from calling Double.doubleToLongBits().

param
val the double to write to the target stream.
throws
IOException if an error occurs while writing to the target stream.
see
DataInputStream#readDouble()
since
Android 1.0

        writeLong(Double.doubleToLongBits(val));
    
public final voidwriteFloat(float val)
Writes a 32-bit float to the target stream. The resulting output is the four bytes resulting from calling Float.floatToIntBits().

param
val the float to write to the target stream.
throws
IOException if an error occurs while writing to the target stream.
see
DataInputStream#readFloat()
since
Android 1.0

        writeInt(Float.floatToIntBits(val));
    
public final voidwriteInt(int val)
Writes a 32-bit int to the target stream. The resulting output is the four bytes, highest order first, of {@code val}.

param
val the int to write to the target stream.
throws
IOException if an error occurs while writing to the target stream.
see
DataInputStream#readInt()
since
Android 1.0

        out.write(val >> 24);
        out.write(val >> 16);
        out.write(val >> 8);
        out.write(val);
        written += 4;
    
public final voidwriteLong(long val)
Writes a 64-bit long to the target stream. The resulting output is the eight bytes, highest order first, of {@code val}.

param
val the long to write to the target stream.
throws
IOException if an error occurs while writing to the target stream.
see
DataInputStream#readLong()
since
Android 1.0

        writeInt((int) (val >> 32));
        writeInt((int) val);
    
public final voidwriteShort(int val)
Writes the specified 16-bit short to the target stream. Only the lower two bytes of the integer {@code val} are written, with the higher one written first.

param
val the short to write to the target stream.
throws
IOException if an error occurs while writing to the target stream.
see
DataInputStream#readShort()
see
DataInputStream#readUnsignedShort()
since
Android 1.0

        writeChar(val);
    
public final voidwriteUTF(java.lang.String str)
Writes the specified encoded in {@link DataInput modified UTF-8} to this stream.

param
str the string to write to the target stream encoded in {@link DataInput modified UTF-8}.
throws
IOException if an error occurs while writing to the target stream.
throws
UTFDataFormatException if the encoded string is longer than 65535 bytes.
see
DataInputStream#readUTF()
since
Android 1.0

    // END android-added
    
                                                                                                                
           
        int length = str.length();
        // BEGIN android-changed
        if (length <= MAX_BUF_SIZE / 3) {
            int size = length * 3;
            byte[] utfBytes = new byte[size];
            // 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$
            }
        }
        // END android-changed
    
voidwriteUTFBytes(java.lang.String str, long count)

        boolean single = true;
        int size = (int) count;
        // BEGIN android-changed
        if (count > MAX_BUF_SIZE) {
            single = false;
            size = MAX_BUF_SIZE;
        }
        byte[] utfBytes = new byte[size];
        // END android-changed
        // BEGIN android-removed
        // 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];
        //     }
        // }
        // END android-removed

        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);
        }
        // BEGIN android-removed
        // if (!makeBuf) {
        //     // Update the useShared flag optimistically (see DataInputStream
        //     // equivalent)
        //     DataInputStream.useShared = true;
        // }
        // END android-removed