rt/emul/compact/src/main/java/java/io/DataOutput.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/DataOutput.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) 1995, 2006, 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
 * The <code>DataOutput</code> interface provides
jaroslav@601
    30
 * for converting data from any of the Java
jaroslav@601
    31
 * primitive types to a series of bytes and
jaroslav@601
    32
 * writing these bytes to a binary stream.
jaroslav@601
    33
 * There is  also a facility for converting
jaroslav@601
    34
 * a <code>String</code> into
jaroslav@601
    35
 * <a href="DataInput.html#modified-utf-8">modified UTF-8</a>
jaroslav@601
    36
 * format and writing the resulting series
jaroslav@601
    37
 * of bytes.
jaroslav@601
    38
 * <p>
jaroslav@601
    39
 * For all the methods in this interface that
jaroslav@601
    40
 * write bytes, it is generally true that if
jaroslav@601
    41
 * a byte cannot be written for any reason,
jaroslav@601
    42
 * an <code>IOException</code> is thrown.
jaroslav@601
    43
 *
jaroslav@601
    44
 * @author  Frank Yellin
jaroslav@601
    45
 * @see     java.io.DataInput
jaroslav@601
    46
 * @see     java.io.DataOutputStream
jaroslav@601
    47
 * @since   JDK1.0
jaroslav@601
    48
 */
jaroslav@601
    49
public
jaroslav@601
    50
interface DataOutput {
jaroslav@601
    51
    /**
jaroslav@601
    52
     * Writes to the output stream the eight
jaroslav@601
    53
     * low-order bits of the argument <code>b</code>.
jaroslav@601
    54
     * The 24 high-order  bits of <code>b</code>
jaroslav@601
    55
     * are ignored.
jaroslav@601
    56
     *
jaroslav@601
    57
     * @param      b   the byte to be written.
jaroslav@601
    58
     * @throws     IOException  if an I/O error occurs.
jaroslav@601
    59
     */
jaroslav@601
    60
    void write(int b) throws IOException;
jaroslav@601
    61
jaroslav@601
    62
    /**
jaroslav@601
    63
     * Writes to the output stream all the bytes in array <code>b</code>.
jaroslav@601
    64
     * If <code>b</code> is <code>null</code>,
jaroslav@601
    65
     * a <code>NullPointerException</code> is thrown.
jaroslav@601
    66
     * If <code>b.length</code> is zero, then
jaroslav@601
    67
     * no bytes are written. Otherwise, the byte
jaroslav@601
    68
     * <code>b[0]</code> is written first, then
jaroslav@601
    69
     * <code>b[1]</code>, and so on; the last byte
jaroslav@601
    70
     * written is <code>b[b.length-1]</code>.
jaroslav@601
    71
     *
jaroslav@601
    72
     * @param      b   the data.
jaroslav@601
    73
     * @throws     IOException  if an I/O error occurs.
jaroslav@601
    74
     */
jaroslav@601
    75
    void write(byte b[]) throws IOException;
jaroslav@601
    76
jaroslav@601
    77
    /**
jaroslav@601
    78
     * Writes <code>len</code> bytes from array
jaroslav@601
    79
     * <code>b</code>, in order,  to
jaroslav@601
    80
     * the output stream.  If <code>b</code>
jaroslav@601
    81
     * is <code>null</code>, a <code>NullPointerException</code>
jaroslav@601
    82
     * is thrown.  If <code>off</code> is negative,
jaroslav@601
    83
     * or <code>len</code> is negative, or <code>off+len</code>
jaroslav@601
    84
     * is greater than the length of the array
jaroslav@601
    85
     * <code>b</code>, then an <code>IndexOutOfBoundsException</code>
jaroslav@601
    86
     * is thrown.  If <code>len</code> is zero,
jaroslav@601
    87
     * then no bytes are written. Otherwise, the
jaroslav@601
    88
     * byte <code>b[off]</code> is written first,
jaroslav@601
    89
     * then <code>b[off+1]</code>, and so on; the
jaroslav@601
    90
     * last byte written is <code>b[off+len-1]</code>.
jaroslav@601
    91
     *
jaroslav@601
    92
     * @param      b     the data.
jaroslav@601
    93
     * @param      off   the start offset in the data.
jaroslav@601
    94
     * @param      len   the number of bytes to write.
jaroslav@601
    95
     * @throws     IOException  if an I/O error occurs.
jaroslav@601
    96
     */
jaroslav@601
    97
    void write(byte b[], int off, int len) throws IOException;
jaroslav@601
    98
jaroslav@601
    99
    /**
jaroslav@601
   100
     * Writes a <code>boolean</code> value to this output stream.
jaroslav@601
   101
     * If the argument <code>v</code>
jaroslav@601
   102
     * is <code>true</code>, the value <code>(byte)1</code>
jaroslav@601
   103
     * is written; if <code>v</code> is <code>false</code>,
jaroslav@601
   104
     * the  value <code>(byte)0</code> is written.
jaroslav@601
   105
     * The byte written by this method may
jaroslav@601
   106
     * be read by the <code>readBoolean</code>
jaroslav@601
   107
     * method of interface <code>DataInput</code>,
jaroslav@601
   108
     * which will then return a <code>boolean</code>
jaroslav@601
   109
     * equal to <code>v</code>.
jaroslav@601
   110
     *
jaroslav@601
   111
     * @param      v   the boolean to be written.
jaroslav@601
   112
     * @throws     IOException  if an I/O error occurs.
jaroslav@601
   113
     */
jaroslav@601
   114
    void writeBoolean(boolean v) throws IOException;
jaroslav@601
   115
jaroslav@601
   116
    /**
jaroslav@601
   117
     * Writes to the output stream the eight low-
jaroslav@601
   118
     * order bits of the argument <code>v</code>.
jaroslav@601
   119
     * The 24 high-order bits of <code>v</code>
jaroslav@601
   120
     * are ignored. (This means  that <code>writeByte</code>
jaroslav@601
   121
     * does exactly the same thing as <code>write</code>
jaroslav@601
   122
     * for an integer argument.) The byte written
jaroslav@601
   123
     * by this method may be read by the <code>readByte</code>
jaroslav@601
   124
     * method of interface <code>DataInput</code>,
jaroslav@601
   125
     * which will then return a <code>byte</code>
jaroslav@601
   126
     * equal to <code>(byte)v</code>.
jaroslav@601
   127
     *
jaroslav@601
   128
     * @param      v   the byte value to be written.
jaroslav@601
   129
     * @throws     IOException  if an I/O error occurs.
jaroslav@601
   130
     */
jaroslav@601
   131
    void writeByte(int v) throws IOException;
jaroslav@601
   132
jaroslav@601
   133
    /**
jaroslav@601
   134
     * Writes two bytes to the output
jaroslav@601
   135
     * stream to represent the value of the argument.
jaroslav@601
   136
     * The byte values to be written, in the  order
jaroslav@601
   137
     * shown, are: <p>
jaroslav@601
   138
     * <pre><code>
jaroslav@601
   139
     * (byte)(0xff &amp; (v &gt;&gt; 8))
jaroslav@601
   140
     * (byte)(0xff &amp; v)
jaroslav@601
   141
     * </code> </pre> <p>
jaroslav@601
   142
     * The bytes written by this method may be
jaroslav@601
   143
     * read by the <code>readShort</code> method
jaroslav@601
   144
     * of interface <code>DataInput</code> , which
jaroslav@601
   145
     * will then return a <code>short</code> equal
jaroslav@601
   146
     * to <code>(short)v</code>.
jaroslav@601
   147
     *
jaroslav@601
   148
     * @param      v   the <code>short</code> value to be written.
jaroslav@601
   149
     * @throws     IOException  if an I/O error occurs.
jaroslav@601
   150
     */
jaroslav@601
   151
    void writeShort(int v) throws IOException;
jaroslav@601
   152
jaroslav@601
   153
    /**
jaroslav@601
   154
     * Writes a <code>char</code> value, which
jaroslav@601
   155
     * is comprised of two bytes, to the
jaroslav@601
   156
     * output stream.
jaroslav@601
   157
     * The byte values to be written, in the  order
jaroslav@601
   158
     * shown, are:
jaroslav@601
   159
     * <p><pre><code>
jaroslav@601
   160
     * (byte)(0xff &amp; (v &gt;&gt; 8))
jaroslav@601
   161
     * (byte)(0xff &amp; v)
jaroslav@601
   162
     * </code></pre><p>
jaroslav@601
   163
     * The bytes written by this method may be
jaroslav@601
   164
     * read by the <code>readChar</code> method
jaroslav@601
   165
     * of interface <code>DataInput</code> , which
jaroslav@601
   166
     * will then return a <code>char</code> equal
jaroslav@601
   167
     * to <code>(char)v</code>.
jaroslav@601
   168
     *
jaroslav@601
   169
     * @param      v   the <code>char</code> value to be written.
jaroslav@601
   170
     * @throws     IOException  if an I/O error occurs.
jaroslav@601
   171
     */
jaroslav@601
   172
    void writeChar(int v) throws IOException;
jaroslav@601
   173
jaroslav@601
   174
    /**
jaroslav@601
   175
     * Writes an <code>int</code> value, which is
jaroslav@601
   176
     * comprised of four bytes, to the output stream.
jaroslav@601
   177
     * The byte values to be written, in the  order
jaroslav@601
   178
     * shown, are:
jaroslav@601
   179
     * <p><pre><code>
jaroslav@601
   180
     * (byte)(0xff &amp; (v &gt;&gt; 24))
jaroslav@601
   181
     * (byte)(0xff &amp; (v &gt;&gt; 16))
jaroslav@601
   182
     * (byte)(0xff &amp; (v &gt;&gt; &#32; &#32;8))
jaroslav@601
   183
     * (byte)(0xff &amp; v)
jaroslav@601
   184
     * </code></pre><p>
jaroslav@601
   185
     * The bytes written by this method may be read
jaroslav@601
   186
     * by the <code>readInt</code> method of interface
jaroslav@601
   187
     * <code>DataInput</code> , which will then
jaroslav@601
   188
     * return an <code>int</code> equal to <code>v</code>.
jaroslav@601
   189
     *
jaroslav@601
   190
     * @param      v   the <code>int</code> value to be written.
jaroslav@601
   191
     * @throws     IOException  if an I/O error occurs.
jaroslav@601
   192
     */
jaroslav@601
   193
    void writeInt(int v) throws IOException;
jaroslav@601
   194
jaroslav@601
   195
    /**
jaroslav@601
   196
     * Writes a <code>long</code> value, which is
jaroslav@601
   197
     * comprised of eight bytes, to the output stream.
jaroslav@601
   198
     * The byte values to be written, in the  order
jaroslav@601
   199
     * shown, are:
jaroslav@601
   200
     * <p><pre><code>
jaroslav@601
   201
     * (byte)(0xff &amp; (v &gt;&gt; 56))
jaroslav@601
   202
     * (byte)(0xff &amp; (v &gt;&gt; 48))
jaroslav@601
   203
     * (byte)(0xff &amp; (v &gt;&gt; 40))
jaroslav@601
   204
     * (byte)(0xff &amp; (v &gt;&gt; 32))
jaroslav@601
   205
     * (byte)(0xff &amp; (v &gt;&gt; 24))
jaroslav@601
   206
     * (byte)(0xff &amp; (v &gt;&gt; 16))
jaroslav@601
   207
     * (byte)(0xff &amp; (v &gt;&gt;  8))
jaroslav@601
   208
     * (byte)(0xff &amp; v)
jaroslav@601
   209
     * </code></pre><p>
jaroslav@601
   210
     * The bytes written by this method may be
jaroslav@601
   211
     * read by the <code>readLong</code> method
jaroslav@601
   212
     * of interface <code>DataInput</code> , which
jaroslav@601
   213
     * will then return a <code>long</code> equal
jaroslav@601
   214
     * to <code>v</code>.
jaroslav@601
   215
     *
jaroslav@601
   216
     * @param      v   the <code>long</code> value to be written.
jaroslav@601
   217
     * @throws     IOException  if an I/O error occurs.
jaroslav@601
   218
     */
jaroslav@601
   219
    void writeLong(long v) throws IOException;
jaroslav@601
   220
jaroslav@601
   221
    /**
jaroslav@601
   222
     * Writes a <code>float</code> value,
jaroslav@601
   223
     * which is comprised of four bytes, to the output stream.
jaroslav@601
   224
     * It does this as if it first converts this
jaroslav@601
   225
     * <code>float</code> value to an <code>int</code>
jaroslav@601
   226
     * in exactly the manner of the <code>Float.floatToIntBits</code>
jaroslav@601
   227
     * method  and then writes the <code>int</code>
jaroslav@601
   228
     * value in exactly the manner of the  <code>writeInt</code>
jaroslav@601
   229
     * method.  The bytes written by this method
jaroslav@601
   230
     * may be read by the <code>readFloat</code>
jaroslav@601
   231
     * method of interface <code>DataInput</code>,
jaroslav@601
   232
     * which will then return a <code>float</code>
jaroslav@601
   233
     * equal to <code>v</code>.
jaroslav@601
   234
     *
jaroslav@601
   235
     * @param      v   the <code>float</code> value to be written.
jaroslav@601
   236
     * @throws     IOException  if an I/O error occurs.
jaroslav@601
   237
     */
jaroslav@601
   238
    void writeFloat(float v) throws IOException;
jaroslav@601
   239
jaroslav@601
   240
    /**
jaroslav@601
   241
     * Writes a <code>double</code> value,
jaroslav@601
   242
     * which is comprised of eight bytes, to the output stream.
jaroslav@601
   243
     * It does this as if it first converts this
jaroslav@601
   244
     * <code>double</code> value to a <code>long</code>
jaroslav@601
   245
     * in exactly the manner of the <code>Double.doubleToLongBits</code>
jaroslav@601
   246
     * method  and then writes the <code>long</code>
jaroslav@601
   247
     * value in exactly the manner of the  <code>writeLong</code>
jaroslav@601
   248
     * method. The bytes written by this method
jaroslav@601
   249
     * may be read by the <code>readDouble</code>
jaroslav@601
   250
     * method of interface <code>DataInput</code>,
jaroslav@601
   251
     * which will then return a <code>double</code>
jaroslav@601
   252
     * equal to <code>v</code>.
jaroslav@601
   253
     *
jaroslav@601
   254
     * @param      v   the <code>double</code> value to be written.
jaroslav@601
   255
     * @throws     IOException  if an I/O error occurs.
jaroslav@601
   256
     */
jaroslav@601
   257
    void writeDouble(double v) throws IOException;
jaroslav@601
   258
jaroslav@601
   259
    /**
jaroslav@601
   260
     * Writes a string to the output stream.
jaroslav@601
   261
     * For every character in the string
jaroslav@601
   262
     * <code>s</code>,  taken in order, one byte
jaroslav@601
   263
     * is written to the output stream.  If
jaroslav@601
   264
     * <code>s</code> is <code>null</code>, a <code>NullPointerException</code>
jaroslav@601
   265
     * is thrown.<p>  If <code>s.length</code>
jaroslav@601
   266
     * is zero, then no bytes are written. Otherwise,
jaroslav@601
   267
     * the character <code>s[0]</code> is written
jaroslav@601
   268
     * first, then <code>s[1]</code>, and so on;
jaroslav@601
   269
     * the last character written is <code>s[s.length-1]</code>.
jaroslav@601
   270
     * For each character, one byte is written,
jaroslav@601
   271
     * the low-order byte, in exactly the manner
jaroslav@601
   272
     * of the <code>writeByte</code> method . The
jaroslav@601
   273
     * high-order eight bits of each character
jaroslav@601
   274
     * in the string are ignored.
jaroslav@601
   275
     *
jaroslav@601
   276
     * @param      s   the string of bytes to be written.
jaroslav@601
   277
     * @throws     IOException  if an I/O error occurs.
jaroslav@601
   278
     */
jaroslav@601
   279
    void writeBytes(String s) throws IOException;
jaroslav@601
   280
jaroslav@601
   281
    /**
jaroslav@601
   282
     * Writes every character in the string <code>s</code>,
jaroslav@601
   283
     * to the output stream, in order,
jaroslav@601
   284
     * two bytes per character. If <code>s</code>
jaroslav@601
   285
     * is <code>null</code>, a <code>NullPointerException</code>
jaroslav@601
   286
     * is thrown.  If <code>s.length</code>
jaroslav@601
   287
     * is zero, then no characters are written.
jaroslav@601
   288
     * Otherwise, the character <code>s[0]</code>
jaroslav@601
   289
     * is written first, then <code>s[1]</code>,
jaroslav@601
   290
     * and so on; the last character written is
jaroslav@601
   291
     * <code>s[s.length-1]</code>. For each character,
jaroslav@601
   292
     * two bytes are actually written, high-order
jaroslav@601
   293
     * byte first, in exactly the manner of the
jaroslav@601
   294
     * <code>writeChar</code> method.
jaroslav@601
   295
     *
jaroslav@601
   296
     * @param      s   the string value to be written.
jaroslav@601
   297
     * @throws     IOException  if an I/O error occurs.
jaroslav@601
   298
     */
jaroslav@601
   299
    void writeChars(String s) throws IOException;
jaroslav@601
   300
jaroslav@601
   301
    /**
jaroslav@601
   302
     * Writes two bytes of length information
jaroslav@601
   303
     * to the output stream, followed
jaroslav@601
   304
     * by the
jaroslav@601
   305
     * <a href="DataInput.html#modified-utf-8">modified UTF-8</a>
jaroslav@601
   306
     * representation
jaroslav@601
   307
     * of  every character in the string <code>s</code>.
jaroslav@601
   308
     * If <code>s</code> is <code>null</code>,
jaroslav@601
   309
     * a <code>NullPointerException</code> is thrown.
jaroslav@601
   310
     * Each character in the string <code>s</code>
jaroslav@601
   311
     * is converted to a group of one, two, or
jaroslav@601
   312
     * three bytes, depending on the value of the
jaroslav@601
   313
     * character.<p>
jaroslav@601
   314
     * If a character <code>c</code>
jaroslav@601
   315
     * is in the range <code>&#92;u0001</code> through
jaroslav@601
   316
     * <code>&#92;u007f</code>, it is represented
jaroslav@601
   317
     * by one byte:<p>
jaroslav@601
   318
     * <pre>(byte)c </pre>  <p>
jaroslav@601
   319
     * If a character <code>c</code> is <code>&#92;u0000</code>
jaroslav@601
   320
     * or is in the range <code>&#92;u0080</code>
jaroslav@601
   321
     * through <code>&#92;u07ff</code>, then it is
jaroslav@601
   322
     * represented by two bytes, to be written
jaroslav@601
   323
     * in the order shown:<p> <pre><code>
jaroslav@601
   324
     * (byte)(0xc0 | (0x1f &amp; (c &gt;&gt; 6)))
jaroslav@601
   325
     * (byte)(0x80 | (0x3f &amp; c))
jaroslav@601
   326
     *  </code></pre>  <p> If a character
jaroslav@601
   327
     * <code>c</code> is in the range <code>&#92;u0800</code>
jaroslav@601
   328
     * through <code>uffff</code>, then it is
jaroslav@601
   329
     * represented by three bytes, to be written
jaroslav@601
   330
     * in the order shown:<p> <pre><code>
jaroslav@601
   331
     * (byte)(0xe0 | (0x0f &amp; (c &gt;&gt; 12)))
jaroslav@601
   332
     * (byte)(0x80 | (0x3f &amp; (c &gt;&gt;  6)))
jaroslav@601
   333
     * (byte)(0x80 | (0x3f &amp; c))
jaroslav@601
   334
     *  </code></pre>  <p> First,
jaroslav@601
   335
     * the total number of bytes needed to represent
jaroslav@601
   336
     * all the characters of <code>s</code> is
jaroslav@601
   337
     * calculated. If this number is larger than
jaroslav@601
   338
     * <code>65535</code>, then a <code>UTFDataFormatException</code>
jaroslav@601
   339
     * is thrown. Otherwise, this length is written
jaroslav@601
   340
     * to the output stream in exactly the manner
jaroslav@601
   341
     * of the <code>writeShort</code> method;
jaroslav@601
   342
     * after this, the one-, two-, or three-byte
jaroslav@601
   343
     * representation of each character in the
jaroslav@601
   344
     * string <code>s</code> is written.<p>  The
jaroslav@601
   345
     * bytes written by this method may be read
jaroslav@601
   346
     * by the <code>readUTF</code> method of interface
jaroslav@601
   347
     * <code>DataInput</code> , which will then
jaroslav@601
   348
     * return a <code>String</code> equal to <code>s</code>.
jaroslav@601
   349
     *
jaroslav@601
   350
     * @param      s   the string value to be written.
jaroslav@601
   351
     * @throws     IOException  if an I/O error occurs.
jaroslav@601
   352
     */
jaroslav@601
   353
    void writeUTF(String s) throws IOException;
jaroslav@601
   354
}