39 |
package java.io; |
package java.io; |
40 |
|
|
41 |
/** |
/** |
42 |
* This class provides a mechanism for writing primitive Java datatypes |
* This class provides a mechanism for writing primitive Java datatypes |
43 |
* to an <code>OutputStream</code> in a portable way. Data written to |
* to an <code>OutputStream</code> in a portable way. Data written to |
44 |
* a stream using this class can be read back in using the |
* a stream using this class can be read back in using the |
45 |
* <code>DataInputStream</code> class on any platform. |
* <code>DataInputStream</code> class on any platform. |
46 |
* |
* |
47 |
* @see DataInputStream |
* @see DataInputStream |
48 |
* |
* |
49 |
* @author Aaron M. Renn (arenn@urbanophile.com) |
* @author Aaron M. Renn (arenn@urbanophile.com) |
50 |
* @author Tom Tromey <tromey@cygnus.com> |
* @author Tom Tromey <tromey@cygnus.com> |
51 |
*/ |
*/ |
52 |
public class DataOutputStream extends FilterOutputStream implements DataOutput |
public class DataOutputStream extends FilterOutputStream implements DataOutput |
53 |
{ |
{ |
54 |
|
|
93 |
return(os.toByteArray()); |
return(os.toByteArray()); |
94 |
} |
} |
95 |
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/* |
|
|
* Instance Variables |
|
|
*/ |
|
|
|
|
96 |
/** |
/** |
97 |
* This is the total number of bytes that have been written to the |
* This is the total number of bytes that have been written to the |
98 |
* stream by this object instance. |
* stream by this object instance. |
|
*/ |
|
|
protected int written = 0; |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/* |
|
|
* Constructors |
|
99 |
*/ |
*/ |
100 |
|
protected int written = 0; |
101 |
|
|
102 |
/** |
/** |
103 |
* This method initializes an instance of <code>DataOutputStream</code> to |
* This method initializes an instance of <code>DataOutputStream</code> to |
104 |
* write its data to the specified underlying <code>OutputStream</code> |
* write its data to the specified underlying <code>OutputStream</code> |
105 |
* |
* |
106 |
* @param out The subordinate <code>OutputStream</code> to which this |
* @param out The subordinate <code>OutputStream</code> to which this |
107 |
* object will write |
* object will write |
108 |
*/ |
*/ |
109 |
public DataOutputStream(OutputStream out) |
public DataOutputStream(OutputStream out) |
110 |
{ |
{ |
111 |
super(out); |
super(out); |
112 |
} |
} |
113 |
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/* |
|
|
* Instance Methods |
|
|
*/ |
|
|
|
|
114 |
/** |
/** |
115 |
* This method returns the total number of bytes that have been written to |
* This method returns the total number of bytes that have been written to |
116 |
* the underlying output stream so far. This is the value of the |
* the underlying output stream so far. This is the value of the |
117 |
* <code>written</code> instance variable |
* <code>written</code> instance variable |
118 |
* |
* |
119 |
* @return The number of bytes written to the stream. |
* @return The number of bytes written to the stream. |
120 |
*/ |
*/ |
121 |
public final int size() |
public final int size() |
122 |
{ |
{ |
123 |
return(written); |
return(written); |
124 |
} |
} |
125 |
|
|
|
/*************************************************************************/ |
|
|
|
|
126 |
/** |
/** |
127 |
* This method writes a Java <code>boolean</code> to the underlying output |
* This method writes a Java <code>boolean</code> to the underlying output |
128 |
* stream. For a value of <code>true</code>, 1 is written to the stream. |
* stream. For a value of <code>true</code>, 1 is written to the stream. |
129 |
* For a value of <code>false</code>, 0 is written. |
* For a value of <code>false</code>, 0 is written. |
130 |
* |
* |
131 |
* @param b The <code>boolean</code> value to write to the stream |
* @param b The <code>boolean</code> value to write to the stream |
132 |
* |
* |
133 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
134 |
*/ |
*/ |
135 |
public final void writeBoolean(boolean b) throws IOException |
public final void writeBoolean(boolean b) throws IOException |
136 |
{ |
{ |
137 |
if (b) |
if (b) |
140 |
write(0); |
write(0); |
141 |
} |
} |
142 |
|
|
|
/*************************************************************************/ |
|
|
|
|
143 |
/** |
/** |
144 |
* This method writes a Java <code>byte</code> value to the underlying |
* This method writes a Java <code>byte</code> value to the underlying |
145 |
* output stream. |
* output stream. |
146 |
* |
* |
147 |
* @param b The <code>byte</code> to write to the stream, passed as |
* @param b The <code>byte</code> to write to the stream, passed as |
148 |
* the low eight bits of an <code>int</code>. |
* the low eight bits of an <code>int</code>. |
149 |
* |
* |
150 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
151 |
*/ |
*/ |
152 |
public final void writeByte(int b) throws IOException |
public final void writeByte(int b) throws IOException |
153 |
{ |
{ |
154 |
write(b & 0xFF); |
write(b & 0xFF); |
155 |
} |
} |
156 |
|
|
|
/*************************************************************************/ |
|
|
|
|
157 |
/** |
/** |
158 |
* This method writes all the bytes in a <code>String</code> out to the |
* This method writes all the bytes in a <code>String</code> out to the |
159 |
* stream. One byte is written for each character in the |
* stream. One byte is written for each character in the |
160 |
* <code>String</code>. |
* <code>String</code>. |
161 |
* The high eight bits of each character are discarded. |
* The high eight bits of each character are discarded. |
162 |
* |
* |
163 |
* @param s The <code>String</code> to write to the stream |
* @param s The <code>String</code> to write to the stream |
164 |
* |
* |
165 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
166 |
*/ |
*/ |
167 |
public final void writeBytes(String s) throws IOException |
public final void writeBytes(String s) throws IOException |
168 |
{ |
{ |
169 |
int len = s.length(); |
int len = s.length(); |
178 |
write(buf, 0, buf.length); |
write(buf, 0, buf.length); |
179 |
} |
} |
180 |
|
|
|
/*************************************************************************/ |
|
|
|
|
181 |
/** |
/** |
182 |
* This method writes a single <code>char</code> value to the stream, |
* This method writes a single <code>char</code> value to the stream, |
183 |
* high byte first. |
* high byte first. |
184 |
* |
* |
185 |
* @param c The <code>char</code> value to write, |
* @param c The <code>char</code> value to write, |
186 |
* passed as an <code>int</code>. |
* passed as an <code>int</code>. |
187 |
* |
* |
188 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
189 |
*/ |
*/ |
190 |
public final synchronized void writeChar(int c) throws IOException |
public final synchronized void writeChar(int c) throws IOException |
191 |
{ |
{ |
192 |
buf[0] = (byte)((c & 0xFF00) >> 8); |
buf[0] = (byte)((c & 0xFF00) >> 8); |
195 |
write(buf, 0, 2); |
write(buf, 0, 2); |
196 |
} |
} |
197 |
|
|
|
/*************************************************************************/ |
|
|
|
|
198 |
/** |
/** |
199 |
* This method writes all the characters in a <code>String</code> to the |
* This method writes all the characters in a <code>String</code> to the |
200 |
* stream. There will be two bytes for each character value. The high |
* stream. There will be two bytes for each character value. The high |
201 |
* byte of the character will be written first. |
* byte of the character will be written first. |
202 |
* |
* |
203 |
* @param s The <code>String</code> to write to the stream. |
* @param s The <code>String</code> to write to the stream. |
204 |
* |
* |
205 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
206 |
*/ |
*/ |
207 |
public final void writeChars(String s) throws IOException |
public final void writeChars(String s) throws IOException |
208 |
{ |
{ |
209 |
int len = s.length(); |
int len = s.length(); |
221 |
write(buf, 0, buf.length); |
write(buf, 0, buf.length); |
222 |
} |
} |
223 |
|
|
|
/*************************************************************************/ |
|
|
|
|
224 |
/** |
/** |
225 |
* This method writes a Java <code>short</code> to the stream, high byte |
* This method writes a Java <code>short</code> to the stream, high byte |
226 |
* first. This method requires two bytes to encode the value. |
* first. This method requires two bytes to encode the value. |
227 |
* |
* |
228 |
* @param s The <code>short</code> value to write to the stream, |
* @param s The <code>short</code> value to write to the stream, |
229 |
* passed as an <code>int</code>. |
* passed as an <code>int</code>. |
230 |
* |
* |
231 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
232 |
*/ |
*/ |
233 |
public final synchronized void writeShort(int s) throws IOException |
public final synchronized void writeShort(int s) throws IOException |
234 |
{ |
{ |
235 |
buf[0] = (byte)((s & 0xFF00) >> 8); |
buf[0] = (byte)((s & 0xFF00) >> 8); |
238 |
write(buf, 0, 2); |
write(buf, 0, 2); |
239 |
} |
} |
240 |
|
|
|
/*************************************************************************/ |
|
|
|
|
241 |
/** |
/** |
242 |
* This method writes a Java <code>int</code> to the stream, high bytes |
* This method writes a Java <code>int</code> to the stream, high bytes |
243 |
* first. This method requires four bytes to encode the value. |
* first. This method requires four bytes to encode the value. |
244 |
* |
* |
245 |
* @param i The <code>int</code> value to write to the stream. |
* @param i The <code>int</code> value to write to the stream. |
246 |
* |
* |
247 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
248 |
*/ |
*/ |
249 |
public final synchronized void writeInt(int i) throws IOException |
public final synchronized void writeInt(int i) throws IOException |
250 |
{ |
{ |
251 |
buf[0] = (byte)((i & 0xFF000000) >> 24); |
buf[0] = (byte)((i & 0xFF000000) >> 24); |
256 |
write(buf, 0, 4); |
write(buf, 0, 4); |
257 |
} |
} |
258 |
|
|
|
/*************************************************************************/ |
|
|
|
|
259 |
/** |
/** |
260 |
* This method writes a Java <code>long</code> to the stream, high bytes |
* This method writes a Java <code>long</code> to the stream, high bytes |
261 |
* first. This method requires eight bytes to encode the value. |
* first. This method requires eight bytes to encode the value. |
262 |
* |
* |
263 |
* @param l The <code>long</code> value to write to the stream. |
* @param l The <code>long</code> value to write to the stream. |
264 |
* |
* |
265 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
266 |
*/ |
*/ |
267 |
public final synchronized void writeLong(long l) throws IOException |
public final synchronized void writeLong(long l) throws IOException |
268 |
{ |
{ |
269 |
buf[0] = (byte)((l & 0xFF00000000000000L) >> 56); |
buf[0] = (byte)((l & 0xFF00000000000000L) >> 56); |
278 |
write(buf, 0, 8); |
write(buf, 0, 8); |
279 |
} |
} |
280 |
|
|
|
/*************************************************************************/ |
|
|
|
|
281 |
/** |
/** |
282 |
* This method writes a Java <code>float</code> value to the stream. This |
* This method writes a Java <code>float</code> value to the stream. This |
283 |
* value is written by first calling the method |
* value is written by first calling the method |
284 |
* <code>Float.floatToIntBits</code> |
* <code>Float.floatToIntBits</code> |
285 |
* to retrieve an <code>int</code> representing the floating point number, |
* to retrieve an <code>int</code> representing the floating point number, |
286 |
* then writing this <code>int</code> value to the stream exactly the same |
* then writing this <code>int</code> value to the stream exactly the same |
287 |
* as the <code>writeInt()</code> method does. |
* as the <code>writeInt()</code> method does. |
288 |
* |
* |
289 |
* @param f The floating point number to write to the stream. |
* @param f The floating point number to write to the stream. |
290 |
* |
* |
291 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
292 |
* |
* |
293 |
* @see writeInt |
* @see #writeInt(int) |
294 |
*/ |
*/ |
295 |
public final void writeFloat(float f) throws IOException |
public final void writeFloat(float f) throws IOException |
296 |
{ |
{ |
297 |
writeInt(Float.floatToIntBits(f)); |
writeInt(Float.floatToIntBits(f)); |
298 |
} |
} |
299 |
|
|
|
/*************************************************************************/ |
|
|
|
|
300 |
/** |
/** |
301 |
* This method writes a Java <code>double</code> value to the stream. This |
* This method writes a Java <code>double</code> value to the stream. This |
302 |
* value is written by first calling the method |
* value is written by first calling the method |
303 |
* <code>Double.doubleToLongBits</code> |
* <code>Double.doubleToLongBits</code> |
304 |
* to retrieve an <code>long</code> representing the floating point number, |
* to retrieve an <code>long</code> representing the floating point number, |
305 |
* then writing this <code>long</code> value to the stream exactly the same |
* then writing this <code>long</code> value to the stream exactly the same |
306 |
* as the <code>writeLong()</code> method does. |
* as the <code>writeLong()</code> method does. |
307 |
* |
* |
308 |
* @param d The double precision floating point number to write to |
* @param d The double precision floating point number to write to |
309 |
* the stream. |
* the stream. |
310 |
* |
* |
311 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
312 |
* |
* |
313 |
* @see writeLong |
* @see #writeLong(long) |
314 |
*/ |
*/ |
315 |
public final void writeDouble(double d) throws IOException |
public final void writeDouble(double d) throws IOException |
316 |
{ |
{ |
317 |
writeLong(Double.doubleToLongBits(d)); |
writeLong(Double.doubleToLongBits(d)); |
318 |
} |
} |
319 |
|
|
|
/*************************************************************************/ |
|
|
|
|
320 |
/** |
/** |
321 |
* This method writes a Java <code>String</code> to the stream in a modified |
* This method writes a Java <code>String</code> to the stream in a modified |
322 |
* UTF-8 format. First, two bytes are written to the stream indicating the |
* UTF-8 format. First, two bytes are written to the stream indicating the |
323 |
* number of bytes to follow. Note that this is the number of bytes in the |
* number of bytes to follow. Note that this is the number of bytes in the |
324 |
* encoded <code>String</code> not the <code>String</code> length. Next |
* encoded <code>String</code> not the <code>String</code> length. Next |
325 |
* come the encoded characters. Each character in the <code>String</code> |
* come the encoded characters. Each character in the <code>String</code> |
326 |
* is encoded as either one, two or three bytes. For characters in the |
* is encoded as either one, two or three bytes. For characters in the |
327 |
* range of <code>\u0001</code> to <\u007F>, one byte is used. The character |
* range of <code>\u0001</code> to <\u007F>, one byte is used. The character |
328 |
* value goes into bits 0-7 and bit eight is 0. For characters in the range |
* value goes into bits 0-7 and bit eight is 0. For characters in the range |
329 |
* of <code>\u0080</code> to <code>\u007FF</code>, two bytes are used. Bits |
* of <code>\u0080</code> to <code>\u007FF</code>, two bytes are used. Bits |
330 |
* 6-10 of the character value are encoded bits 0-4 of the first byte, with |
* 6-10 of the character value are encoded bits 0-4 of the first byte, with |
331 |
* the high bytes having a value of "110". Bits 0-5 of the character value |
* the high bytes having a value of "110". Bits 0-5 of the character value |
332 |
* are stored in bits 0-5 of the second byte, with the high bits set to |
* are stored in bits 0-5 of the second byte, with the high bits set to |
333 |
* "10". This type of encoding is also done for the null character |
* "10". This type of encoding is also done for the null character |
334 |
* <code>\u0000</code>. This eliminates any C style NUL character values |
* <code>\u0000</code>. This eliminates any C style NUL character values |
335 |
* in the output. All remaining characters are stored as three bytes. |
* in the output. All remaining characters are stored as three bytes. |
336 |
* Bits 12-15 of the character value are stored in bits 0-3 of the first |
* Bits 12-15 of the character value are stored in bits 0-3 of the first |
337 |
* byte. The high bits of the first bytes are set to "1110". Bits 6-11 |
* byte. The high bits of the first bytes are set to "1110". Bits 6-11 |
338 |
* of the character value are stored in bits 0-5 of the second byte. The |
* of the character value are stored in bits 0-5 of the second byte. The |
339 |
* high bits of the second byte are set to "10". And bits 0-5 of the |
* high bits of the second byte are set to "10". And bits 0-5 of the |
340 |
* character value are stored in bits 0-5 of byte three, with the high bits |
* character value are stored in bits 0-5 of byte three, with the high bits |
341 |
* of that byte set to "10". |
* of that byte set to "10". |
342 |
* |
* |
343 |
* @param s The <code>String</code> to write to the output in UTF format |
* @param s The <code>String</code> to write to the output in UTF format |
344 |
* |
* |
345 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
346 |
*/ |
*/ |
347 |
public synchronized final void writeUTF(String s) throws IOException |
public synchronized final void writeUTF(String s) throws IOException |
348 |
{ |
{ |
349 |
// FIXME: Stylistically, replacing this with s.getBytes("UTF-8") |
// FIXME: Stylistically, replacing this with s.getBytes("UTF-8") |
357 |
write(buf, 0, buf.length); |
write(buf, 0, buf.length); |
358 |
} |
} |
359 |
|
|
|
/*************************************************************************/ |
|
|
|
|
360 |
/** |
/** |
361 |
* This method writes the specified byte (passed as an <code>int</code>) |
* This method writes the specified byte (passed as an <code>int</code>) |
362 |
* to the underlying output stream. |
* to the underlying output stream. |
363 |
* |
* |
364 |
* @param b The byte to write, passed as an <code>int</code>. |
* @param b The byte to write, passed as an <code>int</code>. |
365 |
* |
* |
366 |
* @exception IOException If an error occurs. |
* @exception IOException If an error occurs. |
367 |
*/ |
*/ |
368 |
public synchronized void write(int b) throws IOException |
public synchronized void write(int b) throws IOException |
369 |
{ |
{ |
370 |
out.write(b); |
out.write(b); |
371 |
++written; |
++written; |
372 |
} |
} |
373 |
|
|
|
/*************************************************************************/ |
|
|
|
|
374 |
/** |
/** |
375 |
* This method writes <code>len</code> bytes from the specified byte array |
* This method writes <code>len</code> bytes from the specified byte array |
376 |
* <code>buf</code> starting at position <code>offset</code> into the |
* <code>buf</code> starting at position <code>offset</code> into the |
377 |
* buffer to the underlying output stream. |
* buffer to the underlying output stream. |
378 |
* |
* |
379 |
* @param buf The byte array to write from. |
* @param buf The byte array to write from. |
380 |
* @param offset The index into the byte array to start writing from. |
* @param offset The index into the byte array to start writing from. |
381 |
* @param len The number of bytes to write. |
* @param len The number of bytes to write. |
382 |
* |
* |
383 |
* @exception IOException If an error occurs. |
* @exception IOException If an error occurs. |
384 |
*/ |
*/ |
385 |
public synchronized void write(byte[] buf, int offset, int len) |
public synchronized void write(byte[] buf, int offset, int len) |
386 |
throws IOException |
throws IOException |
387 |
{ |
{ |
389 |
written += len; |
written += len; |
390 |
} |
} |
391 |
|
|
|
/*************************************************************************/ |
|
|
|
|
392 |
/** |
/** |
393 |
* This method flushes any unwritten bytes to the underlying stream. |
* This method flushes any unwritten bytes to the underlying stream. |
394 |
* |
* |
395 |
* @exception IOException If an error occurs. |
* @exception IOException If an error occurs. |
396 |
*/ |
*/ |
397 |
public void flush() throws IOException |
public void flush() throws IOException |
398 |
{ |
{ |
399 |
out.flush(); |
out.flush(); |