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