KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > io > DataOutput


1 /*
2  * @(#)DataOutput.java 1.21 04/05/13
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.io;
9
10 /**
11  * The <code>DataOutput</code> interface provides
12  * for converting data from any of the Java
13  * primitive types to a series of bytes and
14  * writing these bytes to a binary stream.
15  * There is also a facility for converting
16  * a <code>String</code> into
17  * <a HREF="DataInput.html#modified-utf-8">modified UTF-8</a>
18  * format and writing the resulting series
19  * of bytes.
20  * <p>
21  * For all the methods in this interface that
22  * write bytes, it is generally true that if
23  * a byte cannot be written for any reason,
24  * an <code>IOException</code> is thrown.
25  *
26  * @author Frank Yellin
27  * @version 1.21, 05/13/04
28  * @see java.io.DataInput
29  * @see java.io.DataOutputStream
30  * @since JDK1.0
31  */

32 public
33 interface DataOutput {
34     /**
35      * Writes to the output stream the eight
36      * low-order bits of the argument <code>b</code>.
37      * The 24 high-order bits of <code>b</code>
38      * are ignored.
39      *
40      * @param b the byte to be written.
41      * @exception IOException if an I/O error occurs.
42      */

43     void write(int b) throws IOException JavaDoc;
44
45     /**
46      * Writes to the output stream all the bytes in array <code>b</code>.
47      * If <code>b</code> is <code>null</code>,
48      * a <code>NullPointerException</code> is thrown.
49      * If <code>b.length</code> is zero, then
50      * no bytes are written. Otherwise, the byte
51      * <code>b[0]</code> is written first, then
52      * <code>b[1]</code>, and so on; the last byte
53      * written is <code>b[b.length-1]</code>.
54      *
55      * @param b the data.
56      * @exception IOException if an I/O error occurs.
57      */

58     void write(byte b[]) throws IOException JavaDoc;
59
60     /**
61      * Writes <code>len</code> bytes from array
62      * <code>b</code>, in order, to
63      * the output stream. If <code>b</code>
64      * is <code>null</code>, a <code>NullPointerException</code>
65      * is thrown. If <code>off</code> is negative,
66      * or <code>len</code> is negative, or <code>off+len</code>
67      * is greater than the length of the array
68      * <code>b</code>, then an <code>IndexOutOfBoundsException</code>
69      * is thrown. If <code>len</code> is zero,
70      * then no bytes are written. Otherwise, the
71      * byte <code>b[off]</code> is written first,
72      * then <code>b[off+1]</code>, and so on; the
73      * last byte written is <code>b[off+len-1]</code>.
74      *
75      * @param b the data.
76      * @param off the start offset in the data.
77      * @param len the number of bytes to write.
78      * @exception IOException if an I/O error occurs.
79      */

80     void write(byte b[], int off, int len) throws IOException JavaDoc;
81
82     /**
83      * Writes a <code>boolean</code> value to this output stream.
84      * If the argument <code>v</code>
85      * is <code>true</code>, the value <code>(byte)1</code>
86      * is written; if <code>v</code> is <code>false</code>,
87      * the value <code>(byte)0</code> is written.
88      * The byte written by this method may
89      * be read by the <code>readBoolean</code>
90      * method of interface <code>DataInput</code>,
91      * which will then return a <code>boolean</code>
92      * equal to <code>v</code>.
93      *
94      * @param v the boolean to be written.
95      * @exception IOException if an I/O error occurs.
96      */

97     void writeBoolean(boolean v) throws IOException JavaDoc;
98
99     /**
100      * Writes to the output stream the eight low-
101      * order bits of the argument <code>v</code>.
102      * The 24 high-order bits of <code>v</code>
103      * are ignored. (This means that <code>writeByte</code>
104      * does exactly the same thing as <code>write</code>
105      * for an integer argument.) The byte written
106      * by this method may be read by the <code>readByte</code>
107      * method of interface <code>DataInput</code>,
108      * which will then return a <code>byte</code>
109      * equal to <code>(byte)v</code>.
110      *
111      * @param v the byte value to be written.
112      * @exception IOException if an I/O error occurs.
113      */

114     void writeByte(int v) throws IOException JavaDoc;
115
116     /**
117      * Writes two bytes to the output
118      * stream to represent the value of the argument.
119      * The byte values to be written, in the order
120      * shown, are: <p>
121      * <pre><code>
122      * (byte)(0xff &amp; (v &gt;&gt; 8))
123      * (byte)(0xff &amp; v)
124      * </code> </pre> <p>
125      * The bytes written by this method may be
126      * read by the <code>readShort</code> method
127      * of interface <code>DataInput</code> , which
128      * will then return a <code>short</code> equal
129      * to <code>(short)v</code>.
130      *
131      * @param v the <code>short</code> value to be written.
132      * @exception IOException if an I/O error occurs.
133      */

134     void writeShort(int v) throws IOException JavaDoc;
135
136     /**
137      * Writes a <code>char</code> value, which
138      * is comprised of two bytes, to the
139      * output stream.
140      * The byte values to be written, in the order
141      * shown, are:
142      * <p><pre><code>
143      * (byte)(0xff &amp; (v &gt;&gt; 8))
144      * (byte)(0xff &amp; v)
145      * </code></pre><p>
146      * The bytes written by this method may be
147      * read by the <code>readChar</code> method
148      * of interface <code>DataInput</code> , which
149      * will then return a <code>char</code> equal
150      * to <code>(char)v</code>.
151      *
152      * @param v the <code>char</code> value to be written.
153      * @exception IOException if an I/O error occurs.
154      */

155     void writeChar(int v) throws IOException JavaDoc;
156
157     /**
158      * Writes an <code>int</code> value, which is
159      * comprised of four bytes, to the output stream.
160      * The byte values to be written, in the order
161      * shown, are:
162      * <p><pre><code>
163      * (byte)(0xff &amp; (v &gt;&gt; 24))
164      * (byte)(0xff &amp; (v &gt;&gt; 16))
165      * (byte)(0xff &amp; (v &gt;&gt; &#32; &#32;8))
166      * (byte)(0xff &amp; v)
167      * </code></pre><p>
168      * The bytes written by this method may be read
169      * by the <code>readInt</code> method of interface
170      * <code>DataInput</code> , which will then
171      * return an <code>int</code> equal to <code>v</code>.
172      *
173      * @param v the <code>int</code> value to be written.
174      * @exception IOException if an I/O error occurs.
175      */

176     void writeInt(int v) throws IOException JavaDoc;
177
178     /**
179      * Writes a <code>long</code> value, which is
180      * comprised of eight bytes, to the output stream.
181      * The byte values to be written, in the order
182      * shown, are:
183      * <p><pre><code>
184      * (byte)(0xff &amp; (v &gt;&gt; 56))
185      * (byte)(0xff &amp; (v &gt;&gt; 48))
186      * (byte)(0xff &amp; (v &gt;&gt; 40))
187      * (byte)(0xff &amp; (v &gt;&gt; 32))
188      * (byte)(0xff &amp; (v &gt;&gt; 24))
189      * (byte)(0xff &amp; (v &gt;&gt; 16))
190      * (byte)(0xff &amp; (v &gt;&gt; 8))
191      * (byte)(0xff &amp; v)
192      * </code></pre><p>
193      * The bytes written by this method may be
194      * read by the <code>readLong</code> method
195      * of interface <code>DataInput</code> , which
196      * will then return a <code>long</code> equal
197      * to <code>v</code>.
198      *
199      * @param v the <code>long</code> value to be written.
200      * @exception IOException if an I/O error occurs.
201      */

202     void writeLong(long v) throws IOException JavaDoc;
203
204     /**
205      * Writes a <code>float</code> value,
206      * which is comprised of four bytes, to the output stream.
207      * It does this as if it first converts this
208      * <code>float</code> value to an <code>int</code>
209      * in exactly the manner of the <code>Float.floatToIntBits</code>
210      * method and then writes the <code>int</code>
211      * value in exactly the manner of the <code>writeInt</code>
212      * method. The bytes written by this method
213      * may be read by the <code>readFloat</code>
214      * method of interface <code>DataInput</code>,
215      * which will then return a <code>float</code>
216      * equal to <code>v</code>.
217      *
218      * @param v the <code>float</code> value to be written.
219      * @exception IOException if an I/O error occurs.
220      */

221     void writeFloat(float v) throws IOException JavaDoc;
222
223     /**
224      * Writes a <code>double</code> value,
225      * which is comprised of eight bytes, to the output stream.
226      * It does this as if it first converts this
227      * <code>double</code> value to a <code>long</code>
228      * in exactly the manner of the <code>Double.doubleToLongBits</code>
229      * method and then writes the <code>long</code>
230      * value in exactly the manner of the <code>writeLong</code>
231      * method. The bytes written by this method
232      * may be read by the <code>readDouble</code>
233      * method of interface <code>DataInput</code>,
234      * which will then return a <code>double</code>
235      * equal to <code>v</code>.
236      *
237      * @param v the <code>double</code> value to be written.
238      * @exception IOException if an I/O error occurs.
239      */

240     void writeDouble(double v) throws IOException JavaDoc;
241
242     /**
243      * Writes a string to the output stream.
244      * For every character in the string
245      * <code>s</code>, taken in order, one byte
246      * is written to the output stream. If
247      * <code>s</code> is <code>null</code>, a <code>NullPointerException</code>
248      * is thrown.<p> If <code>s.length</code>
249      * is zero, then no bytes are written. Otherwise,
250      * the character <code>s[0]</code> is written
251      * first, then <code>s[1]</code>, and so on;
252      * the last character written is <code>s[s.length-1]</code>.
253      * For each character, one byte is written,
254      * the low-order byte, in exactly the manner
255      * of the <code>writeByte</code> method . The
256      * high-order eight bits of each character
257      * in the string are ignored.
258      *
259      * @param s the string of bytes to be written.
260      * @exception IOException if an I/O error occurs.
261      */

262     void writeBytes(String JavaDoc s) throws IOException JavaDoc;
263
264     /**
265      * Writes every character in the string <code>s</code>,
266      * to the output stream, in order,
267      * two bytes per character. If <code>s</code>
268      * is <code>null</code>, a <code>NullPointerException</code>
269      * is thrown. If <code>s.length</code>
270      * is zero, then no characters are written.
271      * Otherwise, the character <code>s[0]</code>
272      * is written first, then <code>s[1]</code>,
273      * and so on; the last character written is
274      * <code>s[s.length-1]</code>. For each character,
275      * two bytes are actually written, high-order
276      * byte first, in exactly the manner of the
277      * <code>writeChar</code> method.
278      *
279      * @param s the string value to be written.
280      * @exception IOException if an I/O error occurs.
281      */

282     void writeChars(String JavaDoc s) throws IOException JavaDoc;
283
284     /**
285      * Writes two bytes of length information
286      * to the output stream, followed
287      * by the
288      * <a HREF="DataInput.html#modified-utf-8">modified UTF-8</a>
289      * representation
290      * of every character in the string <code>s</code>.
291      * If <code>s</code> is <code>null</code>,
292      * a <code>NullPointerException</code> is thrown.
293      * Each character in the string <code>s</code>
294      * is converted to a group of one, two, or
295      * three bytes, depending on the value of the
296      * character.<p>
297      * If a character <code>c</code>
298      * is in the range <code>&#92;u0001</code> through
299      * <code>&#92;u007f</code>, it is represented
300      * by one byte:<p>
301      * <pre>(byte)c </pre> <p>
302      * If a character <code>c</code> is <code>&#92;u0000</code>
303      * or is in the range <code>&#92;u0080</code>
304      * through <code>&#92;u07ff</code>, then it is
305      * represented by two bytes, to be written
306      * in the order shown:<p> <pre><code>
307      * (byte)(0xc0 | (0x1f &amp; (c &gt;&gt; 6)))
308      * (byte)(0x80 | (0x3f &amp; c))
309      * </code></pre> <p> If a character
310      * <code>c</code> is in the range <code>&#92;u0800</code>
311      * through <code>uffff</code>, then it is
312      * represented by three bytes, to be written
313      * in the order shown:<p> <pre><code>
314      * (byte)(0xe0 | (0x0f &amp; (c &gt;&gt; 12)))
315      * (byte)(0x80 | (0x3f &amp; (c &gt;&gt; 6)))
316      * (byte)(0x80 | (0x3f &amp; c))
317      * </code></pre> <p> First,
318      * the total number of bytes needed to represent
319      * all the characters of <code>s</code> is
320      * calculated. If this number is larger than
321      * <code>65535</code>, then a <code>UTFDataFormatException</code>
322      * is thrown. Otherwise, this length is written
323      * to the output stream in exactly the manner
324      * of the <code>writeShort</code> method;
325      * after this, the one-, two-, or three-byte
326      * representation of each character in the
327      * string <code>s</code> is written.<p> The
328      * bytes written by this method may be read
329      * by the <code>readUTF</code> method of interface
330      * <code>DataInput</code> , which will then
331      * return a <code>String</code> equal to <code>s</code>.
332      *
333      * @param str the string value to be written.
334      * @exception IOException if an I/O error occurs.
335      */

336     void writeUTF(String JavaDoc str) throws IOException JavaDoc;
337 }
338
Popular Tags