src/java.base/share/classes/java/io/DataOutputStream.java
changeset 58288 48e480e56aad
parent 58242 94bb65cb37d3
child 58679 9c3209ff7550
child 59201 b24f4caa1411
equal deleted inserted replaced
58287:a7f16447085e 58288:48e480e56aad
    47      */
    47      */
    48     private byte[] bytearr = null;
    48     private byte[] bytearr = null;
    49 
    49 
    50     /**
    50     /**
    51      * Creates a new data output stream to write data to the specified
    51      * Creates a new data output stream to write data to the specified
    52      * underlying output stream. The counter <code>written</code> is
    52      * underlying output stream. The counter {@code written} is
    53      * set to zero.
    53      * set to zero.
    54      *
    54      *
    55      * @param   out   the underlying output stream, to be saved for later
    55      * @param   out   the underlying output stream, to be saved for later
    56      *                use.
    56      *                use.
    57      * @see     java.io.FilterOutputStream#out
    57      * @see     java.io.FilterOutputStream#out
    72         written = temp;
    72         written = temp;
    73     }
    73     }
    74 
    74 
    75     /**
    75     /**
    76      * Writes the specified byte (the low eight bits of the argument
    76      * Writes the specified byte (the low eight bits of the argument
    77      * <code>b</code>) to the underlying output stream. If no exception
    77      * {@code b}) to the underlying output stream. If no exception
    78      * is thrown, the counter <code>written</code> is incremented by
    78      * is thrown, the counter {@code written} is incremented by
    79      * <code>1</code>.
    79      * {@code 1}.
    80      * <p>
    80      * <p>
    81      * Implements the <code>write</code> method of <code>OutputStream</code>.
    81      * Implements the {@code write} method of {@code OutputStream}.
    82      *
    82      *
    83      * @param      b   the <code>byte</code> to be written.
    83      * @param      b   the {@code byte} to be written.
    84      * @throws     IOException  if an I/O error occurs.
    84      * @throws     IOException  if an I/O error occurs.
    85      * @see        java.io.FilterOutputStream#out
    85      * @see        java.io.FilterOutputStream#out
    86      */
    86      */
    87     public synchronized void write(int b) throws IOException {
    87     public synchronized void write(int b) throws IOException {
    88         out.write(b);
    88         out.write(b);
    89         incCount(1);
    89         incCount(1);
    90     }
    90     }
    91 
    91 
    92     /**
    92     /**
    93      * Writes <code>len</code> bytes from the specified byte array
    93      * Writes {@code len} bytes from the specified byte array
    94      * starting at offset <code>off</code> to the underlying output stream.
    94      * starting at offset {@code off} to the underlying output stream.
    95      * If no exception is thrown, the counter <code>written</code> is
    95      * If no exception is thrown, the counter {@code written} is
    96      * incremented by <code>len</code>.
    96      * incremented by {@code len}.
    97      *
    97      *
    98      * @param      b     the data.
    98      * @param      b     the data.
    99      * @param      off   the start offset in the data.
    99      * @param      off   the start offset in the data.
   100      * @param      len   the number of bytes to write.
   100      * @param      len   the number of bytes to write.
   101      * @throws     IOException  if an I/O error occurs.
   101      * @throws     IOException  if an I/O error occurs.
   110 
   110 
   111     /**
   111     /**
   112      * Flushes this data output stream. This forces any buffered output
   112      * Flushes this data output stream. This forces any buffered output
   113      * bytes to be written out to the stream.
   113      * bytes to be written out to the stream.
   114      * <p>
   114      * <p>
   115      * The <code>flush</code> method of <code>DataOutputStream</code>
   115      * The {@code flush} method of {@code DataOutputStream}
   116      * calls the <code>flush</code> method of its underlying output stream.
   116      * calls the {@code flush} method of its underlying output stream.
   117      *
   117      *
   118      * @throws     IOException  if an I/O error occurs.
   118      * @throws     IOException  if an I/O error occurs.
   119      * @see        java.io.FilterOutputStream#out
   119      * @see        java.io.FilterOutputStream#out
   120      * @see        java.io.OutputStream#flush()
   120      * @see        java.io.OutputStream#flush()
   121      */
   121      */
   122     public void flush() throws IOException {
   122     public void flush() throws IOException {
   123         out.flush();
   123         out.flush();
   124     }
   124     }
   125 
   125 
   126     /**
   126     /**
   127      * Writes a <code>boolean</code> to the underlying output stream as
   127      * Writes a {@code boolean} to the underlying output stream as
   128      * a 1-byte value. The value <code>true</code> is written out as the
   128      * a 1-byte value. The value {@code true} is written out as the
   129      * value <code>(byte)1</code>; the value <code>false</code> is
   129      * value {@code (byte)1}; the value {@code false} is
   130      * written out as the value <code>(byte)0</code>. If no exception is
   130      * written out as the value {@code (byte)0}. If no exception is
   131      * thrown, the counter <code>written</code> is incremented by
   131      * thrown, the counter {@code written} is incremented by
   132      * <code>1</code>.
   132      * {@code 1}.
   133      *
   133      *
   134      * @param      v   a <code>boolean</code> value to be written.
   134      * @param      v   a {@code boolean} value to be written.
   135      * @throws     IOException  if an I/O error occurs.
   135      * @throws     IOException  if an I/O error occurs.
   136      * @see        java.io.FilterOutputStream#out
   136      * @see        java.io.FilterOutputStream#out
   137      */
   137      */
   138     public final void writeBoolean(boolean v) throws IOException {
   138     public final void writeBoolean(boolean v) throws IOException {
   139         out.write(v ? 1 : 0);
   139         out.write(v ? 1 : 0);
   140         incCount(1);
   140         incCount(1);
   141     }
   141     }
   142 
   142 
   143     /**
   143     /**
   144      * Writes out a <code>byte</code> to the underlying output stream as
   144      * Writes out a {@code byte} to the underlying output stream as
   145      * a 1-byte value. If no exception is thrown, the counter
   145      * a 1-byte value. If no exception is thrown, the counter
   146      * <code>written</code> is incremented by <code>1</code>.
   146      * {@code written} is incremented by {@code 1}.
   147      *
   147      *
   148      * @param      v   a <code>byte</code> value to be written.
   148      * @param      v   a {@code byte} value to be written.
   149      * @throws     IOException  if an I/O error occurs.
   149      * @throws     IOException  if an I/O error occurs.
   150      * @see        java.io.FilterOutputStream#out
   150      * @see        java.io.FilterOutputStream#out
   151      */
   151      */
   152     public final void writeByte(int v) throws IOException {
   152     public final void writeByte(int v) throws IOException {
   153         out.write(v);
   153         out.write(v);
   154         incCount(1);
   154         incCount(1);
   155     }
   155     }
   156 
   156 
   157     /**
   157     /**
   158      * Writes a <code>short</code> to the underlying output stream as two
   158      * Writes a {@code short} to the underlying output stream as two
   159      * bytes, high byte first. If no exception is thrown, the counter
   159      * bytes, high byte first. If no exception is thrown, the counter
   160      * <code>written</code> is incremented by <code>2</code>.
   160      * {@code written} is incremented by {@code 2}.
   161      *
   161      *
   162      * @param      v   a <code>short</code> to be written.
   162      * @param      v   a {@code short} to be written.
   163      * @throws     IOException  if an I/O error occurs.
   163      * @throws     IOException  if an I/O error occurs.
   164      * @see        java.io.FilterOutputStream#out
   164      * @see        java.io.FilterOutputStream#out
   165      */
   165      */
   166     public final void writeShort(int v) throws IOException {
   166     public final void writeShort(int v) throws IOException {
   167         out.write((v >>> 8) & 0xFF);
   167         out.write((v >>> 8) & 0xFF);
   168         out.write((v >>> 0) & 0xFF);
   168         out.write((v >>> 0) & 0xFF);
   169         incCount(2);
   169         incCount(2);
   170     }
   170     }
   171 
   171 
   172     /**
   172     /**
   173      * Writes a <code>char</code> to the underlying output stream as a
   173      * Writes a {@code char} to the underlying output stream as a
   174      * 2-byte value, high byte first. If no exception is thrown, the
   174      * 2-byte value, high byte first. If no exception is thrown, the
   175      * counter <code>written</code> is incremented by <code>2</code>.
   175      * counter {@code written} is incremented by {@code 2}.
   176      *
   176      *
   177      * @param      v   a <code>char</code> value to be written.
   177      * @param      v   a {@code char} value to be written.
   178      * @throws     IOException  if an I/O error occurs.
   178      * @throws     IOException  if an I/O error occurs.
   179      * @see        java.io.FilterOutputStream#out
   179      * @see        java.io.FilterOutputStream#out
   180      */
   180      */
   181     public final void writeChar(int v) throws IOException {
   181     public final void writeChar(int v) throws IOException {
   182         out.write((v >>> 8) & 0xFF);
   182         out.write((v >>> 8) & 0xFF);
   183         out.write((v >>> 0) & 0xFF);
   183         out.write((v >>> 0) & 0xFF);
   184         incCount(2);
   184         incCount(2);
   185     }
   185     }
   186 
   186 
   187     /**
   187     /**
   188      * Writes an <code>int</code> to the underlying output stream as four
   188      * Writes an {@code int} to the underlying output stream as four
   189      * bytes, high byte first. If no exception is thrown, the counter
   189      * bytes, high byte first. If no exception is thrown, the counter
   190      * <code>written</code> is incremented by <code>4</code>.
   190      * {@code written} is incremented by {@code 4}.
   191      *
   191      *
   192      * @param      v   an <code>int</code> to be written.
   192      * @param      v   an {@code int} to be written.
   193      * @throws     IOException  if an I/O error occurs.
   193      * @throws     IOException  if an I/O error occurs.
   194      * @see        java.io.FilterOutputStream#out
   194      * @see        java.io.FilterOutputStream#out
   195      */
   195      */
   196     public final void writeInt(int v) throws IOException {
   196     public final void writeInt(int v) throws IOException {
   197         out.write((v >>> 24) & 0xFF);
   197         out.write((v >>> 24) & 0xFF);
   202     }
   202     }
   203 
   203 
   204     private byte writeBuffer[] = new byte[8];
   204     private byte writeBuffer[] = new byte[8];
   205 
   205 
   206     /**
   206     /**
   207      * Writes a <code>long</code> to the underlying output stream as eight
   207      * Writes a {@code long} to the underlying output stream as eight
   208      * bytes, high byte first. In no exception is thrown, the counter
   208      * bytes, high byte first. In no exception is thrown, the counter
   209      * <code>written</code> is incremented by <code>8</code>.
   209      * {@code written} is incremented by {@code 8}.
   210      *
   210      *
   211      * @param      v   a <code>long</code> to be written.
   211      * @param      v   a {@code long} to be written.
   212      * @throws     IOException  if an I/O error occurs.
   212      * @throws     IOException  if an I/O error occurs.
   213      * @see        java.io.FilterOutputStream#out
   213      * @see        java.io.FilterOutputStream#out
   214      */
   214      */
   215     public final void writeLong(long v) throws IOException {
   215     public final void writeLong(long v) throws IOException {
   216         writeBuffer[0] = (byte)(v >>> 56);
   216         writeBuffer[0] = (byte)(v >>> 56);
   224         out.write(writeBuffer, 0, 8);
   224         out.write(writeBuffer, 0, 8);
   225         incCount(8);
   225         incCount(8);
   226     }
   226     }
   227 
   227 
   228     /**
   228     /**
   229      * Converts the float argument to an <code>int</code> using the
   229      * Converts the float argument to an {@code int} using the
   230      * <code>floatToIntBits</code> method in class <code>Float</code>,
   230      * {@code floatToIntBits} method in class {@code Float},
   231      * and then writes that <code>int</code> value to the underlying
   231      * and then writes that {@code int} value to the underlying
   232      * output stream as a 4-byte quantity, high byte first. If no
   232      * output stream as a 4-byte quantity, high byte first. If no
   233      * exception is thrown, the counter <code>written</code> is
   233      * exception is thrown, the counter {@code written} is
   234      * incremented by <code>4</code>.
   234      * incremented by {@code 4}.
   235      *
   235      *
   236      * @param      v   a <code>float</code> value to be written.
   236      * @param      v   a {@code float} value to be written.
   237      * @throws     IOException  if an I/O error occurs.
   237      * @throws     IOException  if an I/O error occurs.
   238      * @see        java.io.FilterOutputStream#out
   238      * @see        java.io.FilterOutputStream#out
   239      * @see        java.lang.Float#floatToIntBits(float)
   239      * @see        java.lang.Float#floatToIntBits(float)
   240      */
   240      */
   241     public final void writeFloat(float v) throws IOException {
   241     public final void writeFloat(float v) throws IOException {
   242         writeInt(Float.floatToIntBits(v));
   242         writeInt(Float.floatToIntBits(v));
   243     }
   243     }
   244 
   244 
   245     /**
   245     /**
   246      * Converts the double argument to a <code>long</code> using the
   246      * Converts the double argument to a {@code long} using the
   247      * <code>doubleToLongBits</code> method in class <code>Double</code>,
   247      * {@code doubleToLongBits} method in class {@code Double},
   248      * and then writes that <code>long</code> value to the underlying
   248      * and then writes that {@code long} value to the underlying
   249      * output stream as an 8-byte quantity, high byte first. If no
   249      * output stream as an 8-byte quantity, high byte first. If no
   250      * exception is thrown, the counter <code>written</code> is
   250      * exception is thrown, the counter {@code written} is
   251      * incremented by <code>8</code>.
   251      * incremented by {@code 8}.
   252      *
   252      *
   253      * @param      v   a <code>double</code> value to be written.
   253      * @param      v   a {@code double} value to be written.
   254      * @throws     IOException  if an I/O error occurs.
   254      * @throws     IOException  if an I/O error occurs.
   255      * @see        java.io.FilterOutputStream#out
   255      * @see        java.io.FilterOutputStream#out
   256      * @see        java.lang.Double#doubleToLongBits(double)
   256      * @see        java.lang.Double#doubleToLongBits(double)
   257      */
   257      */
   258     public final void writeDouble(double v) throws IOException {
   258     public final void writeDouble(double v) throws IOException {
   261 
   261 
   262     /**
   262     /**
   263      * Writes out the string to the underlying output stream as a
   263      * Writes out the string to the underlying output stream as a
   264      * sequence of bytes. Each character in the string is written out, in
   264      * sequence of bytes. Each character in the string is written out, in
   265      * sequence, by discarding its high eight bits. If no exception is
   265      * sequence, by discarding its high eight bits. If no exception is
   266      * thrown, the counter <code>written</code> is incremented by the
   266      * thrown, the counter {@code written} is incremented by the
   267      * length of <code>s</code>.
   267      * length of {@code s}.
   268      *
   268      *
   269      * @param      s   a string of bytes to be written.
   269      * @param      s   a string of bytes to be written.
   270      * @throws     IOException  if an I/O error occurs.
   270      * @throws     IOException  if an I/O error occurs.
   271      * @see        java.io.FilterOutputStream#out
   271      * @see        java.io.FilterOutputStream#out
   272      */
   272      */
   279     }
   279     }
   280 
   280 
   281     /**
   281     /**
   282      * Writes a string to the underlying output stream as a sequence of
   282      * Writes a string to the underlying output stream as a sequence of
   283      * characters. Each character is written to the data output stream as
   283      * characters. Each character is written to the data output stream as
   284      * if by the <code>writeChar</code> method. If no exception is
   284      * if by the {@code writeChar} method. If no exception is
   285      * thrown, the counter <code>written</code> is incremented by twice
   285      * thrown, the counter {@code written} is incremented by twice
   286      * the length of <code>s</code>.
   286      * the length of {@code s}.
   287      *
   287      *
   288      * @param      s   a <code>String</code> value to be written.
   288      * @param      s   a {@code String} value to be written.
   289      * @throws     IOException  if an I/O error occurs.
   289      * @throws     IOException  if an I/O error occurs.
   290      * @see        java.io.DataOutputStream#writeChar(int)
   290      * @see        java.io.DataOutputStream#writeChar(int)
   291      * @see        java.io.FilterOutputStream#out
   291      * @see        java.io.FilterOutputStream#out
   292      */
   292      */
   293     public final void writeChars(String s) throws IOException {
   293     public final void writeChars(String s) throws IOException {
   304      * Writes a string to the underlying output stream using
   304      * Writes a string to the underlying output stream using
   305      * <a href="DataInput.html#modified-utf-8">modified UTF-8</a>
   305      * <a href="DataInput.html#modified-utf-8">modified UTF-8</a>
   306      * encoding in a machine-independent manner.
   306      * encoding in a machine-independent manner.
   307      * <p>
   307      * <p>
   308      * First, two bytes are written to the output stream as if by the
   308      * First, two bytes are written to the output stream as if by the
   309      * <code>writeShort</code> method giving the number of bytes to
   309      * {@code writeShort} method giving the number of bytes to
   310      * follow. This value is the number of bytes actually written out,
   310      * follow. This value is the number of bytes actually written out,
   311      * not the length of the string. Following the length, each character
   311      * not the length of the string. Following the length, each character
   312      * of the string is output, in sequence, using the modified UTF-8 encoding
   312      * of the string is output, in sequence, using the modified UTF-8 encoding
   313      * for the character. If no exception is thrown, the counter
   313      * for the character. If no exception is thrown, the counter
   314      * <code>written</code> is incremented by the total number of
   314      * {@code written} is incremented by the total number of
   315      * bytes written to the output stream. This will be at least two
   315      * bytes written to the output stream. This will be at least two
   316      * plus the length of <code>str</code>, and at most two plus
   316      * plus the length of {@code str}, and at most two plus
   317      * thrice the length of <code>str</code>.
   317      * thrice the length of {@code str}.
   318      *
   318      *
   319      * @param      str   a string to be written.
   319      * @param      str   a string to be written.
   320      * @throws     UTFDataFormatException  if the modified UTF-8 encoding of
   320      * @throws     UTFDataFormatException  if the modified UTF-8 encoding of
   321      *             {@code str} would exceed 65535 bytes in length
   321      *             {@code str} would exceed 65535 bytes in length
   322      * @throws     IOException  if some other I/O error occurs.
   322      * @throws     IOException  if some other I/O error occurs.
   329     /**
   329     /**
   330      * Writes a string to the specified DataOutput using
   330      * Writes a string to the specified DataOutput using
   331      * <a href="DataInput.html#modified-utf-8">modified UTF-8</a>
   331      * <a href="DataInput.html#modified-utf-8">modified UTF-8</a>
   332      * encoding in a machine-independent manner.
   332      * encoding in a machine-independent manner.
   333      * <p>
   333      * <p>
   334      * First, two bytes are written to out as if by the <code>writeShort</code>
   334      * First, two bytes are written to out as if by the {@code writeShort}
   335      * method giving the number of bytes to follow. This value is the number of
   335      * method giving the number of bytes to follow. This value is the number of
   336      * bytes actually written out, not the length of the string. Following the
   336      * bytes actually written out, not the length of the string. Following the
   337      * length, each character of the string is output, in sequence, using the
   337      * length, each character of the string is output, in sequence, using the
   338      * modified UTF-8 encoding for the character. If no exception is thrown, the
   338      * modified UTF-8 encoding for the character. If no exception is thrown, the
   339      * counter <code>written</code> is incremented by the total number of
   339      * counter {@code written} is incremented by the total number of
   340      * bytes written to the output stream. This will be at least two
   340      * bytes written to the output stream. This will be at least two
   341      * plus the length of <code>str</code>, and at most two plus
   341      * plus the length of {@code str}, and at most two plus
   342      * thrice the length of <code>str</code>.
   342      * thrice the length of {@code str}.
   343      *
   343      *
   344      * @param      str   a string to be written.
   344      * @param      str   a string to be written.
   345      * @param      out   destination to write to
   345      * @param      out   destination to write to
   346      * @return     The number of bytes written out.
   346      * @return     The number of bytes written out.
   347      * @throws     UTFDataFormatException  if the modified UTF-8 encoding of
   347      * @throws     UTFDataFormatException  if the modified UTF-8 encoding of
   408         return "encoded string (" + head + "..." + tail + ") too long: "
   408         return "encoded string (" + head + "..." + tail + ") too long: "
   409             + actualLength + " bytes";
   409             + actualLength + " bytes";
   410     }
   410     }
   411 
   411 
   412     /**
   412     /**
   413      * Returns the current value of the counter <code>written</code>,
   413      * Returns the current value of the counter {@code written},
   414      * the number of bytes written to this data output stream so far.
   414      * the number of bytes written to this data output stream so far.
   415      * If the counter overflows, it will be wrapped to Integer.MAX_VALUE.
   415      * If the counter overflows, it will be wrapped to Integer.MAX_VALUE.
   416      *
   416      *
   417      * @return  the value of the <code>written</code> field.
   417      * @return  the value of the {@code written} field.
   418      * @see     java.io.DataOutputStream#written
   418      * @see     java.io.DataOutputStream#written
   419      */
   419      */
   420     public final int size() {
   420     public final int size() {
   421         return written;
   421         return written;
   422     }
   422     }