emul/compact/src/main/java/java/io/DataOutputStream.java
changeset 772 d382dacfd73f
parent 771 4252bfc396fc
child 773 406faa8bc64f
     1.1 --- a/emul/compact/src/main/java/java/io/DataOutputStream.java	Tue Feb 26 14:55:55 2013 +0100
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,416 +0,0 @@
     1.4 -/*
     1.5 - * Copyright (c) 1994, 2004, Oracle and/or its affiliates. All rights reserved.
     1.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 - *
     1.8 - * This code is free software; you can redistribute it and/or modify it
     1.9 - * under the terms of the GNU General Public License version 2 only, as
    1.10 - * published by the Free Software Foundation.  Oracle designates this
    1.11 - * particular file as subject to the "Classpath" exception as provided
    1.12 - * by Oracle in the LICENSE file that accompanied this code.
    1.13 - *
    1.14 - * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 - * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 - * version 2 for more details (a copy is included in the LICENSE file that
    1.18 - * accompanied this code).
    1.19 - *
    1.20 - * You should have received a copy of the GNU General Public License version
    1.21 - * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 - *
    1.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 - * or visit www.oracle.com if you need additional information or have any
    1.26 - * questions.
    1.27 - */
    1.28 -
    1.29 -package java.io;
    1.30 -
    1.31 -/**
    1.32 - * A data output stream lets an application write primitive Java data
    1.33 - * types to an output stream in a portable way. An application can
    1.34 - * then use a data input stream to read the data back in.
    1.35 - *
    1.36 - * @author  unascribed
    1.37 - * @see     java.io.DataInputStream
    1.38 - * @since   JDK1.0
    1.39 - */
    1.40 -public
    1.41 -class DataOutputStream extends FilterOutputStream implements DataOutput {
    1.42 -    /**
    1.43 -     * The number of bytes written to the data output stream so far.
    1.44 -     * If this counter overflows, it will be wrapped to Integer.MAX_VALUE.
    1.45 -     */
    1.46 -    protected int written;
    1.47 -
    1.48 -    /**
    1.49 -     * bytearr is initialized on demand by writeUTF
    1.50 -     */
    1.51 -    private byte[] bytearr = null;
    1.52 -
    1.53 -    /**
    1.54 -     * Creates a new data output stream to write data to the specified
    1.55 -     * underlying output stream. The counter <code>written</code> is
    1.56 -     * set to zero.
    1.57 -     *
    1.58 -     * @param   out   the underlying output stream, to be saved for later
    1.59 -     *                use.
    1.60 -     * @see     java.io.FilterOutputStream#out
    1.61 -     */
    1.62 -    public DataOutputStream(OutputStream out) {
    1.63 -        super(out);
    1.64 -    }
    1.65 -
    1.66 -    /**
    1.67 -     * Increases the written counter by the specified value
    1.68 -     * until it reaches Integer.MAX_VALUE.
    1.69 -     */
    1.70 -    private void incCount(int value) {
    1.71 -        int temp = written + value;
    1.72 -        if (temp < 0) {
    1.73 -            temp = Integer.MAX_VALUE;
    1.74 -        }
    1.75 -        written = temp;
    1.76 -    }
    1.77 -
    1.78 -    /**
    1.79 -     * Writes the specified byte (the low eight bits of the argument
    1.80 -     * <code>b</code>) to the underlying output stream. If no exception
    1.81 -     * is thrown, the counter <code>written</code> is incremented by
    1.82 -     * <code>1</code>.
    1.83 -     * <p>
    1.84 -     * Implements the <code>write</code> method of <code>OutputStream</code>.
    1.85 -     *
    1.86 -     * @param      b   the <code>byte</code> to be written.
    1.87 -     * @exception  IOException  if an I/O error occurs.
    1.88 -     * @see        java.io.FilterOutputStream#out
    1.89 -     */
    1.90 -    public synchronized void write(int b) throws IOException {
    1.91 -        out.write(b);
    1.92 -        incCount(1);
    1.93 -    }
    1.94 -
    1.95 -    /**
    1.96 -     * Writes <code>len</code> bytes from the specified byte array
    1.97 -     * starting at offset <code>off</code> to the underlying output stream.
    1.98 -     * If no exception is thrown, the counter <code>written</code> is
    1.99 -     * incremented by <code>len</code>.
   1.100 -     *
   1.101 -     * @param      b     the data.
   1.102 -     * @param      off   the start offset in the data.
   1.103 -     * @param      len   the number of bytes to write.
   1.104 -     * @exception  IOException  if an I/O error occurs.
   1.105 -     * @see        java.io.FilterOutputStream#out
   1.106 -     */
   1.107 -    public synchronized void write(byte b[], int off, int len)
   1.108 -        throws IOException
   1.109 -    {
   1.110 -        out.write(b, off, len);
   1.111 -        incCount(len);
   1.112 -    }
   1.113 -
   1.114 -    /**
   1.115 -     * Flushes this data output stream. This forces any buffered output
   1.116 -     * bytes to be written out to the stream.
   1.117 -     * <p>
   1.118 -     * The <code>flush</code> method of <code>DataOutputStream</code>
   1.119 -     * calls the <code>flush</code> method of its underlying output stream.
   1.120 -     *
   1.121 -     * @exception  IOException  if an I/O error occurs.
   1.122 -     * @see        java.io.FilterOutputStream#out
   1.123 -     * @see        java.io.OutputStream#flush()
   1.124 -     */
   1.125 -    public void flush() throws IOException {
   1.126 -        out.flush();
   1.127 -    }
   1.128 -
   1.129 -    /**
   1.130 -     * Writes a <code>boolean</code> to the underlying output stream as
   1.131 -     * a 1-byte value. The value <code>true</code> is written out as the
   1.132 -     * value <code>(byte)1</code>; the value <code>false</code> is
   1.133 -     * written out as the value <code>(byte)0</code>. If no exception is
   1.134 -     * thrown, the counter <code>written</code> is incremented by
   1.135 -     * <code>1</code>.
   1.136 -     *
   1.137 -     * @param      v   a <code>boolean</code> value to be written.
   1.138 -     * @exception  IOException  if an I/O error occurs.
   1.139 -     * @see        java.io.FilterOutputStream#out
   1.140 -     */
   1.141 -    public final void writeBoolean(boolean v) throws IOException {
   1.142 -        out.write(v ? 1 : 0);
   1.143 -        incCount(1);
   1.144 -    }
   1.145 -
   1.146 -    /**
   1.147 -     * Writes out a <code>byte</code> to the underlying output stream as
   1.148 -     * a 1-byte value. If no exception is thrown, the counter
   1.149 -     * <code>written</code> is incremented by <code>1</code>.
   1.150 -     *
   1.151 -     * @param      v   a <code>byte</code> value to be written.
   1.152 -     * @exception  IOException  if an I/O error occurs.
   1.153 -     * @see        java.io.FilterOutputStream#out
   1.154 -     */
   1.155 -    public final void writeByte(int v) throws IOException {
   1.156 -        out.write(v);
   1.157 -        incCount(1);
   1.158 -    }
   1.159 -
   1.160 -    /**
   1.161 -     * Writes a <code>short</code> to the underlying output stream as two
   1.162 -     * bytes, high byte first. If no exception is thrown, the counter
   1.163 -     * <code>written</code> is incremented by <code>2</code>.
   1.164 -     *
   1.165 -     * @param      v   a <code>short</code> to be written.
   1.166 -     * @exception  IOException  if an I/O error occurs.
   1.167 -     * @see        java.io.FilterOutputStream#out
   1.168 -     */
   1.169 -    public final void writeShort(int v) throws IOException {
   1.170 -        out.write((v >>> 8) & 0xFF);
   1.171 -        out.write((v >>> 0) & 0xFF);
   1.172 -        incCount(2);
   1.173 -    }
   1.174 -
   1.175 -    /**
   1.176 -     * Writes a <code>char</code> to the underlying output stream as a
   1.177 -     * 2-byte value, high byte first. If no exception is thrown, the
   1.178 -     * counter <code>written</code> is incremented by <code>2</code>.
   1.179 -     *
   1.180 -     * @param      v   a <code>char</code> value to be written.
   1.181 -     * @exception  IOException  if an I/O error occurs.
   1.182 -     * @see        java.io.FilterOutputStream#out
   1.183 -     */
   1.184 -    public final void writeChar(int v) throws IOException {
   1.185 -        out.write((v >>> 8) & 0xFF);
   1.186 -        out.write((v >>> 0) & 0xFF);
   1.187 -        incCount(2);
   1.188 -    }
   1.189 -
   1.190 -    /**
   1.191 -     * Writes an <code>int</code> to the underlying output stream as four
   1.192 -     * bytes, high byte first. If no exception is thrown, the counter
   1.193 -     * <code>written</code> is incremented by <code>4</code>.
   1.194 -     *
   1.195 -     * @param      v   an <code>int</code> to be written.
   1.196 -     * @exception  IOException  if an I/O error occurs.
   1.197 -     * @see        java.io.FilterOutputStream#out
   1.198 -     */
   1.199 -    public final void writeInt(int v) throws IOException {
   1.200 -        out.write((v >>> 24) & 0xFF);
   1.201 -        out.write((v >>> 16) & 0xFF);
   1.202 -        out.write((v >>>  8) & 0xFF);
   1.203 -        out.write((v >>>  0) & 0xFF);
   1.204 -        incCount(4);
   1.205 -    }
   1.206 -
   1.207 -    private byte writeBuffer[] = new byte[8];
   1.208 -
   1.209 -    /**
   1.210 -     * Writes a <code>long</code> to the underlying output stream as eight
   1.211 -     * bytes, high byte first. In no exception is thrown, the counter
   1.212 -     * <code>written</code> is incremented by <code>8</code>.
   1.213 -     *
   1.214 -     * @param      v   a <code>long</code> to be written.
   1.215 -     * @exception  IOException  if an I/O error occurs.
   1.216 -     * @see        java.io.FilterOutputStream#out
   1.217 -     */
   1.218 -    public final void writeLong(long v) throws IOException {
   1.219 -        writeBuffer[0] = (byte)(v >>> 56);
   1.220 -        writeBuffer[1] = (byte)(v >>> 48);
   1.221 -        writeBuffer[2] = (byte)(v >>> 40);
   1.222 -        writeBuffer[3] = (byte)(v >>> 32);
   1.223 -        writeBuffer[4] = (byte)(v >>> 24);
   1.224 -        writeBuffer[5] = (byte)(v >>> 16);
   1.225 -        writeBuffer[6] = (byte)(v >>>  8);
   1.226 -        writeBuffer[7] = (byte)(v >>>  0);
   1.227 -        out.write(writeBuffer, 0, 8);
   1.228 -        incCount(8);
   1.229 -    }
   1.230 -
   1.231 -    /**
   1.232 -     * Converts the float argument to an <code>int</code> using the
   1.233 -     * <code>floatToIntBits</code> method in class <code>Float</code>,
   1.234 -     * and then writes that <code>int</code> value to the underlying
   1.235 -     * output stream as a 4-byte quantity, high byte first. If no
   1.236 -     * exception is thrown, the counter <code>written</code> is
   1.237 -     * incremented by <code>4</code>.
   1.238 -     *
   1.239 -     * @param      v   a <code>float</code> value to be written.
   1.240 -     * @exception  IOException  if an I/O error occurs.
   1.241 -     * @see        java.io.FilterOutputStream#out
   1.242 -     * @see        java.lang.Float#floatToIntBits(float)
   1.243 -     */
   1.244 -    public final void writeFloat(float v) throws IOException {
   1.245 -        writeInt(Float.floatToIntBits(v));
   1.246 -    }
   1.247 -
   1.248 -    /**
   1.249 -     * Converts the double argument to a <code>long</code> using the
   1.250 -     * <code>doubleToLongBits</code> method in class <code>Double</code>,
   1.251 -     * and then writes that <code>long</code> value to the underlying
   1.252 -     * output stream as an 8-byte quantity, high byte first. If no
   1.253 -     * exception is thrown, the counter <code>written</code> is
   1.254 -     * incremented by <code>8</code>.
   1.255 -     *
   1.256 -     * @param      v   a <code>double</code> value to be written.
   1.257 -     * @exception  IOException  if an I/O error occurs.
   1.258 -     * @see        java.io.FilterOutputStream#out
   1.259 -     * @see        java.lang.Double#doubleToLongBits(double)
   1.260 -     */
   1.261 -    public final void writeDouble(double v) throws IOException {
   1.262 -        writeLong(Double.doubleToLongBits(v));
   1.263 -    }
   1.264 -
   1.265 -    /**
   1.266 -     * Writes out the string to the underlying output stream as a
   1.267 -     * sequence of bytes. Each character in the string is written out, in
   1.268 -     * sequence, by discarding its high eight bits. If no exception is
   1.269 -     * thrown, the counter <code>written</code> is incremented by the
   1.270 -     * length of <code>s</code>.
   1.271 -     *
   1.272 -     * @param      s   a string of bytes to be written.
   1.273 -     * @exception  IOException  if an I/O error occurs.
   1.274 -     * @see        java.io.FilterOutputStream#out
   1.275 -     */
   1.276 -    public final void writeBytes(String s) throws IOException {
   1.277 -        int len = s.length();
   1.278 -        for (int i = 0 ; i < len ; i++) {
   1.279 -            out.write((byte)s.charAt(i));
   1.280 -        }
   1.281 -        incCount(len);
   1.282 -    }
   1.283 -
   1.284 -    /**
   1.285 -     * Writes a string to the underlying output stream as a sequence of
   1.286 -     * characters. Each character is written to the data output stream as
   1.287 -     * if by the <code>writeChar</code> method. If no exception is
   1.288 -     * thrown, the counter <code>written</code> is incremented by twice
   1.289 -     * the length of <code>s</code>.
   1.290 -     *
   1.291 -     * @param      s   a <code>String</code> value to be written.
   1.292 -     * @exception  IOException  if an I/O error occurs.
   1.293 -     * @see        java.io.DataOutputStream#writeChar(int)
   1.294 -     * @see        java.io.FilterOutputStream#out
   1.295 -     */
   1.296 -    public final void writeChars(String s) throws IOException {
   1.297 -        int len = s.length();
   1.298 -        for (int i = 0 ; i < len ; i++) {
   1.299 -            int v = s.charAt(i);
   1.300 -            out.write((v >>> 8) & 0xFF);
   1.301 -            out.write((v >>> 0) & 0xFF);
   1.302 -        }
   1.303 -        incCount(len * 2);
   1.304 -    }
   1.305 -
   1.306 -    /**
   1.307 -     * Writes a string to the underlying output stream using
   1.308 -     * <a href="DataInput.html#modified-utf-8">modified UTF-8</a>
   1.309 -     * encoding in a machine-independent manner.
   1.310 -     * <p>
   1.311 -     * First, two bytes are written to the output stream as if by the
   1.312 -     * <code>writeShort</code> method giving the number of bytes to
   1.313 -     * follow. This value is the number of bytes actually written out,
   1.314 -     * not the length of the string. Following the length, each character
   1.315 -     * of the string is output, in sequence, using the modified UTF-8 encoding
   1.316 -     * for the character. If no exception is thrown, the counter
   1.317 -     * <code>written</code> is incremented by the total number of
   1.318 -     * bytes written to the output stream. This will be at least two
   1.319 -     * plus the length of <code>str</code>, and at most two plus
   1.320 -     * thrice the length of <code>str</code>.
   1.321 -     *
   1.322 -     * @param      str   a string to be written.
   1.323 -     * @exception  IOException  if an I/O error occurs.
   1.324 -     */
   1.325 -    public final void writeUTF(String str) throws IOException {
   1.326 -        writeUTF(str, this);
   1.327 -    }
   1.328 -
   1.329 -    /**
   1.330 -     * Writes a string to the specified DataOutput using
   1.331 -     * <a href="DataInput.html#modified-utf-8">modified UTF-8</a>
   1.332 -     * encoding in a machine-independent manner.
   1.333 -     * <p>
   1.334 -     * First, two bytes are written to out as if by the <code>writeShort</code>
   1.335 -     * method giving the number of bytes to follow. This value is the number of
   1.336 -     * bytes actually written out, not the length of the string. Following the
   1.337 -     * length, each character of the string is output, in sequence, using the
   1.338 -     * modified UTF-8 encoding for the character. If no exception is thrown, the
   1.339 -     * counter <code>written</code> is incremented by the total number of
   1.340 -     * bytes written to the output stream. This will be at least two
   1.341 -     * plus the length of <code>str</code>, and at most two plus
   1.342 -     * thrice the length of <code>str</code>.
   1.343 -     *
   1.344 -     * @param      str   a string to be written.
   1.345 -     * @param      out   destination to write to
   1.346 -     * @return     The number of bytes written out.
   1.347 -     * @exception  IOException  if an I/O error occurs.
   1.348 -     */
   1.349 -    static int writeUTF(String str, DataOutput out) throws IOException {
   1.350 -        int strlen = str.length();
   1.351 -        int utflen = 0;
   1.352 -        int c, count = 0;
   1.353 -
   1.354 -        /* use charAt instead of copying String to char array */
   1.355 -        for (int i = 0; i < strlen; i++) {
   1.356 -            c = str.charAt(i);
   1.357 -            if ((c >= 0x0001) && (c <= 0x007F)) {
   1.358 -                utflen++;
   1.359 -            } else if (c > 0x07FF) {
   1.360 -                utflen += 3;
   1.361 -            } else {
   1.362 -                utflen += 2;
   1.363 -            }
   1.364 -        }
   1.365 -
   1.366 -        if (utflen > 65535)
   1.367 -            throw new UTFDataFormatException(
   1.368 -                "encoded string too long: " + utflen + " bytes");
   1.369 -
   1.370 -        byte[] bytearr = null;
   1.371 -        if (out instanceof DataOutputStream) {
   1.372 -            DataOutputStream dos = (DataOutputStream)out;
   1.373 -            if(dos.bytearr == null || (dos.bytearr.length < (utflen+2)))
   1.374 -                dos.bytearr = new byte[(utflen*2) + 2];
   1.375 -            bytearr = dos.bytearr;
   1.376 -        } else {
   1.377 -            bytearr = new byte[utflen+2];
   1.378 -        }
   1.379 -
   1.380 -        bytearr[count++] = (byte) ((utflen >>> 8) & 0xFF);
   1.381 -        bytearr[count++] = (byte) ((utflen >>> 0) & 0xFF);
   1.382 -
   1.383 -        int i=0;
   1.384 -        for (i=0; i<strlen; i++) {
   1.385 -           c = str.charAt(i);
   1.386 -           if (!((c >= 0x0001) && (c <= 0x007F))) break;
   1.387 -           bytearr[count++] = (byte) c;
   1.388 -        }
   1.389 -
   1.390 -        for (;i < strlen; i++){
   1.391 -            c = str.charAt(i);
   1.392 -            if ((c >= 0x0001) && (c <= 0x007F)) {
   1.393 -                bytearr[count++] = (byte) c;
   1.394 -
   1.395 -            } else if (c > 0x07FF) {
   1.396 -                bytearr[count++] = (byte) (0xE0 | ((c >> 12) & 0x0F));
   1.397 -                bytearr[count++] = (byte) (0x80 | ((c >>  6) & 0x3F));
   1.398 -                bytearr[count++] = (byte) (0x80 | ((c >>  0) & 0x3F));
   1.399 -            } else {
   1.400 -                bytearr[count++] = (byte) (0xC0 | ((c >>  6) & 0x1F));
   1.401 -                bytearr[count++] = (byte) (0x80 | ((c >>  0) & 0x3F));
   1.402 -            }
   1.403 -        }
   1.404 -        out.write(bytearr, 0, utflen+2);
   1.405 -        return utflen + 2;
   1.406 -    }
   1.407 -
   1.408 -    /**
   1.409 -     * Returns the current value of the counter <code>written</code>,
   1.410 -     * the number of bytes written to this data output stream so far.
   1.411 -     * If the counter overflows, it will be wrapped to Integer.MAX_VALUE.
   1.412 -     *
   1.413 -     * @return  the value of the <code>written</code> field.
   1.414 -     * @see     java.io.DataOutputStream#written
   1.415 -     */
   1.416 -    public final int size() {
   1.417 -        return written;
   1.418 -    }
   1.419 -}