0N/A/*
2362N/A * Copyright (c) 1994, 2004, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.io;
0N/A
0N/A/**
0N/A * A data output stream lets an application write primitive Java data
0N/A * types to an output stream in a portable way. An application can
0N/A * then use a data input stream to read the data back in.
0N/A *
0N/A * @author unascribed
0N/A * @see java.io.DataInputStream
0N/A * @since JDK1.0
0N/A */
0N/Apublic
0N/Aclass DataOutputStream extends FilterOutputStream implements DataOutput {
0N/A /**
0N/A * The number of bytes written to the data output stream so far.
0N/A * If this counter overflows, it will be wrapped to Integer.MAX_VALUE.
0N/A */
0N/A protected int written;
0N/A
0N/A /**
0N/A * bytearr is initialized on demand by writeUTF
0N/A */
0N/A private byte[] bytearr = null;
0N/A
0N/A /**
0N/A * Creates a new data output stream to write data to the specified
0N/A * underlying output stream. The counter <code>written</code> is
0N/A * set to zero.
0N/A *
0N/A * @param out the underlying output stream, to be saved for later
0N/A * use.
0N/A * @see java.io.FilterOutputStream#out
0N/A */
0N/A public DataOutputStream(OutputStream out) {
0N/A super(out);
0N/A }
0N/A
0N/A /**
0N/A * Increases the written counter by the specified value
0N/A * until it reaches Integer.MAX_VALUE.
0N/A */
0N/A private void incCount(int value) {
0N/A int temp = written + value;
0N/A if (temp < 0) {
0N/A temp = Integer.MAX_VALUE;
0N/A }
0N/A written = temp;
0N/A }
0N/A
0N/A /**
0N/A * Writes the specified byte (the low eight bits of the argument
0N/A * <code>b</code>) to the underlying output stream. If no exception
0N/A * is thrown, the counter <code>written</code> is incremented by
0N/A * <code>1</code>.
0N/A * <p>
0N/A * Implements the <code>write</code> method of <code>OutputStream</code>.
0N/A *
0N/A * @param b the <code>byte</code> to be written.
0N/A * @exception IOException if an I/O error occurs.
0N/A * @see java.io.FilterOutputStream#out
0N/A */
0N/A public synchronized void write(int b) throws IOException {
0N/A out.write(b);
0N/A incCount(1);
0N/A }
0N/A
0N/A /**
0N/A * Writes <code>len</code> bytes from the specified byte array
0N/A * starting at offset <code>off</code> to the underlying output stream.
0N/A * If no exception is thrown, the counter <code>written</code> is
0N/A * incremented by <code>len</code>.
0N/A *
0N/A * @param b the data.
0N/A * @param off the start offset in the data.
0N/A * @param len the number of bytes to write.
0N/A * @exception IOException if an I/O error occurs.
0N/A * @see java.io.FilterOutputStream#out
0N/A */
0N/A public synchronized void write(byte b[], int off, int len)
0N/A throws IOException
0N/A {
0N/A out.write(b, off, len);
0N/A incCount(len);
0N/A }
0N/A
0N/A /**
0N/A * Flushes this data output stream. This forces any buffered output
0N/A * bytes to be written out to the stream.
0N/A * <p>
0N/A * The <code>flush</code> method of <code>DataOutputStream</code>
0N/A * calls the <code>flush</code> method of its underlying output stream.
0N/A *
0N/A * @exception IOException if an I/O error occurs.
0N/A * @see java.io.FilterOutputStream#out
0N/A * @see java.io.OutputStream#flush()
0N/A */
0N/A public void flush() throws IOException {
0N/A out.flush();
0N/A }
0N/A
0N/A /**
0N/A * Writes a <code>boolean</code> to the underlying output stream as
0N/A * a 1-byte value. The value <code>true</code> is written out as the
0N/A * value <code>(byte)1</code>; the value <code>false</code> is
0N/A * written out as the value <code>(byte)0</code>. If no exception is
0N/A * thrown, the counter <code>written</code> is incremented by
0N/A * <code>1</code>.
0N/A *
0N/A * @param v a <code>boolean</code> value to be written.
0N/A * @exception IOException if an I/O error occurs.
0N/A * @see java.io.FilterOutputStream#out
0N/A */
0N/A public final void writeBoolean(boolean v) throws IOException {
0N/A out.write(v ? 1 : 0);
0N/A incCount(1);
0N/A }
0N/A
0N/A /**
0N/A * Writes out a <code>byte</code> to the underlying output stream as
0N/A * a 1-byte value. If no exception is thrown, the counter
0N/A * <code>written</code> is incremented by <code>1</code>.
0N/A *
0N/A * @param v a <code>byte</code> value to be written.
0N/A * @exception IOException if an I/O error occurs.
0N/A * @see java.io.FilterOutputStream#out
0N/A */
0N/A public final void writeByte(int v) throws IOException {
0N/A out.write(v);
0N/A incCount(1);
0N/A }
0N/A
0N/A /**
0N/A * Writes a <code>short</code> to the underlying output stream as two
0N/A * bytes, high byte first. If no exception is thrown, the counter
0N/A * <code>written</code> is incremented by <code>2</code>.
0N/A *
0N/A * @param v a <code>short</code> to be written.
0N/A * @exception IOException if an I/O error occurs.
0N/A * @see java.io.FilterOutputStream#out
0N/A */
0N/A public final void writeShort(int v) throws IOException {
0N/A out.write((v >>> 8) & 0xFF);
0N/A out.write((v >>> 0) & 0xFF);
0N/A incCount(2);
0N/A }
0N/A
0N/A /**
0N/A * Writes a <code>char</code> to the underlying output stream as a
0N/A * 2-byte value, high byte first. If no exception is thrown, the
0N/A * counter <code>written</code> is incremented by <code>2</code>.
0N/A *
0N/A * @param v a <code>char</code> value to be written.
0N/A * @exception IOException if an I/O error occurs.
0N/A * @see java.io.FilterOutputStream#out
0N/A */
0N/A public final void writeChar(int v) throws IOException {
0N/A out.write((v >>> 8) & 0xFF);
0N/A out.write((v >>> 0) & 0xFF);
0N/A incCount(2);
0N/A }
0N/A
0N/A /**
0N/A * Writes an <code>int</code> to the underlying output stream as four
0N/A * bytes, high byte first. If no exception is thrown, the counter
0N/A * <code>written</code> is incremented by <code>4</code>.
0N/A *
0N/A * @param v an <code>int</code> to be written.
0N/A * @exception IOException if an I/O error occurs.
0N/A * @see java.io.FilterOutputStream#out
0N/A */
0N/A public final void writeInt(int v) throws IOException {
0N/A out.write((v >>> 24) & 0xFF);
0N/A out.write((v >>> 16) & 0xFF);
0N/A out.write((v >>> 8) & 0xFF);
0N/A out.write((v >>> 0) & 0xFF);
0N/A incCount(4);
0N/A }
0N/A
0N/A private byte writeBuffer[] = new byte[8];
0N/A
0N/A /**
0N/A * Writes a <code>long</code> to the underlying output stream as eight
0N/A * bytes, high byte first. In no exception is thrown, the counter
0N/A * <code>written</code> is incremented by <code>8</code>.
0N/A *
0N/A * @param v a <code>long</code> to be written.
0N/A * @exception IOException if an I/O error occurs.
0N/A * @see java.io.FilterOutputStream#out
0N/A */
0N/A public final void writeLong(long v) throws IOException {
0N/A writeBuffer[0] = (byte)(v >>> 56);
0N/A writeBuffer[1] = (byte)(v >>> 48);
0N/A writeBuffer[2] = (byte)(v >>> 40);
0N/A writeBuffer[3] = (byte)(v >>> 32);
0N/A writeBuffer[4] = (byte)(v >>> 24);
0N/A writeBuffer[5] = (byte)(v >>> 16);
0N/A writeBuffer[6] = (byte)(v >>> 8);
0N/A writeBuffer[7] = (byte)(v >>> 0);
0N/A out.write(writeBuffer, 0, 8);
0N/A incCount(8);
0N/A }
0N/A
0N/A /**
0N/A * Converts the float argument to an <code>int</code> using the
0N/A * <code>floatToIntBits</code> method in class <code>Float</code>,
0N/A * and then writes that <code>int</code> value to the underlying
0N/A * output stream as a 4-byte quantity, high byte first. If no
0N/A * exception is thrown, the counter <code>written</code> is
0N/A * incremented by <code>4</code>.
0N/A *
0N/A * @param v a <code>float</code> value to be written.
0N/A * @exception IOException if an I/O error occurs.
0N/A * @see java.io.FilterOutputStream#out
0N/A * @see java.lang.Float#floatToIntBits(float)
0N/A */
0N/A public final void writeFloat(float v) throws IOException {
0N/A writeInt(Float.floatToIntBits(v));
0N/A }
0N/A
0N/A /**
0N/A * Converts the double argument to a <code>long</code> using the
0N/A * <code>doubleToLongBits</code> method in class <code>Double</code>,
0N/A * and then writes that <code>long</code> value to the underlying
0N/A * output stream as an 8-byte quantity, high byte first. If no
0N/A * exception is thrown, the counter <code>written</code> is
0N/A * incremented by <code>8</code>.
0N/A *
0N/A * @param v a <code>double</code> value to be written.
0N/A * @exception IOException if an I/O error occurs.
0N/A * @see java.io.FilterOutputStream#out
0N/A * @see java.lang.Double#doubleToLongBits(double)
0N/A */
0N/A public final void writeDouble(double v) throws IOException {
0N/A writeLong(Double.doubleToLongBits(v));
0N/A }
0N/A
0N/A /**
0N/A * Writes out the string to the underlying output stream as a
0N/A * sequence of bytes. Each character in the string is written out, in
0N/A * sequence, by discarding its high eight bits. If no exception is
0N/A * thrown, the counter <code>written</code> is incremented by the
0N/A * length of <code>s</code>.
0N/A *
0N/A * @param s a string of bytes to be written.
0N/A * @exception IOException if an I/O error occurs.
0N/A * @see java.io.FilterOutputStream#out
0N/A */
0N/A public final void writeBytes(String s) throws IOException {
0N/A int len = s.length();
0N/A for (int i = 0 ; i < len ; i++) {
0N/A out.write((byte)s.charAt(i));
0N/A }
0N/A incCount(len);
0N/A }
0N/A
0N/A /**
0N/A * Writes a string to the underlying output stream as a sequence of
0N/A * characters. Each character is written to the data output stream as
0N/A * if by the <code>writeChar</code> method. If no exception is
0N/A * thrown, the counter <code>written</code> is incremented by twice
0N/A * the length of <code>s</code>.
0N/A *
0N/A * @param s a <code>String</code> value to be written.
0N/A * @exception IOException if an I/O error occurs.
0N/A * @see java.io.DataOutputStream#writeChar(int)
0N/A * @see java.io.FilterOutputStream#out
0N/A */
0N/A public final void writeChars(String s) throws IOException {
0N/A int len = s.length();
0N/A for (int i = 0 ; i < len ; i++) {
0N/A int v = s.charAt(i);
0N/A out.write((v >>> 8) & 0xFF);
0N/A out.write((v >>> 0) & 0xFF);
0N/A }
0N/A incCount(len * 2);
0N/A }
0N/A
0N/A /**
0N/A * Writes a string to the underlying output stream using
0N/A * <a href="DataInput.html#modified-utf-8">modified UTF-8</a>
0N/A * encoding in a machine-independent manner.
0N/A * <p>
0N/A * First, two bytes are written to the output stream as if by the
0N/A * <code>writeShort</code> method giving the number of bytes to
0N/A * follow. This value is the number of bytes actually written out,
0N/A * not the length of the string. Following the length, each character
0N/A * of the string is output, in sequence, using the modified UTF-8 encoding
0N/A * for the character. If no exception is thrown, the counter
0N/A * <code>written</code> is incremented by the total number of
0N/A * bytes written to the output stream. This will be at least two
0N/A * plus the length of <code>str</code>, and at most two plus
0N/A * thrice the length of <code>str</code>.
0N/A *
0N/A * @param str a string to be written.
0N/A * @exception IOException if an I/O error occurs.
0N/A */
0N/A public final void writeUTF(String str) throws IOException {
0N/A writeUTF(str, this);
0N/A }
0N/A
0N/A /**
0N/A * Writes a string to the specified DataOutput using
0N/A * <a href="DataInput.html#modified-utf-8">modified UTF-8</a>
0N/A * encoding in a machine-independent manner.
0N/A * <p>
0N/A * First, two bytes are written to out as if by the <code>writeShort</code>
0N/A * method giving the number of bytes to follow. This value is the number of
0N/A * bytes actually written out, not the length of the string. Following the
0N/A * length, each character of the string is output, in sequence, using the
0N/A * modified UTF-8 encoding for the character. If no exception is thrown, the
0N/A * counter <code>written</code> is incremented by the total number of
0N/A * bytes written to the output stream. This will be at least two
0N/A * plus the length of <code>str</code>, and at most two plus
0N/A * thrice the length of <code>str</code>.
0N/A *
0N/A * @param str a string to be written.
0N/A * @param out destination to write to
0N/A * @return The number of bytes written out.
0N/A * @exception IOException if an I/O error occurs.
0N/A */
0N/A static int writeUTF(String str, DataOutput out) throws IOException {
0N/A int strlen = str.length();
0N/A int utflen = 0;
0N/A int c, count = 0;
0N/A
0N/A /* use charAt instead of copying String to char array */
0N/A for (int i = 0; i < strlen; i++) {
0N/A c = str.charAt(i);
0N/A if ((c >= 0x0001) && (c <= 0x007F)) {
0N/A utflen++;
0N/A } else if (c > 0x07FF) {
0N/A utflen += 3;
0N/A } else {
0N/A utflen += 2;
0N/A }
0N/A }
0N/A
0N/A if (utflen > 65535)
0N/A throw new UTFDataFormatException(
0N/A "encoded string too long: " + utflen + " bytes");
0N/A
0N/A byte[] bytearr = null;
0N/A if (out instanceof DataOutputStream) {
0N/A DataOutputStream dos = (DataOutputStream)out;
0N/A if(dos.bytearr == null || (dos.bytearr.length < (utflen+2)))
0N/A dos.bytearr = new byte[(utflen*2) + 2];
0N/A bytearr = dos.bytearr;
0N/A } else {
0N/A bytearr = new byte[utflen+2];
0N/A }
0N/A
0N/A bytearr[count++] = (byte) ((utflen >>> 8) & 0xFF);
0N/A bytearr[count++] = (byte) ((utflen >>> 0) & 0xFF);
0N/A
0N/A int i=0;
0N/A for (i=0; i<strlen; i++) {
0N/A c = str.charAt(i);
0N/A if (!((c >= 0x0001) && (c <= 0x007F))) break;
0N/A bytearr[count++] = (byte) c;
0N/A }
0N/A
0N/A for (;i < strlen; i++){
0N/A c = str.charAt(i);
0N/A if ((c >= 0x0001) && (c <= 0x007F)) {
0N/A bytearr[count++] = (byte) c;
0N/A
0N/A } else if (c > 0x07FF) {
0N/A bytearr[count++] = (byte) (0xE0 | ((c >> 12) & 0x0F));
0N/A bytearr[count++] = (byte) (0x80 | ((c >> 6) & 0x3F));
0N/A bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F));
0N/A } else {
0N/A bytearr[count++] = (byte) (0xC0 | ((c >> 6) & 0x1F));
0N/A bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F));
0N/A }
0N/A }
0N/A out.write(bytearr, 0, utflen+2);
0N/A return utflen + 2;
0N/A }
0N/A
0N/A /**
0N/A * Returns the current value of the counter <code>written</code>,
0N/A * the number of bytes written to this data output stream so far.
0N/A * If the counter overflows, it will be wrapped to Integer.MAX_VALUE.
0N/A *
0N/A * @return the value of the <code>written</code> field.
0N/A * @see java.io.DataOutputStream#written
0N/A */
0N/A public final int size() {
0N/A return written;
0N/A }
0N/A}