src/java.base/share/classes/java/io/DataOutputStream.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 58288 48e480e56aad
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
54309
c4c225b49c5f 8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents: 47216
diff changeset
     2
 * Copyright (c) 1994, 2019, 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
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    52
     * underlying output stream. The counter {@code written} is
2
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
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    77
     * {@code b}) to the underlying output stream. If no exception
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    78
     * is thrown, the counter {@code written} is incremented by
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    79
     * {@code 1}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * <p>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    81
     * Implements the {@code write} method of {@code OutputStream}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    83
     * @param      b   the {@code byte} to be written.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54309
diff changeset
    84
     * @throws     IOException  if an I/O error occurs.
2
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
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    93
     * Writes {@code len} bytes from the specified byte array
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    94
     * starting at offset {@code off} to the underlying output stream.
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    95
     * If no exception is thrown, the counter {@code written} is
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    96
     * incremented by {@code len}.
2
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.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54309
diff changeset
   101
     * @throws     IOException  if an I/O error occurs.
2
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>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   115
     * The {@code flush} method of {@code DataOutputStream}
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   116
     * calls the {@code flush} method of its underlying output stream.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54309
diff changeset
   118
     * @throws     IOException  if an I/O error occurs.
2
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
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   127
     * Writes a {@code boolean} to the underlying output stream as
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   128
     * a 1-byte value. The value {@code true} is written out as the
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   129
     * value {@code (byte)1}; the value {@code false} is
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   130
     * written out as the value {@code (byte)0}. If no exception is
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   131
     * thrown, the counter {@code written} is incremented by
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   132
     * {@code 1}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   134
     * @param      v   a {@code boolean} value to be written.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54309
diff changeset
   135
     * @throws     IOException  if an I/O error occurs.
2
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
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   144
     * Writes out a {@code byte} to the underlying output stream as
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * a 1-byte value. If no exception is thrown, the counter
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   146
     * {@code written} is incremented by {@code 1}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   148
     * @param      v   a {@code byte} value to be written.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54309
diff changeset
   149
     * @throws     IOException  if an I/O error occurs.
2
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
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   158
     * Writes a {@code short} to the underlying output stream as two
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * bytes, high byte first. If no exception is thrown, the counter
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   160
     * {@code written} is incremented by {@code 2}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   162
     * @param      v   a {@code short} to be written.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54309
diff changeset
   163
     * @throws     IOException  if an I/O error occurs.
2
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
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   173
     * Writes a {@code char} to the underlying output stream as a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * 2-byte value, high byte first. If no exception is thrown, the
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   175
     * counter {@code written} is incremented by {@code 2}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   177
     * @param      v   a {@code char} value to be written.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54309
diff changeset
   178
     * @throws     IOException  if an I/O error occurs.
2
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
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   188
     * Writes an {@code int} to the underlying output stream as four
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * bytes, high byte first. If no exception is thrown, the counter
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   190
     * {@code written} is incremented by {@code 4}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   192
     * @param      v   an {@code int} to be written.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54309
diff changeset
   193
     * @throws     IOException  if an I/O error occurs.
2
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
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   207
     * Writes a {@code long} to the underlying output stream as eight
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * bytes, high byte first. In no exception is thrown, the counter
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   209
     * {@code written} is incremented by {@code 8}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   211
     * @param      v   a {@code long} to be written.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54309
diff changeset
   212
     * @throws     IOException  if an I/O error occurs.
2
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
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   229
     * Converts the float argument to an {@code int} using the
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   230
     * {@code floatToIntBits} method in class {@code Float},
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   231
     * and then writes that {@code int} value to the underlying
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * output stream as a 4-byte quantity, high byte first. If no
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   233
     * exception is thrown, the counter {@code written} is
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   234
     * incremented by {@code 4}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   236
     * @param      v   a {@code float} value to be written.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54309
diff changeset
   237
     * @throws     IOException  if an I/O error occurs.
2
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
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   246
     * Converts the double argument to a {@code long} using the
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   247
     * {@code doubleToLongBits} method in class {@code Double},
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   248
     * and then writes that {@code long} value to the underlying
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * output stream as an 8-byte quantity, high byte first. If no
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   250
     * exception is thrown, the counter {@code written} is
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   251
     * incremented by {@code 8}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   253
     * @param      v   a {@code double} value to be written.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54309
diff changeset
   254
     * @throws     IOException  if an I/O error occurs.
2
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
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   266
     * thrown, the counter {@code written} is incremented by the
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   267
     * length of {@code s}.
2
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.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54309
diff changeset
   270
     * @throws     IOException  if an I/O error occurs.
2
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
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   284
     * if by the {@code writeChar} method. If no exception is
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   285
     * thrown, the counter {@code written} is incremented by twice
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   286
     * the length of {@code s}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   288
     * @param      s   a {@code String} value to be written.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54309
diff changeset
   289
     * @throws     IOException  if an I/O error occurs.
2
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
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   309
     * {@code writeShort} method giving the number of bytes to
2
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
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   314
     * {@code written} is incremented by the total number of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * bytes written to the output stream. This will be at least two
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   316
     * plus the length of {@code str}, and at most two plus
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   317
     * thrice the length of {@code str}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * @param      str   a string to be written.
54309
c4c225b49c5f 8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents: 47216
diff changeset
   320
     * @throws     UTFDataFormatException  if the modified UTF-8 encoding of
c4c225b49c5f 8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents: 47216
diff changeset
   321
     *             {@code str} would exceed 65535 bytes in length
c4c225b49c5f 8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents: 47216
diff changeset
   322
     * @throws     IOException  if some other I/O error occurs.
c4c225b49c5f 8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents: 47216
diff changeset
   323
     * @see        #writeChars(String)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    public final void writeUTF(String str) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        writeUTF(str, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * Writes a string to the specified DataOutput using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * <a href="DataInput.html#modified-utf-8">modified UTF-8</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * encoding in a machine-independent manner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * <p>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   334
     * First, two bytes are written to out as if by the {@code writeShort}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * method giving the number of bytes to follow. This value is the number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * bytes actually written out, not the length of the string. Following the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * length, each character of the string is output, in sequence, using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * modified UTF-8 encoding for the character. If no exception is thrown, the
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   339
     * counter {@code written} is incremented by the total number of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * bytes written to the output stream. This will be at least two
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   341
     * plus the length of {@code str}, and at most two plus
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   342
     * thrice the length of {@code str}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * @param      str   a string to be written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * @param      out   destination to write to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * @return     The number of bytes written out.
54309
c4c225b49c5f 8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents: 47216
diff changeset
   347
     * @throws     UTFDataFormatException  if the modified UTF-8 encoding of
c4c225b49c5f 8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents: 47216
diff changeset
   348
     *             {@code str} would exceed 65535 bytes in length
c4c225b49c5f 8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents: 47216
diff changeset
   349
     * @throws     IOException  if some other I/O error occurs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    static int writeUTF(String str, DataOutput out) throws IOException {
54309
c4c225b49c5f 8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents: 47216
diff changeset
   352
        final int strlen = str.length();
c4c225b49c5f 8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents: 47216
diff changeset
   353
        int utflen = strlen; // optimized for ASCII
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        for (int i = 0; i < strlen; i++) {
54309
c4c225b49c5f 8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents: 47216
diff changeset
   356
            int c = str.charAt(i);
c4c225b49c5f 8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents: 47216
diff changeset
   357
            if (c >= 0x80 || c == 0)
c4c225b49c5f 8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents: 47216
diff changeset
   358
                utflen += (c >= 0x800) ? 2 : 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
54309
c4c225b49c5f 8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents: 47216
diff changeset
   361
        if (utflen > 65535 || /* overflow */ utflen < strlen)
c4c225b49c5f 8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents: 47216
diff changeset
   362
            throw new UTFDataFormatException(tooLongMsg(str, utflen));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
54309
c4c225b49c5f 8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents: 47216
diff changeset
   364
        final byte[] bytearr;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        if (out instanceof DataOutputStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            DataOutputStream dos = (DataOutputStream)out;
54309
c4c225b49c5f 8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents: 47216
diff changeset
   367
            if (dos.bytearr == null || (dos.bytearr.length < (utflen + 2)))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                dos.bytearr = new byte[(utflen*2) + 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            bytearr = dos.bytearr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        } else {
54309
c4c225b49c5f 8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents: 47216
diff changeset
   371
            bytearr = new byte[utflen + 2];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
54309
c4c225b49c5f 8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents: 47216
diff changeset
   374
        int count = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        bytearr[count++] = (byte) ((utflen >>> 8) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        bytearr[count++] = (byte) ((utflen >>> 0) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
54309
c4c225b49c5f 8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents: 47216
diff changeset
   378
        int i = 0;
c4c225b49c5f 8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents: 47216
diff changeset
   379
        for (i = 0; i < strlen; i++) { // optimized for initial run of ASCII
c4c225b49c5f 8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents: 47216
diff changeset
   380
            int c = str.charAt(i);
c4c225b49c5f 8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents: 47216
diff changeset
   381
            if (c >= 0x80 || c == 0) break;
c4c225b49c5f 8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents: 47216
diff changeset
   382
            bytearr[count++] = (byte) c;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
54309
c4c225b49c5f 8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents: 47216
diff changeset
   385
        for (; i < strlen; i++) {
c4c225b49c5f 8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents: 47216
diff changeset
   386
            int c = str.charAt(i);
c4c225b49c5f 8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents: 47216
diff changeset
   387
            if (c < 0x80 && c != 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                bytearr[count++] = (byte) c;
54309
c4c225b49c5f 8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents: 47216
diff changeset
   389
            } else if (c >= 0x800) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                bytearr[count++] = (byte) (0xE0 | ((c >> 12) & 0x0F));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                bytearr[count++] = (byte) (0x80 | ((c >>  6) & 0x3F));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                bytearr[count++] = (byte) (0x80 | ((c >>  0) & 0x3F));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                bytearr[count++] = (byte) (0xC0 | ((c >>  6) & 0x1F));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                bytearr[count++] = (byte) (0x80 | ((c >>  0) & 0x3F));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        }
54309
c4c225b49c5f 8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents: 47216
diff changeset
   398
        out.write(bytearr, 0, utflen + 2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        return utflen + 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
54309
c4c225b49c5f 8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents: 47216
diff changeset
   402
    private static String tooLongMsg(String s, int bits32) {
c4c225b49c5f 8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents: 47216
diff changeset
   403
        int slen = s.length();
c4c225b49c5f 8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents: 47216
diff changeset
   404
        String head = s.substring(0, 8);
c4c225b49c5f 8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents: 47216
diff changeset
   405
        String tail = s.substring(slen - 8, slen);
c4c225b49c5f 8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents: 47216
diff changeset
   406
        // handle int overflow with max 3x expansion
c4c225b49c5f 8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents: 47216
diff changeset
   407
        long actualLength = (long)slen + Integer.toUnsignedLong(bits32 - slen);
c4c225b49c5f 8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents: 47216
diff changeset
   408
        return "encoded string (" + head + "..." + tail + ") too long: "
c4c225b49c5f 8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents: 47216
diff changeset
   409
            + actualLength + " bytes";
c4c225b49c5f 8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents: 47216
diff changeset
   410
    }
c4c225b49c5f 8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents: 47216
diff changeset
   411
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   413
     * Returns the current value of the counter {@code written},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * the number of bytes written to this data output stream so far.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * If the counter overflows, it will be wrapped to Integer.MAX_VALUE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   417
     * @return  the value of the {@code written} field.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * @see     java.io.DataOutputStream#written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    public final int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        return written;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
}