src/java.base/share/classes/java/io/DataOutputStream.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 25859 jdk/src/java.base/share/classes/java/io/DataOutputStream.java@3317bb8137f4
child 54309 c4c225b49c5f
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1994, 2004, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.io;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * A data output stream lets an application write primitive Java data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * types to an output stream in a portable way. An application can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * then use a data input stream to read the data back in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * @author  unascribed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * @see     java.io.DataInputStream
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 5506
diff changeset
    35
 * @since   1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
class DataOutputStream extends FilterOutputStream implements DataOutput {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
     * The number of bytes written to the data output stream so far.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
     * If this counter overflows, it will be wrapped to Integer.MAX_VALUE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    protected int written;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * bytearr is initialized on demand by writeUTF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private byte[] bytearr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * Creates a new data output stream to write data to the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * underlying output stream. The counter <code>written</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * set to zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * @param   out   the underlying output stream, to be saved for later
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     *                use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * @see     java.io.FilterOutputStream#out
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public DataOutputStream(OutputStream out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        super(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Increases the written counter by the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * until it reaches Integer.MAX_VALUE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private void incCount(int value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        int temp = written + value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        if (temp < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            temp = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        written = temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Writes the specified byte (the low eight bits of the argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * <code>b</code>) to the underlying output stream. If no exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * is thrown, the counter <code>written</code> is incremented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * <code>1</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * Implements the <code>write</code> method of <code>OutputStream</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @param      b   the <code>byte</code> to be written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @see        java.io.FilterOutputStream#out
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public synchronized void write(int b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        out.write(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        incCount(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Writes <code>len</code> bytes from the specified byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * starting at offset <code>off</code> to the underlying output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * If no exception is thrown, the counter <code>written</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * incremented by <code>len</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @param      b     the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @param      off   the start offset in the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @param      len   the number of bytes to write.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @see        java.io.FilterOutputStream#out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public synchronized void write(byte b[], int off, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        out.write(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        incCount(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * Flushes this data output stream. This forces any buffered output
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * bytes to be written out to the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * The <code>flush</code> method of <code>DataOutputStream</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * calls the <code>flush</code> method of its underlying output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @see        java.io.FilterOutputStream#out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @see        java.io.OutputStream#flush()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public void flush() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Writes a <code>boolean</code> to the underlying output stream as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * a 1-byte value. The value <code>true</code> is written out as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * value <code>(byte)1</code>; the value <code>false</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * written out as the value <code>(byte)0</code>. If no exception is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * thrown, the counter <code>written</code> is incremented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * <code>1</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @param      v   a <code>boolean</code> value to be written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @see        java.io.FilterOutputStream#out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public final void writeBoolean(boolean v) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        out.write(v ? 1 : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        incCount(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Writes out a <code>byte</code> to the underlying output stream as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * a 1-byte value. If no exception is thrown, the counter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * <code>written</code> is incremented by <code>1</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @param      v   a <code>byte</code> value to be written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @see        java.io.FilterOutputStream#out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public final void writeByte(int v) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        out.write(v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        incCount(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * Writes a <code>short</code> to the underlying output stream as two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * bytes, high byte first. If no exception is thrown, the counter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * <code>written</code> is incremented by <code>2</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @param      v   a <code>short</code> to be written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @see        java.io.FilterOutputStream#out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    public final void writeShort(int v) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        out.write((v >>> 8) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        out.write((v >>> 0) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        incCount(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * Writes a <code>char</code> to the underlying output stream as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * 2-byte value, high byte first. If no exception is thrown, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * counter <code>written</code> is incremented by <code>2</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @param      v   a <code>char</code> value to be written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @see        java.io.FilterOutputStream#out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    public final void writeChar(int v) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        out.write((v >>> 8) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        out.write((v >>> 0) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        incCount(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * Writes an <code>int</code> to the underlying output stream as four
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * bytes, high byte first. If no exception is thrown, the counter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * <code>written</code> is incremented by <code>4</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @param      v   an <code>int</code> to be written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @see        java.io.FilterOutputStream#out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    public final void writeInt(int v) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        out.write((v >>> 24) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        out.write((v >>> 16) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        out.write((v >>>  8) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        out.write((v >>>  0) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        incCount(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    private byte writeBuffer[] = new byte[8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * Writes a <code>long</code> to the underlying output stream as eight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * bytes, high byte first. In no exception is thrown, the counter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * <code>written</code> is incremented by <code>8</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @param      v   a <code>long</code> to be written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @see        java.io.FilterOutputStream#out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    public final void writeLong(long v) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        writeBuffer[0] = (byte)(v >>> 56);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        writeBuffer[1] = (byte)(v >>> 48);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        writeBuffer[2] = (byte)(v >>> 40);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        writeBuffer[3] = (byte)(v >>> 32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        writeBuffer[4] = (byte)(v >>> 24);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        writeBuffer[5] = (byte)(v >>> 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        writeBuffer[6] = (byte)(v >>>  8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        writeBuffer[7] = (byte)(v >>>  0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        out.write(writeBuffer, 0, 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        incCount(8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * Converts the float argument to an <code>int</code> using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * <code>floatToIntBits</code> method in class <code>Float</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * and then writes that <code>int</code> value to the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * output stream as a 4-byte quantity, high byte first. If no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * exception is thrown, the counter <code>written</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * incremented by <code>4</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @param      v   a <code>float</code> value to be written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @see        java.io.FilterOutputStream#out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @see        java.lang.Float#floatToIntBits(float)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    public final void writeFloat(float v) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        writeInt(Float.floatToIntBits(v));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * Converts the double argument to a <code>long</code> using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * <code>doubleToLongBits</code> method in class <code>Double</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * and then writes that <code>long</code> value to the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * output stream as an 8-byte quantity, high byte first. If no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * exception is thrown, the counter <code>written</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * incremented by <code>8</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @param      v   a <code>double</code> value to be written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * @see        java.io.FilterOutputStream#out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * @see        java.lang.Double#doubleToLongBits(double)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    public final void writeDouble(double v) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        writeLong(Double.doubleToLongBits(v));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * Writes out the string to the underlying output stream as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * sequence of bytes. Each character in the string is written out, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * sequence, by discarding its high eight bits. If no exception is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * thrown, the counter <code>written</code> is incremented by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * length of <code>s</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @param      s   a string of bytes to be written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @see        java.io.FilterOutputStream#out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    public final void writeBytes(String s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        int len = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        for (int i = 0 ; i < len ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            out.write((byte)s.charAt(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        incCount(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * Writes a string to the underlying output stream as a sequence of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * characters. Each character is written to the data output stream as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * if by the <code>writeChar</code> method. If no exception is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * thrown, the counter <code>written</code> is incremented by twice
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * the length of <code>s</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * @param      s   a <code>String</code> value to be written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @see        java.io.DataOutputStream#writeChar(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @see        java.io.FilterOutputStream#out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    public final void writeChars(String s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        int len = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        for (int i = 0 ; i < len ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            int v = s.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            out.write((v >>> 8) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            out.write((v >>> 0) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        incCount(len * 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * Writes a string to the underlying output stream using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * <a href="DataInput.html#modified-utf-8">modified UTF-8</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * encoding in a machine-independent manner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * First, two bytes are written to the output stream as if by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * <code>writeShort</code> method giving the number of bytes to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * follow. This value is the number of bytes actually written out,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * not the length of the string. Following the length, each character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * of the string is output, in sequence, using the modified UTF-8 encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * for the character. If no exception is thrown, the counter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * <code>written</code> is incremented by the total number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * bytes written to the output stream. This will be at least two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * plus the length of <code>str</code>, and at most two plus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * thrice the length of <code>str</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * @param      str   a string to be written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    public final void writeUTF(String str) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        writeUTF(str, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * Writes a string to the specified DataOutput using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * <a href="DataInput.html#modified-utf-8">modified UTF-8</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * encoding in a machine-independent manner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * First, two bytes are written to out as if by the <code>writeShort</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * method giving the number of bytes to follow. This value is the number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * bytes actually written out, not the length of the string. Following the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * length, each character of the string is output, in sequence, using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * modified UTF-8 encoding for the character. If no exception is thrown, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * counter <code>written</code> is incremented by the total number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * bytes written to the output stream. This will be at least two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * plus the length of <code>str</code>, and at most two plus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * thrice the length of <code>str</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * @param      str   a string to be written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * @param      out   destination to write to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * @return     The number of bytes written out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    static int writeUTF(String str, DataOutput out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        int strlen = str.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        int utflen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        int c, count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        /* use charAt instead of copying String to char array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        for (int i = 0; i < strlen; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            c = str.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            if ((c >= 0x0001) && (c <= 0x007F)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                utflen++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            } else if (c > 0x07FF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                utflen += 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                utflen += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        if (utflen > 65535)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            throw new UTFDataFormatException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                "encoded string too long: " + utflen + " bytes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        byte[] bytearr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        if (out instanceof DataOutputStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            DataOutputStream dos = (DataOutputStream)out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            if(dos.bytearr == null || (dos.bytearr.length < (utflen+2)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                dos.bytearr = new byte[(utflen*2) + 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            bytearr = dos.bytearr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            bytearr = new byte[utflen+2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        bytearr[count++] = (byte) ((utflen >>> 8) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        bytearr[count++] = (byte) ((utflen >>> 0) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        int i=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        for (i=0; i<strlen; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
           c = str.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
           if (!((c >= 0x0001) && (c <= 0x007F))) break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
           bytearr[count++] = (byte) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        for (;i < strlen; i++){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            c = str.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            if ((c >= 0x0001) && (c <= 0x007F)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                bytearr[count++] = (byte) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            } else if (c > 0x07FF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                bytearr[count++] = (byte) (0xE0 | ((c >> 12) & 0x0F));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                bytearr[count++] = (byte) (0x80 | ((c >>  6) & 0x3F));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                bytearr[count++] = (byte) (0x80 | ((c >>  0) & 0x3F));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                bytearr[count++] = (byte) (0xC0 | ((c >>  6) & 0x1F));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                bytearr[count++] = (byte) (0x80 | ((c >>  0) & 0x3F));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        out.write(bytearr, 0, utflen+2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        return utflen + 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * Returns the current value of the counter <code>written</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * the number of bytes written to this data output stream so far.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * If the counter overflows, it will be wrapped to Integer.MAX_VALUE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * @return  the value of the <code>written</code> field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * @see     java.io.DataOutputStream#written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    public final int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        return written;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
}