43 |
import gnu.java.nio.FileChannelImpl; |
import gnu.java.nio.FileChannelImpl; |
44 |
|
|
45 |
/** |
/** |
46 |
* This class allows reading and writing of files at random locations. |
* This class allows reading and writing of files at random locations. |
47 |
* Most Java I/O classes are either pure sequential input or output. This |
* Most Java I/O classes are either pure sequential input or output. This |
48 |
* class fulfills the need to be able to read the bytes of a file in an |
* class fulfills the need to be able to read the bytes of a file in an |
49 |
* arbitrary order. In addition, this class implements the |
* arbitrary order. In addition, this class implements the |
50 |
* <code>DataInput</code> and <code>DataOutput</code> interfaces to allow |
* <code>DataInput</code> and <code>DataOutput</code> interfaces to allow |
51 |
* the reading and writing of Java primitives. |
* the reading and writing of Java primitives. |
52 |
* |
* |
53 |
* @author Aaron M. Renn (arenn@urbanophile.com) |
* @author Aaron M. Renn (arenn@urbanophile.com) |
54 |
*/ |
*/ |
55 |
public class RandomAccessFile implements DataOutput, DataInput |
public class RandomAccessFile implements DataOutput, DataInput |
56 |
{ |
{ |
|
|
|
|
/* |
|
|
* Instance Variables |
|
|
*/ |
|
|
|
|
57 |
private FileDescriptor fd; |
private FileDescriptor fd; |
58 |
|
|
59 |
/** |
/** |
60 |
* Whether or not this file is open in read only mode |
* Whether or not this file is open in read only mode |
61 |
*/ |
*/ |
62 |
private boolean readOnly; |
private boolean readOnly; |
63 |
|
|
64 |
// Used for DataOutput methods writing values to the underlying file |
// Used for DataOutput methods writing values to the underlying file |
65 |
private byte[] buf = new byte[8]; |
private byte[] buf = new byte[8]; |
66 |
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/* |
|
|
* Constructors |
|
|
*/ |
|
|
|
|
67 |
/** |
/** |
68 |
* This method initializes a new instance of <code>RandomAccessFile</code> |
* This method initializes a new instance of <code>RandomAccessFile</code> |
69 |
* to read from the specified <code>File</code> object with the specified |
* to read from the specified <code>File</code> object with the specified |
70 |
* access mode. The access mode is either "r" for read only access or "rw" |
* access mode. The access mode is either "r" for read only access or "rw" |
71 |
* for read-write access. |
* for read-write access. |
72 |
* <p> |
* <p> |
73 |
* Note that a <code>SecurityManager</code> check is made prior to |
* Note that a <code>SecurityManager</code> check is made prior to |
74 |
* opening the file to determine whether or not this file is allowed to |
* opening the file to determine whether or not this file is allowed to |
75 |
* be read or written. |
* be read or written. |
76 |
* |
* |
77 |
* @param file The <code>File</code> object to read and/or write. |
* @param file The <code>File</code> object to read and/or write. |
78 |
* @param mode "r" for read only or "rw" for read-write access to the file |
* @param mode "r" for read only or "rw" for read-write access to the file |
79 |
* |
* |
80 |
* @exception IllegalArgumentException If <code>mode</code> has an |
* @exception IllegalArgumentException If <code>mode</code> has an |
81 |
* illegal value |
* illegal value |
82 |
* @exception SecurityException If the requested access to the file |
* @exception SecurityException If the requested access to the file |
83 |
* is not allowed |
* is not allowed |
84 |
* @exception IOException If any other error occurs |
* @exception IOException If any other error occurs |
85 |
*/ |
*/ |
86 |
public RandomAccessFile(File file, String mode) throws FileNotFoundException |
public RandomAccessFile(File file, String mode) throws FileNotFoundException |
87 |
{ |
{ |
88 |
this(file.getPath(), mode); |
this(file.getPath(), mode); |
89 |
} |
} |
90 |
|
|
|
/*************************************************************************/ |
|
|
|
|
91 |
/** |
/** |
92 |
* This method initializes a new instance of <code>RandomAccessFile</code> |
* This method initializes a new instance of <code>RandomAccessFile</code> |
93 |
* to read from the specified file name with the specified access mode. |
* to read from the specified file name with the specified access mode. |
94 |
* The access mode is either "r" for read only access or "rw" for read |
* The access mode is either "r" for read only access or "rw" for read |
95 |
* write access. |
* write access. |
96 |
* <p> |
* <p> |
97 |
* Note that a <code>SecurityManager</code> check is made prior to |
* Note that a <code>SecurityManager</code> check is made prior to |
98 |
* opening the file to determine whether or not this file is allowed to |
* opening the file to determine whether or not this file is allowed to |
99 |
* be read or written. |
* be read or written. |
100 |
* |
* |
101 |
* @param name The name of the file to read and/or write |
* @param name The name of the file to read and/or write |
102 |
* @param mode "r" for read only or "rw" for read-write access to the file |
* @param mode "r" for read only or "rw" for read-write access to the file |
103 |
* |
* |
104 |
* @exception IllegalArgumentException If <code>mode</code> has an |
* @exception IllegalArgumentException If <code>mode</code> has an |
105 |
* illegal value |
* illegal value |
106 |
* @exception SecurityException If the requested access to the file |
* @exception SecurityException If the requested access to the file |
107 |
* is not allowed |
* is not allowed |
108 |
* @exception FileNotFoundException If any other error occurs |
* @exception FileNotFoundException If any other error occurs |
109 |
*/ |
*/ |
110 |
public RandomAccessFile(String name, String mode) |
public RandomAccessFile(String name, String mode) |
111 |
throws IllegalArgumentException, SecurityException, FileNotFoundException |
throws IllegalArgumentException, SecurityException, FileNotFoundException |
112 |
{ |
{ |
138 |
} |
} |
139 |
} |
} |
140 |
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/* |
|
|
* Instance Methods |
|
|
*/ |
|
|
|
|
141 |
/** |
/** |
142 |
* This method closes the file and frees up all file related system |
* This method closes the file and frees up all file related system |
143 |
* resources. Since most operating systems put a limit on how many files |
* resources. Since most operating systems put a limit on how many files |
144 |
* may be opened at any given time, it is a good idea to close all files |
* may be opened at any given time, it is a good idea to close all files |
145 |
* when no longer needed to avoid hitting this limit |
* when no longer needed to avoid hitting this limit |
146 |
*/ |
*/ |
147 |
public void close() throws IOException |
public void close() throws IOException |
148 |
{ |
{ |
149 |
fd.close(); |
fd.close(); |
150 |
} |
} |
151 |
|
|
|
/*************************************************************************/ |
|
|
|
|
152 |
/** |
/** |
153 |
* This method returns a <code>FileDescriptor</code> object that |
* This method returns a <code>FileDescriptor</code> object that |
154 |
* represents the native file handle for this file. |
* represents the native file handle for this file. |
155 |
* |
* |
156 |
* @return The <code>FileDescriptor</code> object for this file |
* @return The <code>FileDescriptor</code> object for this file |
157 |
* |
* |
158 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
159 |
*/ |
*/ |
160 |
public final FileDescriptor getFD() throws IOException |
public final FileDescriptor getFD() throws IOException |
161 |
{ |
{ |
162 |
return(fd); |
return(fd); |
163 |
} |
} |
164 |
|
|
|
/*************************************************************************/ |
|
|
|
|
165 |
/** |
/** |
166 |
* This method returns the current offset in the file at which the next |
* This method returns the current offset in the file at which the next |
167 |
* read or write will occur |
* read or write will occur |
168 |
* |
* |
169 |
* @return The current file position |
* @return The current file position |
170 |
* |
* |
171 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
172 |
*/ |
*/ |
173 |
public long getFilePointer() throws IOException |
public long getFilePointer() throws IOException |
174 |
{ |
{ |
175 |
return(fd.getFilePointer()); |
return(fd.getFilePointer()); |
176 |
} |
} |
177 |
|
|
|
/*************************************************************************/ |
|
|
|
|
178 |
/** |
/** |
179 |
* This method returns the length of the file in bytes |
* This method returns the length of the file in bytes |
180 |
* |
* |
181 |
* @return The length of the file |
* @return The length of the file |
182 |
* |
* |
183 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
184 |
*/ |
*/ |
185 |
public long length() throws IOException |
public long length() throws IOException |
186 |
{ |
{ |
187 |
return(fd.getLength()); |
return(fd.getLength()); |
188 |
} |
} |
189 |
|
|
|
/*************************************************************************/ |
|
|
|
|
190 |
/** |
/** |
191 |
* This method sets the current file position to the specified offset |
* This method sets the current file position to the specified offset |
192 |
* from the beginning of the file. Note that some operating systems will |
* from the beginning of the file. Note that some operating systems will |
193 |
* allow the file pointer to be set past the current end of the file. |
* allow the file pointer to be set past the current end of the file. |
194 |
* |
* |
195 |
* @param pos The offset from the beginning of the file at which to set |
* @param pos The offset from the beginning of the file at which to set |
196 |
* the file pointer |
* the file pointer |
197 |
* |
* |
198 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
199 |
*/ |
*/ |
200 |
public void seek(long pos) throws IOException |
public void seek(long pos) throws IOException |
201 |
{ |
{ |
202 |
fd.seek(pos, fd.SET, false); |
fd.seek(pos, fd.SET, false); |
203 |
} |
} |
204 |
|
|
|
/*************************************************************************/ |
|
|
|
|
205 |
/** |
/** |
206 |
* This method sets the length of the file to the specified length. If |
* This method sets the length of the file to the specified length. If |
207 |
* the currently length of the file is longer than the specified length, |
* the currently length of the file is longer than the specified length, |
208 |
* then the file is truncated to the specified length. If the current |
* then the file is truncated to the specified length. If the current |
209 |
* length of the file is shorter than the specified length, the file |
* length of the file is shorter than the specified length, the file |
210 |
* is extended with bytes of an undefined value. |
* is extended with bytes of an undefined value. |
211 |
* <p> |
* <p> |
212 |
* The file must be open for write access for this operation to succeed. |
* The file must be open for write access for this operation to succeed. |
213 |
* |
* |
214 |
* @param newlen The new length of the file |
* @param newlen The new length of the file |
215 |
* |
* |
216 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
217 |
*/ |
*/ |
218 |
public void setLength(long newlen) throws IOException |
public void setLength(long newlen) throws IOException |
219 |
{ |
{ |
220 |
if (readOnly) |
if (readOnly) |
223 |
fd.setLength(newlen); |
fd.setLength(newlen); |
224 |
} |
} |
225 |
|
|
|
/*************************************************************************/ |
|
|
|
|
226 |
/** |
/** |
227 |
* This method reads a single byte of data from the file and returns it |
* This method reads a single byte of data from the file and returns it |
228 |
* as an integer. |
* as an integer. |
236 |
return(fd.read()); |
return(fd.read()); |
237 |
} |
} |
238 |
|
|
|
/*************************************************************************/ |
|
|
|
|
239 |
/** |
/** |
240 |
* This method reads bytes from the file into the specified array. The |
* This method reads bytes from the file into the specified array. The |
241 |
* bytes are stored starting at the beginning of the array and up to |
* bytes are stored starting at the beginning of the array and up to |
242 |
* <code>buf.length</code> bytes can be read. |
* <code>buf.length</code> bytes can be read. |
243 |
* |
* |
244 |
* @param buf The buffer to read bytes from the file into |
* @param buf The buffer to read bytes from the file into |
245 |
* |
* |
246 |
* @return The actual number of bytes read or -1 if end of file |
* @return The actual number of bytes read or -1 if end of file |
247 |
* |
* |
248 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
249 |
*/ |
*/ |
250 |
public int read(byte[] buf) throws IOException |
public int read(byte[] buf) throws IOException |
251 |
{ |
{ |
252 |
return(read(buf, 0, buf.length)); |
return(read(buf, 0, buf.length)); |
253 |
} |
} |
254 |
|
|
|
/*************************************************************************/ |
|
|
|
|
255 |
/** |
/** |
256 |
* This methods reads up to <code>len</code> bytes from the file into the s |
* This methods reads up to <code>len</code> bytes from the file into the s |
257 |
* pecified array starting at position <code>offset</code> into the array. |
* pecified array starting at position <code>offset</code> into the array. |
258 |
* |
* |
259 |
* @param buf The array to read the bytes into |
* @param buf The array to read the bytes into |
260 |
* @param offset The index into the array to start storing bytes |
* @param offset The index into the array to start storing bytes |
261 |
* @param len The requested number of bytes to read |
* @param len The requested number of bytes to read |
262 |
* |
* |
263 |
* @param len The actual number of bytes read, or -1 if end of file |
* @return The actual number of bytes read, or -1 if end of file |
264 |
* |
* |
265 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
266 |
*/ |
*/ |
267 |
public int read(byte[] buf, int offset, int len) throws IOException |
public int read(byte[] buf, int offset, int len) throws IOException |
268 |
{ |
{ |
269 |
return(fd.read(buf,offset, len)); |
return(fd.read(buf,offset, len)); |
270 |
} |
} |
271 |
|
|
|
/*************************************************************************/ |
|
|
|
|
272 |
/** |
/** |
273 |
* This method reads a Java boolean value from an input stream. It does |
* This method reads a Java boolean value from an input stream. It does |
274 |
* so by reading a single byte of data. If that byte is zero, then the |
* so by reading a single byte of data. If that byte is zero, then the |
275 |
* value returned is <code>false</code> If the byte is non-zero, then |
* value returned is <code>false</code> If the byte is non-zero, then |
276 |
* the value returned is <code>true</code> |
* the value returned is <code>true</code> |
277 |
* <p> |
* <p> |
278 |
* This method can read a <code>boolean</code> written by an object |
* This method can read a <code>boolean</code> written by an object |
279 |
* implementing the |
* implementing the |
280 |
* <code>writeBoolean()</code> method in the <code>DataOutput</code> |
* <code>writeBoolean()</code> method in the <code>DataOutput</code> |
281 |
* interface. |
* interface. |
282 |
* |
* |
283 |
* @return The <code>boolean</code> value read |
* @return The <code>boolean</code> value read |
284 |
* |
* |
285 |
* @exception EOFException If end of file is reached before reading the |
* @exception EOFException If end of file is reached before reading the |
286 |
* boolean |
* boolean |
287 |
* @exception IOException If any other error occurs |
* @exception IOException If any other error occurs |
288 |
*/ |
*/ |
289 |
public final boolean readBoolean() throws EOFException, IOException |
public final boolean readBoolean() throws EOFException, IOException |
290 |
{ |
{ |
291 |
int byte_read = read(); |
int byte_read = read(); |
296 |
return(byte_read != 0); |
return(byte_read != 0); |
297 |
} |
} |
298 |
|
|
|
/*************************************************************************/ |
|
|
|
|
299 |
/** |
/** |
300 |
* This method reads a Java byte value from an input stream. The value |
* This method reads a Java byte value from an input stream. The value |
301 |
* is in the range of -128 to 127. |
* is in the range of -128 to 127. |
302 |
* <p> |
* <p> |
303 |
* This method can read a <code>byte</code> written by an object |
* This method can read a <code>byte</code> written by an object |
304 |
* implementing the |
* implementing the |
305 |
* <code>writeByte()</code> method in the <code>DataOutput</code> interface. |
* <code>writeByte()</code> method in the <code>DataOutput</code> interface. |
306 |
* |
* |
307 |
* @return The <code>byte</code> value read |
* @return The <code>byte</code> value read |
308 |
* |
* |
309 |
* @exception EOFException If end of file is reached before reading the byte |
* @exception EOFException If end of file is reached before reading the byte |
310 |
* @exception IOException If any other error occurs |
* @exception IOException If any other error occurs |
311 |
* |
* |
312 |
* @see DataOutput |
* @see DataOutput |
313 |
*/ |
*/ |
314 |
public final byte readByte() throws EOFException, IOException |
public final byte readByte() throws EOFException, IOException |
315 |
{ |
{ |
316 |
int byte_read = read(); |
int byte_read = read(); |
321 |
return (byte)byte_read; |
return (byte)byte_read; |
322 |
} |
} |
323 |
|
|
|
/*************************************************************************/ |
|
|
|
|
324 |
/** |
/** |
325 |
* This method reads 8 unsigned bits into a Java <code>int</code> value |
* This method reads 8 unsigned bits into a Java <code>int</code> value |
326 |
* from the |
* from the |
327 |
* stream. The value returned is in the range of 0 to 255. |
* stream. The value returned is in the range of 0 to 255. |
328 |
* <p> |
* <p> |
329 |
* This method can read an unsigned byte written by an object implementing |
* This method can read an unsigned byte written by an object implementing |
330 |
* the <code>writeUnsignedByte()</code> method in the |
* the <code>writeUnsignedByte()</code> method in the |
331 |
* <code>DataOutput</code> interface. |
* <code>DataOutput</code> interface. |
332 |
* |
* |
333 |
* @return The unsigned bytes value read as a Java <code>int</code> |
* @return The unsigned bytes value read as a Java <code>int</code> |
334 |
* |
* |
335 |
* @exception EOFException If end of file is reached before reading the value |
* @exception EOFException If end of file is reached before reading the value |
336 |
* @exception IOException If any other error occurs |
* @exception IOException If any other error occurs |
337 |
* |
* |
338 |
* @see DataOutput |
* @see DataOutput |
339 |
*/ |
*/ |
340 |
public final int readUnsignedByte() throws EOFException, IOException |
public final int readUnsignedByte() throws EOFException, IOException |
341 |
{ |
{ |
342 |
int byte_read = read(); |
int byte_read = read(); |
347 |
return(byte_read & 0xFF); |
return(byte_read & 0xFF); |
348 |
} |
} |
349 |
|
|
|
/*************************************************************************/ |
|
|
|
|
350 |
/** |
/** |
351 |
* This method reads a Java <code>char</code> value from an input stream. |
* This method reads a Java <code>char</code> value from an input stream. |
352 |
* It operates by reading two bytes from the stream and converting them to |
* It operates by reading two bytes from the stream and converting them to |
353 |
* a single 16-bit Java <code>char</code> The two bytes are stored most |
* a single 16-bit Java <code>char</code> The two bytes are stored most |
354 |
* significant byte first (i.e., "big endian") regardless of the native |
* significant byte first (i.e., "big endian") regardless of the native |
355 |
* host byte ordering. |
* host byte ordering. |
356 |
* <p> |
* <p> |
357 |
* As an example, if <code>byte1</code> and code{byte2</code> represent |
* As an example, if <code>byte1</code> and code{byte2</code> represent |
358 |
* the first |
* the first |
359 |
* and second byte read from the stream respectively, they will be |
* and second byte read from the stream respectively, they will be |
360 |
* transformed to a <code>char</code> in the following manner: |
* transformed to a <code>char</code> in the following manner: |
361 |
* <p> |
* <p> |
362 |
* <code>(char)(((byte1 & 0xFF) << 8) | (byte2 & 0xFF)</code> |
* <code>(char)(((byte1 & 0xFF) << 8) | (byte2 & 0xFF)</code> |
363 |
* <p> |
* <p> |
364 |
* This method can read a <code>char</code> written by an object |
* This method can read a <code>char</code> written by an object |
365 |
* implementing the |
* implementing the |
366 |
* <code>writeChar()</code> method in the <code>DataOutput</code> interface. |
* <code>writeChar()</code> method in the <code>DataOutput</code> interface. |
367 |
* |
* |
368 |
* @return The <code>char</code> value read |
* @return The <code>char</code> value read |
369 |
* |
* |
370 |
* @exception EOFException If end of file is reached before reading the char |
* @exception EOFException If end of file is reached before reading the char |
371 |
* @exception IOException If any other error occurs |
* @exception IOException If any other error occurs |
372 |
* |
* |
373 |
* @see DataOutput |
* @see DataOutput |
374 |
*/ |
*/ |
375 |
public final synchronized char readChar() throws EOFException, IOException |
public final synchronized char readChar() throws EOFException, IOException |
376 |
{ |
{ |
377 |
readFully(buf, 0, 2); |
readFully(buf, 0, 2); |
379 |
return (char) ((buf[0] << 8) | (buf[1] & 0xff)); |
return (char) ((buf[0] << 8) | (buf[1] & 0xff)); |
380 |
} |
} |
381 |
|
|
|
/*************************************************************************/ |
|
|
|
|
382 |
/** |
/** |
383 |
* This method reads a signed 16-bit value into a Java in from the stream. |
* This method reads a signed 16-bit value into a Java in from the stream. |
384 |
* It operates by reading two bytes from the stream and converting them to |
* It operates by reading two bytes from the stream and converting them to |
385 |
* a single 16-bit Java <code>short</code> The two bytes are stored most |
* a single 16-bit Java <code>short</code> The two bytes are stored most |
386 |
* significant byte first (i.e., "big endian") regardless of the native |
* significant byte first (i.e., "big endian") regardless of the native |
387 |
* host byte ordering. |
* host byte ordering. |
388 |
* <p> |
* <p> |
389 |
* As an example, if <code>byte1</code> and code{byte2</code> |
* As an example, if <code>byte1</code> and code{byte2</code> |
390 |
* represent the first |
* represent the first |
391 |
* and second byte read from the stream respectively, they will be |
* and second byte read from the stream respectively, they will be |
392 |
* transformed to a <code>short</code> in the following manner: |
* transformed to a <code>short</code> in the following manner: |
393 |
* <p> |
* <p> |
394 |
* <code>(short)(((byte1 & 0xFF) << 8) | (byte2 & 0xFF)</code> |
* <code>(short)(((byte1 & 0xFF) << 8) | (byte2 & 0xFF)</code> |
395 |
* <p> |
* <p> |
396 |
* The value returned is in the range of -32768 to 32767. |
* The value returned is in the range of -32768 to 32767. |
397 |
* <p> |
* <p> |
398 |
* This method can read a <code>short</code> written by an object |
* This method can read a <code>short</code> written by an object |
399 |
* implementing the |
* implementing the |
400 |
* <code>writeShort()</code> method in the <code>DataOutput</code> interface. |
* <code>writeShort()</code> method in the <code>DataOutput</code> interface. |
401 |
* |
* |
402 |
* @return The <code>short</code> value read |
* @return The <code>short</code> value read |
403 |
* |
* |
404 |
* @exception EOFException If end of file is reached before reading the value |
* @exception EOFException If end of file is reached before reading the value |
405 |
* @exception IOException If any other error occurs |
* @exception IOException If any other error occurs |
406 |
* |
* |
407 |
* @see DataOutput |
* @see DataOutput |
408 |
*/ |
*/ |
409 |
public final synchronized short readShort() throws EOFException, IOException |
public final synchronized short readShort() throws EOFException, IOException |
410 |
{ |
{ |
411 |
readFully(buf, 0, 2); |
readFully(buf, 0, 2); |
412 |
return (short) ((buf[0] << 8) | (buf[1] & 0xff)); |
return (short) ((buf[0] << 8) | (buf[1] & 0xff)); |
413 |
} |
} |
414 |
|
|
|
/*************************************************************************/ |
|
|
|
|
415 |
/** |
/** |
416 |
* This method reads 16 unsigned bits into a Java int value from the stream. |
* This method reads 16 unsigned bits into a Java int value from the stream. |
417 |
* It operates by reading two bytes from the stream and converting them to |
* It operates by reading two bytes from the stream and converting them to |
418 |
* a single Java <code>int</code> The two bytes are stored most |
* a single Java <code>int</code> The two bytes are stored most |
419 |
* significant byte first (i.e., "big endian") regardless of the native |
* significant byte first (i.e., "big endian") regardless of the native |
420 |
* host byte ordering. |
* host byte ordering. |
421 |
* <p> |
* <p> |
422 |
* As an example, if <code>byte1</code> and <code>byte2</code> |
* As an example, if <code>byte1</code> and <code>byte2</code> |
423 |
* represent the first |
* represent the first |
424 |
* and second byte read from the stream respectively, they will be |
* and second byte read from the stream respectively, they will be |
425 |
* transformed to an <code>int</code> in the following manner: |
* transformed to an <code>int</code> in the following manner: |
426 |
* <p> |
* <p> |
427 |
* <code>(int)(((byte1 & 0xFF) << 8) + (byte2 & 0xFF))</code> |
* <code>(int)(((byte1 & 0xFF) << 8) + (byte2 & 0xFF))</code> |
428 |
* <p> |
* <p> |
429 |
* The value returned is in the range of 0 to 65535. |
* The value returned is in the range of 0 to 65535. |
430 |
* <p> |
* <p> |
431 |
* This method can read an unsigned short written by an object implementing |
* This method can read an unsigned short written by an object implementing |
432 |
* the <code>writeUnsignedShort()</code> method in the |
* the <code>writeUnsignedShort()</code> method in the |
433 |
* <code>DataOutput</code> interface. |
* <code>DataOutput</code> interface. |
434 |
* |
* |
435 |
* @return The unsigned short value read as a Java <code>int</code> |
* @return The unsigned short value read as a Java <code>int</code> |
436 |
* |
* |
437 |
* @exception EOFException If end of file is reached before reading the value |
* @exception EOFException If end of file is reached before reading the value |
438 |
* @exception IOException If any other error occurs |
* @exception IOException If any other error occurs |
439 |
*/ |
*/ |
440 |
public final synchronized int readUnsignedShort() |
public final synchronized int readUnsignedShort() |
441 |
throws EOFException, IOException |
throws EOFException, IOException |
442 |
{ |
{ |
444 |
return (((buf[0] & 0xff) << 8) | (buf[1] & 0xff)); |
return (((buf[0] & 0xff) << 8) | (buf[1] & 0xff)); |
445 |
} |
} |
446 |
|
|
|
/*************************************************************************/ |
|
|
|
|
447 |
/** |
/** |
448 |
* This method reads a Java <code>int</code> value from an input stream |
* This method reads a Java <code>int</code> value from an input stream |
449 |
* It operates by reading four bytes from the stream and converting them to |
* It operates by reading four bytes from the stream and converting them to |
450 |
* a single Java <code>int</code> The bytes are stored most |
* a single Java <code>int</code> The bytes are stored most |
451 |
* significant byte first (i.e., "big endian") regardless of the native |
* significant byte first (i.e., "big endian") regardless of the native |
452 |
* host byte ordering. |
* host byte ordering. |
453 |
* <p> |
* <p> |
454 |
* As an example, if <code>byte1</code> through <code>byte4</code> |
* As an example, if <code>byte1</code> through <code>byte4</code> |
455 |
* represent the first |
* represent the first |
456 |
* four bytes read from the stream, they will be |
* four bytes read from the stream, they will be |
457 |
* transformed to an <code>int</code> in the following manner: |
* transformed to an <code>int</code> in the following manner: |
458 |
* <p> |
* <p> |
459 |
* <code>(int)(((byte1 & 0xFF) << 24) + ((byte2 & 0xFF) << 16) + |
* <code>(int)(((byte1 & 0xFF) << 24) + ((byte2 & 0xFF) << 16) + |
460 |
* ((byte3 & 0xFF) << 8) + (byte4 & 0xFF)))</code> |
* ((byte3 & 0xFF) << 8) + (byte4 & 0xFF)))</code> |
461 |
* <p> |
* <p> |
462 |
* The value returned is in the range of 0 to 65535. |
* The value returned is in the range of 0 to 65535. |
463 |
* <p> |
* <p> |
464 |
* This method can read an <code>int</code> written by an object |
* This method can read an <code>int</code> written by an object |
465 |
* implementing the |
* implementing the |
466 |
* <code>writeInt()</code> method in the <code>DataOutput</code> interface. |
* <code>writeInt()</code> method in the <code>DataOutput</code> interface. |
467 |
* |
* |
468 |
* @return The <code>int</code> value read |
* @return The <code>int</code> value read |
469 |
* |
* |
470 |
* @exception EOFException If end of file is reached before reading the int |
* @exception EOFException If end of file is reached before reading the int |
471 |
* @exception IOException If any other error occurs |
* @exception IOException If any other error occurs |
472 |
* |
* |
473 |
* @see DataOutput |
* @see DataOutput |
474 |
*/ |
*/ |
475 |
public final synchronized int readInt() throws EOFException, IOException |
public final synchronized int readInt() throws EOFException, IOException |
476 |
{ |
{ |
477 |
readFully(buf, 0, 4); |
readFully(buf, 0, 4); |
480 |
((buf[2] & 0xff) << 8) | (buf[3] & 0xff)); |
((buf[2] & 0xff) << 8) | (buf[3] & 0xff)); |
481 |
} |
} |
482 |
|
|
|
/*************************************************************************/ |
|
|
|
|
483 |
/** |
/** |
484 |
* This method reads a Java long value from an input stream |
* This method reads a Java long value from an input stream |
485 |
* It operates by reading eight bytes from the stream and converting them to |
* It operates by reading eight bytes from the stream and converting them to |
486 |
* a single Java <code>long</code> The bytes are stored most |
* a single Java <code>long</code> The bytes are stored most |
487 |
* significant byte first (i.e., "big endian") regardless of the native |
* significant byte first (i.e., "big endian") regardless of the native |
488 |
* host byte ordering. |
* host byte ordering. |
489 |
* <p> |
* <p> |
490 |
* As an example, if <code>byte1</code> through <code>byte8</code> |
* As an example, if <code>byte1</code> through <code>byte8</code> |
491 |
* represent the first |
* represent the first |
492 |
* eight bytes read from the stream, they will be |
* eight bytes read from the stream, they will be |
493 |
* transformed to an <code>long</code> in the following manner: |
* transformed to an <code>long</code> in the following manner: |
494 |
* <p> |
* <p> |
495 |
* <code> |
* <code> |
496 |
* (long)((((long)byte1 & 0xFF) << 56) + (((long)byte2 & 0xFF) << 48) + |
* (long)((((long)byte1 & 0xFF) << 56) + (((long)byte2 & 0xFF) << 48) + |
497 |
* (((long)byte3 & 0xFF) << 40) + (((long)byte4 & 0xFF) << 32) + |
* (((long)byte3 & 0xFF) << 40) + (((long)byte4 & 0xFF) << 32) + |
498 |
* (((long)byte5 & 0xFF) << 24) + (((long)byte6 & 0xFF) << 16) + |
* (((long)byte5 & 0xFF) << 24) + (((long)byte6 & 0xFF) << 16) + |
499 |
* (((long)byte7 & 0xFF) << 8) + ((long)byte9 & 0xFF)))</code> |
* (((long)byte7 & 0xFF) << 8) + ((long)byte9 & 0xFF)))</code> |
500 |
* <p> |
* <p> |
501 |
* The value returned is in the range of 0 to 65535. |
* The value returned is in the range of 0 to 65535. |
502 |
* <p> |
* <p> |
503 |
* This method can read an <code>long</code> written by an object |
* This method can read an <code>long</code> written by an object |
504 |
* implementing the |
* implementing the |
505 |
* <code>writeLong()</code> method in the <code>DataOutput</code> interface. |
* <code>writeLong()</code> method in the <code>DataOutput</code> interface. |
506 |
* |
* |
507 |
* @return The <code>long</code> value read |
* @return The <code>long</code> value read |
508 |
* |
* |
509 |
* @exception EOFException If end of file is reached before reading the long |
* @exception EOFException If end of file is reached before reading the long |
510 |
* @exception IOException If any other error occurs |
* @exception IOException If any other error occurs |
511 |
* |
* |
512 |
* @see DataOutput |
* @see DataOutput |
513 |
*/ |
*/ |
514 |
public final synchronized long readLong() throws EOFException, IOException |
public final synchronized long readLong() throws EOFException, IOException |
515 |
{ |
{ |
516 |
readFully(buf, 0, 8); |
readFully(buf, 0, 8); |
525 |
((long)(buf[7] & 0xff))); |
((long)(buf[7] & 0xff))); |
526 |
} |
} |
527 |
|
|
|
/*************************************************************************/ |
|
|
|
|
528 |
/** |
/** |
529 |
* This method reads a Java float value from an input stream. It operates |
* This method reads a Java float value from an input stream. It operates |
530 |
* by first reading an <code>int</code> value from the stream by calling the |
* by first reading an <code>int</code> value from the stream by calling the |
531 |
* <code>readInt()</code> method in this interface, then converts |
* <code>readInt()</code> method in this interface, then converts |
532 |
* that <code>int</code> |
* that <code>int</code> |
533 |
* to a <code>float</code> using the <code>intBitsToFloat</code> method in |
* to a <code>float</code> using the <code>intBitsToFloat</code> method in |
534 |
* the class <code>java.lang.Float</code> |
* the class <code>java.lang.Float</code> |
535 |
* <p> |
* <p> |
536 |
* This method can read a <code>float</code> written by an object |
* This method can read a <code>float</code> written by an object |
537 |
* implementing the |
* implementing the |
538 |
* <code>writeFloat()</code> method in the <code>DataOutput</code> interface. |
* <code>writeFloat()</code> method in the <code>DataOutput</code> interface. |
539 |
* |
* |
540 |
* @return The <code>float</code> value read |
* @return The <code>float</code> value read |
541 |
* |
* |
542 |
* @exception EOFException If end of file is reached before reading the float |
* @exception EOFException If end of file is reached before reading the float |
543 |
* @exception IOException If any other error occurs |
* @exception IOException If any other error occurs |
544 |
* |
* |
545 |
* @see java.lang.Float |
* @see java.lang.Float |
546 |
* @see DataOutput |
* @see DataOutput |
547 |
*/ |
*/ |
548 |
public final float readFloat() throws EOFException, IOException |
public final float readFloat() throws EOFException, IOException |
549 |
{ |
{ |
550 |
int val = readInt(); |
int val = readInt(); |
552 |
return(Float.intBitsToFloat(val)); |
return(Float.intBitsToFloat(val)); |
553 |
} |
} |
554 |
|
|
|
/*************************************************************************/ |
|
|
|
|
555 |
/** |
/** |
556 |
* This method reads a Java double value from an input stream. It operates |
* This method reads a Java double value from an input stream. It operates |
557 |
* by first reading a <code>logn</code> value from the stream by calling the |
* by first reading a <code>logn</code> value from the stream by calling the |
558 |
* <code>readLong()</code> method in this interface, then |
* <code>readLong()</code> method in this interface, then |
559 |
* converts that <code>long</code> |
* converts that <code>long</code> |
560 |
* to a <code>double</code> using the <code>longBitsToDouble</code> |
* to a <code>double</code> using the <code>longBitsToDouble</code> |
561 |
* method in the class <code>java.lang.Double</code> |
* method in the class <code>java.lang.Double</code> |
562 |
* <p> |
* <p> |
563 |
* This method can read a <code>double</code> written by an object |
* This method can read a <code>double</code> written by an object |
564 |
* implementing the |
* implementing the |
565 |
* <code>writeDouble()</code> method in the <code>DataOutput</code> |
* <code>writeDouble()</code> method in the <code>DataOutput</code> |
566 |
* interface. |
* interface. |
567 |
* |
* |
568 |
* @return The <code>double</code> value read |
* @return The <code>double</code> value read |
569 |
* |
* |
570 |
* @exception EOFException If end of file is reached before reading |
* @exception EOFException If end of file is reached before reading |
571 |
* the double |
* the double |
572 |
* @exception IOException If any other error occurs |
* @exception IOException If any other error occurs |
573 |
* |
* |
574 |
* @see java.lang.Double |
* @see java.lang.Double |
575 |
* @see DataOutput |
* @see DataOutput |
576 |
*/ |
*/ |
577 |
public final double readDouble() throws EOFException, IOException |
public final double readDouble() throws EOFException, IOException |
578 |
{ |
{ |
579 |
long val = readLong(); |
long val = readLong(); |
581 |
return(Double.longBitsToDouble(val)); |
return(Double.longBitsToDouble(val)); |
582 |
} |
} |
583 |
|
|
|
/*************************************************************************/ |
|
|
|
|
584 |
/** |
/** |
585 |
* This method reads the next line of text data from an input stream. |
* This method reads the next line of text data from an input stream. |
586 |
* It operates by reading bytes and converting those bytes to |
* It operates by reading bytes and converting those bytes to |
587 |
* <code>char</code> |
* <code>char</code> |
588 |
* values by treating the byte read as the low eight bits of the |
* values by treating the byte read as the low eight bits of the |
589 |
* <code>char</code> |
* <code>char</code> |
590 |
* and using <code>0</code> as the high eight bits. Because of this, it does |
* and using <code>0</code> as the high eight bits. Because of this, it does |
591 |
* not support the full 16-bit Unicode character set. |
* not support the full 16-bit Unicode character set. |
592 |
* <p> |
* <p> |
593 |
* The reading of bytes ends when either the end of file or a line terminator |
* The reading of bytes ends when either the end of file or a line terminator |
594 |
* is encountered. The bytes read are then returned as a <code>String</code> |
* is encountered. The bytes read are then returned as a <code>String</code> |
595 |
* A line terminator is a byte sequence consisting of either |
* A line terminator is a byte sequence consisting of either |
596 |
* <code>\r</code> <code>\n</code> or <code>\r\n</code> These |
* <code>\r</code> <code>\n</code> or <code>\r\n</code> These |
597 |
* termination charaters are |
* termination charaters are |
598 |
* discarded and are not returned as part of the string. |
* discarded and are not returned as part of the string. |
599 |
* <p> |
* <p> |
600 |
* This method can read data that was written by an object implementing the |
* This method can read data that was written by an object implementing the |
601 |
* <code>writeLine()</code> method in <code>DataOutput</code> |
* <code>writeLine()</code> method in <code>DataOutput</code> |
602 |
* |
* |
603 |
* @return The line read as a <code>String</code> |
* @return The line read as a <code>String</code> |
604 |
* |
* |
605 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
606 |
* |
* |
607 |
* @see DataOutput |
* @see DataOutput |
608 |
* |
* |
609 |
* @deprecated |
* @deprecated |
610 |
*/ |
*/ |
611 |
public synchronized final String readLine() throws IOException |
public synchronized final String readLine() throws IOException |
612 |
{ |
{ |
613 |
StringBuffer sb = new StringBuffer(""); |
StringBuffer sb = new StringBuffer(""); |
637 |
} |
} |
638 |
} |
} |
639 |
|
|
|
/*************************************************************************/ |
|
|
|
|
640 |
/** |
/** |
641 |
* This method reads a <code>String</code> from an input stream that |
* This method reads a <code>String</code> from an input stream that |
642 |
* is encoded in |
* is encoded in |
643 |
* a modified UTF-8 format. This format has a leading two byte sequence |
* a modified UTF-8 format. This format has a leading two byte sequence |
644 |
* that contains the remaining number of bytes to read. This two byte |
* that contains the remaining number of bytes to read. This two byte |
645 |
* sequence is read using the <code>readUnsignedShort()</code> method of this |
* sequence is read using the <code>readUnsignedShort()</code> method of this |
646 |
* interface. |
* interface. |
647 |
* <p> |
* <p> |
648 |
* After the number of remaining bytes have been determined, these bytes |
* After the number of remaining bytes have been determined, these bytes |
649 |
* are read an transformed into <code>char</code> values. |
* are read an transformed into <code>char</code> values. |
650 |
* These <code>char</code> values |
* These <code>char</code> values |
651 |
* are encoded in the stream using either a one, two, or three byte format. |
* are encoded in the stream using either a one, two, or three byte format. |
652 |
* The particular format in use can be determined by examining the first |
* The particular format in use can be determined by examining the first |
653 |
* byte read. |
* byte read. |
654 |
* <p> |
* <p> |
655 |
* If the first byte has a high order bit of 0 then |
* If the first byte has a high order bit of 0 then |
656 |
* that character consists on only one byte. This character value consists |
* that character consists on only one byte. This character value consists |
657 |
* of seven bits that are at positions 0 through 6 of the byte. As an |
* of seven bits that are at positions 0 through 6 of the byte. As an |
658 |
* example, if <code>byte1</code> is the byte read from the stream, it would |
* example, if <code>byte1</code> is the byte read from the stream, it would |
659 |
* be converted to a <code>char</code> like so: |
* be converted to a <code>char</code> like so: |
660 |
* <p> |
* <p> |
661 |
* <code>(char)byte1</code> |
* <code>(char)byte1</code> |
662 |
* <p> |
* <p> |
663 |
* If the first byte has <code>110</code> as its high order bits, then the |
* If the first byte has <code>110</code> as its high order bits, then the |
664 |
* character consists of two bytes. The bits that make up the character |
* character consists of two bytes. The bits that make up the character |
665 |
* value are in positions 0 through 4 of the first byte and bit positions |
* value are in positions 0 through 4 of the first byte and bit positions |
666 |
* 0 through 5 of the second byte. (The second byte should have |
* 0 through 5 of the second byte. (The second byte should have |
667 |
* 10 as its high order bits). These values are in most significant |
* 10 as its high order bits). These values are in most significant |
668 |
* byte first (i.e., "big endian") order. |
* byte first (i.e., "big endian") order. |
669 |
* <p> |
* <p> |
670 |
* As an example, if <code>byte1</code> and <code>byte2</code> |
* As an example, if <code>byte1</code> and <code>byte2</code> |
671 |
* are the first two bytes |
* are the first two bytes |
672 |
* read respectively, and the high order bits of them match the patterns |
* read respectively, and the high order bits of them match the patterns |
673 |
* which indicate a two byte character encoding, then they would be |
* which indicate a two byte character encoding, then they would be |
674 |
* converted to a Java <code>char</code> like so: |
* converted to a Java <code>char</code> like so: |
675 |
* <p> |
* <p> |
676 |
* <code>(char)(((byte1 & 0x1F) << 6) | (byte2 & 0x3F))</code> |
* <code>(char)(((byte1 & 0x1F) << 6) | (byte2 & 0x3F))</code> |
677 |
* <p> |
* <p> |
678 |
* If the first byte has a <code>1110</code> as its high order bits, then the |
* If the first byte has a <code>1110</code> as its high order bits, then the |
679 |
* character consists of three bytes. The bits that make up the character |
* character consists of three bytes. The bits that make up the character |
680 |
* value are in positions 0 through 3 of the first byte and bit positions |
* value are in positions 0 through 3 of the first byte and bit positions |
681 |
* 0 through 5 of the other two bytes. (The second and third bytes should |
* 0 through 5 of the other two bytes. (The second and third bytes should |
682 |
* have <code>10</code> as their high order bits). These values are in most |
* have <code>10</code> as their high order bits). These values are in most |
683 |
* significant byte first (i.e., "big endian") order. |
* significant byte first (i.e., "big endian") order. |
684 |
* <p> |
* <p> |
685 |
* As an example, if <code>byte1</code> <code>byte2</code> |
* As an example, if <code>byte1</code> <code>byte2</code> |
686 |
* and <code>byte3</code> are the |
* and <code>byte3</code> are the |
687 |
* three bytes read, and the high order bits of them match the patterns |
* three bytes read, and the high order bits of them match the patterns |
688 |
* which indicate a three byte character encoding, then they would be |
* which indicate a three byte character encoding, then they would be |
689 |
* converted to a Java <code>char</code> like so: |
* converted to a Java <code>char</code> like so: |
690 |
* <p> |
* <p> |
691 |
* <code>(char)(((byte1 & 0x0F) << 12) | ((byte2 & 0x3F) << 6) | |
* <code>(char)(((byte1 & 0x0F) << 12) | ((byte2 & 0x3F) << 6) | |
692 |
* (byte3 & 0x3F))</code> |
* (byte3 & 0x3F))</code> |
693 |
* <p> |
* <p> |
694 |
* Note that all characters are encoded in the method that requires the |
* Note that all characters are encoded in the method that requires the |
695 |
* fewest number of bytes with the exception of the character with the |
* fewest number of bytes with the exception of the character with the |
696 |
* value of <code>\u0000</code> which is encoded as two bytes. This is |
* value of <code>\u0000</code> which is encoded as two bytes. This is |
697 |
* a modification of the UTF standard used to prevent C language style |
* a modification of the UTF standard used to prevent C language style |
698 |
* <code>NUL</code> values from appearing in the byte stream. |
* <code>NUL</code> values from appearing in the byte stream. |
699 |
* <p> |
* <p> |
700 |
* This method can read data that was written by an object implementing the |
* This method can read data that was written by an object implementing the |
701 |
* <code>writeUTF()</code> method in <code>DataOutput</code> |
* <code>writeUTF()</code> method in <code>DataOutput</code> |
702 |
* |
* |
703 |
* @return The <code>String</code> read |
* @return The <code>String</code> read |
704 |
* |
* |
705 |
* @exception EOFException If end of file is reached before reading the |
* @exception EOFException If end of file is reached before reading the |
706 |
* String |
* String |
707 |
* @exception UTFDataFormatException If the data is not in UTF-8 format |
* @exception UTFDataFormatException If the data is not in UTF-8 format |
708 |
* @exception IOException If any other error occurs |
* @exception IOException If any other error occurs |
709 |
* |
* |
710 |
* @see DataOutput |
* @see DataOutput |
711 |
*/ |
*/ |
712 |
public synchronized final String readUTF() |
public synchronized final String readUTF() |
713 |
throws EOFException, UTFDataFormatException, IOException |
throws EOFException, UTFDataFormatException, IOException |
714 |
{ |
{ |
722 |
return(DataInputStream.convertFromUTF(buf)); |
return(DataInputStream.convertFromUTF(buf)); |
723 |
} |
} |
724 |
|
|
|
/*************************************************************************/ |
|
|
|
|
725 |
/** |
/** |
726 |
* This method reads raw bytes into the passed array until the array is |
* This method reads raw bytes into the passed array until the array is |
727 |
* full. Note that this method blocks until the data is available and |
* full. Note that this method blocks until the data is available and |
728 |
* throws an exception if there is not enough data left in the stream to |
* throws an exception if there is not enough data left in the stream to |
729 |
* fill the buffer |
* fill the buffer |
730 |
* |
* |
731 |
* @param buf The buffer into which to read the data |
* @param buf The buffer into which to read the data |
732 |
* |
* |
733 |
* @exception EOFException If end of file is reached before filling the |
* @exception EOFException If end of file is reached before filling the |
734 |
* buffer |
* buffer |
735 |
* @exception IOException If any other error occurs |
* @exception IOException If any other error occurs |
736 |
*/ |
*/ |
737 |
public final void readFully(byte[] buf) throws EOFException, IOException |
public final void readFully(byte[] buf) throws EOFException, IOException |
738 |
{ |
{ |
739 |
readFully(buf, 0, buf.length); |
readFully(buf, 0, buf.length); |
740 |
} |
} |
741 |
|
|
|
/*************************************************************************/ |
|
|
|
|
742 |
/** |
/** |
743 |
* This method reads raw bytes into the passed array <code>buf</code> |
* This method reads raw bytes into the passed array <code>buf</code> |
744 |
* starting |
* starting |
745 |
* <code>offset</code> bytes into the buffer. The number of bytes read |
* <code>offset</code> bytes into the buffer. The number of bytes read |
746 |
* will be |
* will be |
747 |
* exactly <code>len</code> Note that this method blocks until the data is |
* exactly <code>len</code> Note that this method blocks until the data is |
748 |
* available and throws an exception if there is not enough data left in |
* available and throws an exception if there is not enough data left in |
749 |
* the stream to read <code>len</code> bytes. |
* the stream to read <code>len</code> bytes. |
750 |
* |
* |
751 |
* @param buf The buffer into which to read the data |
* @param buf The buffer into which to read the data |
752 |
* @param offset The offset into the buffer to start storing data |
* @param offset The offset into the buffer to start storing data |
753 |
* @param len The number of bytes to read into the buffer |
* @param len The number of bytes to read into the buffer |
754 |
* |
* |
755 |
* @exception EOFException If end of file is reached before filling |
* @exception EOFException If end of file is reached before filling |
756 |
* the buffer |
* the buffer |
757 |
* @exception IOException If any other error occurs |
* @exception IOException If any other error occurs |
758 |
*/ |
*/ |
759 |
public synchronized final void readFully(byte[] buf, int offset, int len) |
public synchronized final void readFully(byte[] buf, int offset, int len) |
760 |
throws EOFException, IOException |
throws EOFException, IOException |
761 |
{ |
{ |
771 |
} |
} |
772 |
} |
} |
773 |
|
|
|
/*************************************************************************/ |
|
|
|
|
774 |
/** |
/** |
775 |
* This method attempts to skip and discard the specified number of bytes |
* This method attempts to skip and discard the specified number of bytes |
776 |
* in the input stream. It may actually skip fewer bytes than requested. |
* in the input stream. It may actually skip fewer bytes than requested. |
777 |
* The actual number of bytes skipped is returned. This method will not |
* The actual number of bytes skipped is returned. This method will not |
778 |
* skip any bytes if passed a negative number of bytes to skip. |
* skip any bytes if passed a negative number of bytes to skip. |
779 |
* |
* |
780 |
* @param numBytes The requested number of bytes to skip. |
* @param numBytes The requested number of bytes to skip. |
781 |
* |
* |
782 |
* @return The number of bytes actually skipped. |
* @return The number of bytes actually skipped. |
783 |
* |
* |
784 |
* @exception IOException If an error occurs. |
* @exception IOException If an error occurs. |
785 |
*/ |
*/ |
786 |
public int skipBytes(int numBytes) throws EOFException, IOException |
public int skipBytes(int numBytes) throws EOFException, IOException |
787 |
{ |
{ |
788 |
if (numBytes < 0) |
if (numBytes < 0) |
798 |
return((int)(newPos-curPos)); |
return((int)(newPos-curPos)); |
799 |
} |
} |
800 |
|
|
|
/*************************************************************************/ |
|
|
|
|
801 |
/** |
/** |
802 |
* This method writes a single byte of data to the file. The file must |
* This method writes a single byte of data to the file. The file must |
803 |
* be open for read-write in order for this operation to succeed. |
* be open for read-write in order for this operation to succeed. |
804 |
* |
* |
805 |
* @param The byte of data to write, passed as an int. |
* @param The byte of data to write, passed as an int. |
806 |
* |
* |
807 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
808 |
*/ |
*/ |
809 |
public void write(int b) throws IOException |
public void write(int b) throws IOException |
810 |
{ |
{ |
811 |
if (readOnly) |
if (readOnly) |
814 |
fd.write(b); |
fd.write(b); |
815 |
} |
} |
816 |
|
|
|
/*************************************************************************/ |
|
|
|
|
817 |
/** |
/** |
818 |
* This method writes all the bytes in the specified array to the file. |
* This method writes all the bytes in the specified array to the file. |
819 |
* The file must be open read-write in order for this operation to succeed. |
* The file must be open read-write in order for this operation to succeed. |
820 |
* |
* |
821 |
* @param buf The array of bytes to write to the file |
* @param buf The array of bytes to write to the file |
822 |
*/ |
*/ |
823 |
public void write(byte[] buf) throws IOException |
public void write(byte[] buf) throws IOException |
824 |
{ |
{ |
825 |
write(buf, 0, buf.length); |
write(buf, 0, buf.length); |
826 |
} |
} |
827 |
|
|
|
/*************************************************************************/ |
|
|
|
|
828 |
/** |
/** |
829 |
* This method writes <code>len</code> bytes to the file from the specified |
* This method writes <code>len</code> bytes to the file from the specified |
830 |
* array starting at index <code>offset</code> into the array. |
* array starting at index <code>offset</code> into the array. |
831 |
* |
* |
832 |
* @param buf The array of bytes to write to the file |
* @param buf The array of bytes to write to the file |
833 |
* @param offset The index into the array to start writing file |
* @param offset The index into the array to start writing file |
834 |
* @param len The number of bytes to write |
* @param len The number of bytes to write |
835 |
* |
* |
836 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
837 |
*/ |
*/ |
838 |
public void write(byte[] buf, int offset, int len) throws IOException |
public void write(byte[] buf, int offset, int len) throws IOException |
839 |
{ |
{ |
840 |
if (readOnly) |
if (readOnly) |
843 |
fd.write(buf, offset, len); |
fd.write(buf, offset, len); |
844 |
} |
} |
845 |
|
|
|
/*************************************************************************/ |
|
|
|
|
846 |
/** |
/** |
847 |
* This method writes a Java <code>boolean</code> to the underlying output |
* This method writes a Java <code>boolean</code> to the underlying output |
848 |
* 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. |
849 |
* For a value of <code>false</code>, 0 is written. |
* For a value of <code>false</code>, 0 is written. |
850 |
* |
* |
851 |
* @param b The <code>boolean</code> value to write to the stream |
* @param b The <code>boolean</code> value to write to the stream |
852 |
* |
* |
853 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
854 |
*/ |
*/ |
855 |
public final void writeBoolean(boolean b) throws IOException |
public final void writeBoolean(boolean b) throws IOException |
856 |
{ |
{ |
857 |
if (b) |
if (b) |
860 |
write(0); |
write(0); |
861 |
} |
} |
862 |
|
|
|
/*************************************************************************/ |
|
|
|
|
863 |
/** |
/** |
864 |
* This method writes a Java <code>byte</code> value to the underlying |
* This method writes a Java <code>byte</code> value to the underlying |
865 |
* output stream. |
* output stream. |
866 |
* |
* |
867 |
* @param b The <code>byte</code> to write to the stream, passed |
* @param b The <code>byte</code> to write to the stream, passed |
868 |
* as an <code>int</code>. |
* as an <code>int</code>. |
869 |
* |
* |
870 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
871 |
*/ |
*/ |
872 |
public final void writeByte(int b) throws IOException |
public final void writeByte(int b) throws IOException |
873 |
{ |
{ |
874 |
write(b & 0xFF); |
write(b & 0xFF); |
875 |
} |
} |
876 |
|
|
|
/*************************************************************************/ |
|
|
|
|
877 |
/** |
/** |
878 |
* 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 |
879 |
* stream. One byte is written for each character in the <code>String</code>. |
* stream. One byte is written for each character in the <code>String</code>. |
880 |
* The high eight bits of each character are discarded. |
* The high eight bits of each character are discarded. |
881 |
* |
* |
882 |
* @param s The <code>String</code> to write to the stream |
* @param s The <code>String</code> to write to the stream |
883 |
* |
* |
884 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
885 |
*/ |
*/ |
886 |
public final void writeBytes(String s) throws IOException |
public final void writeBytes(String s) throws IOException |
887 |
{ |
{ |
888 |
int len = s.length(); |
int len = s.length(); |
898 |
write(buf); |
write(buf); |
899 |
} |
} |
900 |
|
|
|
/*************************************************************************/ |
|
|
|
|
901 |
/** |
/** |
902 |
* This method writes a single <code>char</code> value to the stream, |
* This method writes a single <code>char</code> value to the stream, |
903 |
* high byte first. |
* high byte first. |
904 |
* |
* |
905 |
* @param c The <code>char</code> value to write, passed as |
* @param c The <code>char</code> value to write, passed as |
906 |
* an <code>int</code>. |
* an <code>int</code>. |
907 |
* |
* |
908 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
909 |
*/ |
*/ |
910 |
public final synchronized void writeChar(int c) throws IOException |
public final synchronized void writeChar(int c) throws IOException |
911 |
{ |
{ |
912 |
buf[0] = (byte)((c & 0xFF00) >> 8); |
buf[0] = (byte)((c & 0xFF00) >> 8); |
915 |
write(buf, 0, 2); |
write(buf, 0, 2); |
916 |
} |
} |
917 |
|
|
|
/*************************************************************************/ |
|
|
|
|
918 |
/** |
/** |
919 |
* 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 |
920 |
* stream. There will be two bytes for each character value. The high |
* stream. There will be two bytes for each character value. The high |
921 |
* byte of the character will be written first. |
* byte of the character will be written first. |
922 |
* |
* |
923 |
* @param s The <code>String</code> to write to the stream. |
* @param s The <code>String</code> to write to the stream. |
924 |
* |
* |
925 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
926 |
*/ |
*/ |
927 |
public final void writeChars(String s) throws IOException |
public final void writeChars(String s) throws IOException |
928 |
{ |
{ |
929 |
int len = s.length(); |
int len = s.length(); |
941 |
write(buf, 0, buf.length); |
write(buf, 0, buf.length); |
942 |
} |
} |
943 |
|
|
|
/*************************************************************************/ |
|
|
|
|
944 |
/** |
/** |
945 |
* 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 |
946 |
* first. This method requires two bytes to encode the value. |
* first. This method requires two bytes to encode the value. |
947 |
* |
* |
948 |
* @param s The <code>short</code> value to write to the stream, |
* @param s The <code>short</code> value to write to the stream, |
949 |
* passed as an <code>int</code>. |
* passed as an <code>int</code>. |
950 |
* |
* |
951 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
952 |
*/ |
*/ |
953 |
public final synchronized void writeShort(int s) throws IOException |
public final synchronized void writeShort(int s) throws IOException |
954 |
{ |
{ |
955 |
buf[0] = (byte)((s & 0xFF00) >> 8); |
buf[0] = (byte)((s & 0xFF00) >> 8); |
958 |
write(buf, 0, 2); |
write(buf, 0, 2); |
959 |
} |
} |
960 |
|
|
|
/*************************************************************************/ |
|
|
|
|
961 |
/** |
/** |
962 |
* 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 |
963 |
* first. This method requires four bytes to encode the value. |
* first. This method requires four bytes to encode the value. |
964 |
* |
* |
965 |
* @param i The <code>int</code> value to write to the stream. |
* @param i The <code>int</code> value to write to the stream. |
966 |
* |
* |
967 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
968 |
*/ |
*/ |
969 |
public final synchronized void writeInt(int i) throws IOException |
public final synchronized void writeInt(int i) throws IOException |
970 |
{ |
{ |
971 |
buf[0] = (byte)((i & 0xFF000000) >> 24); |
buf[0] = (byte)((i & 0xFF000000) >> 24); |
976 |
write(buf, 0, 4); |
write(buf, 0, 4); |
977 |
} |
} |
978 |
|
|
|
/*************************************************************************/ |
|
|
|
|
979 |
/** |
/** |
980 |
* 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 |
981 |
* first. This method requires eight bytes to encode the value. |
* first. This method requires eight bytes to encode the value. |
982 |
* |
* |
983 |
* @param l The <code>long</code> value to write to the stream. |
* @param l The <code>long</code> value to write to the stream. |
984 |
* |
* |
985 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
986 |
*/ |
*/ |
987 |
public final synchronized void writeLong(long l) throws IOException |
public final synchronized void writeLong(long l) throws IOException |
988 |
{ |
{ |
989 |
buf[0] = (byte)((l & 0xFF00000000000000L) >> 56); |
buf[0] = (byte)((l & 0xFF00000000000000L) >> 56); |
998 |
write(buf, 0, 8); |
write(buf, 0, 8); |
999 |
} |
} |
1000 |
|
|
|
/*************************************************************************/ |
|
|
|
|
1001 |
/** |
/** |
1002 |
* 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 |
1003 |
* value is written by first calling the method |
* value is written by first calling the method |
1004 |
* <code>Float.floatToIntBits</code> |
* <code>Float.floatToIntBits</code> |
1005 |
* to retrieve an <code>int</code> representing the floating point number, |
* to retrieve an <code>int</code> representing the floating point number, |
1006 |
* 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 |
1007 |
* as the <code>writeInt()</code> method does. |
* as the <code>writeInt()</code> method does. |
1008 |
* |
* |
1009 |
* @param f The floating point number to write to the stream. |
* @param f The floating point number to write to the stream. |
1010 |
* |
* |
1011 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
1012 |
* |
* |
1013 |
* @see #writeInt(int) |
* @see #writeInt(int) |
1014 |
*/ |
*/ |
1015 |
public final void writeFloat(float f) throws IOException |
public final void writeFloat(float f) throws IOException |
1016 |
{ |
{ |
1017 |
int i = Float.floatToIntBits(f); |
int i = Float.floatToIntBits(f); |
1018 |
writeInt(i); |
writeInt(i); |
1019 |
} |
} |
1020 |
|
|
|
/*************************************************************************/ |
|
|
|
|
1021 |
/** |
/** |
1022 |
* 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 |
1023 |
* value is written by first calling the method |
* value is written by first calling the method |
1024 |
* <code>Double.doubleToLongBits</code> |
* <code>Double.doubleToLongBits</code> |
1025 |
* to retrieve an <code>long</code> representing the floating point number, |
* to retrieve an <code>long</code> representing the floating point number, |
1026 |
* 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 |
1027 |
* as the <code>writeLong()</code> method does. |
* as the <code>writeLong()</code> method does. |
1028 |
* |
* |
1029 |
* @param d The double precision floating point number to write to the |
* @param d The double precision floating point number to write to the |
1030 |
* stream. |
* stream. |
1031 |
* |
* |
1032 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
1033 |
* |
* |
1034 |
* @see #writeLong(long) |
* @see #writeLong(long) |
1035 |
*/ |
*/ |
1036 |
public final void writeDouble(double d) throws IOException |
public final void writeDouble(double d) throws IOException |
1037 |
{ |
{ |
1038 |
long l = Double.doubleToLongBits(d); |
long l = Double.doubleToLongBits(d); |
1039 |
writeLong(l); |
writeLong(l); |
1040 |
} |
} |
1041 |
|
|
|
/*************************************************************************/ |
|
|
|
|
1042 |
/** |
/** |
1043 |
* 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 |
1044 |
* 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 |
1045 |
* 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 |
1046 |
* encoded <code>String</code> not the <code>String</code> length. Next |
* encoded <code>String</code> not the <code>String</code> length. Next |
1047 |
* come the encoded characters. Each character in the <code>String</code> |
* come the encoded characters. Each character in the <code>String</code> |
1048 |
* 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 |
1049 |
* range of <code>\u0001</code> to <code>\u007F</code>, |
* range of <code>\u0001</code> to <code>\u007F</code>, |
1050 |
* one byte is used. The character |
* one byte is used. The character |
1051 |
* 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 |
1052 |
* of <code>\u0080</code> to <code>\u007FF</code>, two |
* of <code>\u0080</code> to <code>\u007FF</code>, two |
1053 |
* bytes are used. Bits |
* bytes are used. Bits |
1054 |
* 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 |
1055 |
* 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 |
1056 |
* 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 |
1057 |
* "10". This type of encoding is also done for the null character |
* "10". This type of encoding is also done for the null character |
1058 |
* <code>\u0000</code>. This eliminates any C style NUL character values |
* <code>\u0000</code>. This eliminates any C style NUL character values |
1059 |
* in the output. All remaining characters are stored as three bytes. |
* in the output. All remaining characters are stored as three bytes. |
1060 |
* 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 |
1061 |
* 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 |
1062 |
* 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 |
1063 |
* 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 |
1064 |
* 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 |
1065 |
* of that byte set to "10". |
* of that byte set to "10". |
1066 |
* |
* |
1067 |
* @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 |
1068 |
* |
* |
1069 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
1070 |
*/ |
*/ |
1071 |
public final void writeUTF(String s) throws IOException |
public final void writeUTF(String s) throws IOException |
1072 |
{ |
{ |
1073 |
// FIXME: Look to migrate to s.getBytes("UTF-8") if performance ok |
// FIXME: Look to migrate to s.getBytes("UTF-8") if performance ok |
1077 |
write(buf); |
write(buf); |
1078 |
} |
} |
1079 |
|
|
|
/*************************************************************************/ |
|
|
|
|
1080 |
/** |
/** |
1081 |
* This method creates a java.nio.channels.FileChannel. |
* This method creates a java.nio.channels.FileChannel. |
1082 |
* Nio does not allow one to create a file channel directly. |
* Nio does not allow one to create a file channel directly. |