62 |
|
|
63 |
// The underlying file. |
// The underlying file. |
64 |
private FileDescriptor fd; |
private FileDescriptor fd; |
65 |
|
// The corresponding input and output streams. |
66 |
|
private DataOutputStream out; |
67 |
|
private DataInputStream in; |
68 |
|
|
69 |
private FileChannel ch; /* cached associated file-channel */ |
private FileChannel ch; /* cached associated file-channel */ |
70 |
|
|
|
// Used for DataOutput methods writing values to the underlying file |
|
|
private byte[] buf = new byte[8]; |
|
|
|
|
71 |
/** |
/** |
72 |
* This method initializes a new instance of <code>RandomAccessFile</code> |
* This method initializes a new instance of <code>RandomAccessFile</code> |
73 |
* to read from the specified <code>File</code> object with the specified |
* to read from the specified <code>File</code> object with the specified |
122 |
fdmode = FileDescriptor.READ; |
fdmode = FileDescriptor.READ; |
123 |
else if (mode.equals("rw")) |
else if (mode.equals("rw")) |
124 |
fdmode = FileDescriptor.READ | FileDescriptor.WRITE; |
fdmode = FileDescriptor.READ | FileDescriptor.WRITE; |
125 |
else if (mode.equals("rws") || mode.equals("rwd")) |
else if (mode.equals("rws")) |
126 |
{ |
{ |
|
// FIXME: for now we treat rws and rwd and synonyms. |
|
127 |
fdmode = (FileDescriptor.READ | FileDescriptor.WRITE |
fdmode = (FileDescriptor.READ | FileDescriptor.WRITE |
128 |
| FileDescriptor.SYNC); |
| FileDescriptor.SYNC); |
129 |
} |
} |
130 |
|
else if (mode.equals("rwd")) |
131 |
|
{ |
132 |
|
fdmode = (FileDescriptor.READ | FileDescriptor.WRITE |
133 |
|
| FileDescriptor.DSYNC); |
134 |
|
} |
135 |
else |
else |
136 |
throw new IllegalArgumentException ("invalid mode: " + mode); |
throw new IllegalArgumentException ("invalid mode: " + mode); |
137 |
|
|
146 |
} |
} |
147 |
|
|
148 |
fd = new FileDescriptor (fileName, fdmode); |
fd = new FileDescriptor (fileName, fdmode); |
149 |
|
out = new DataOutputStream (new FileOutputStream (fd)); |
150 |
|
in = new DataInputStream (new FileInputStream (fd)); |
151 |
} |
} |
152 |
|
|
153 |
/** |
/** |
231 |
*/ |
*/ |
232 |
public int read () throws IOException |
public int read () throws IOException |
233 |
{ |
{ |
234 |
return fd.read(); |
return in.read(); |
235 |
} |
} |
236 |
|
|
237 |
/** |
/** |
264 |
*/ |
*/ |
265 |
public int read (byte[] buffer, int offset, int len) throws IOException |
public int read (byte[] buffer, int offset, int len) throws IOException |
266 |
{ |
{ |
267 |
return fd.read (buffer, offset, len); |
return in.read (buffer, offset, len); |
268 |
} |
} |
269 |
|
|
270 |
/** |
/** |
286 |
*/ |
*/ |
287 |
public final boolean readBoolean () throws IOException |
public final boolean readBoolean () throws IOException |
288 |
{ |
{ |
289 |
int byte_read = read(); |
return in.readBoolean (); |
|
|
|
|
if (byte_read == -1) |
|
|
throw new EOFException("Unexpected end of stream"); |
|
|
|
|
|
return byte_read != 0; |
|
290 |
} |
} |
291 |
|
|
292 |
/** |
/** |
306 |
*/ |
*/ |
307 |
public final byte readByte () throws IOException |
public final byte readByte () throws IOException |
308 |
{ |
{ |
309 |
int byte_read = read (); |
return in.readByte (); |
|
|
|
|
if (byte_read == -1) |
|
|
throw new EOFException ("Unexpected end of stream"); |
|
|
|
|
|
return (byte) byte_read; |
|
310 |
} |
} |
311 |
|
|
312 |
/** |
/** |
334 |
* |
* |
335 |
* @see DataOutput |
* @see DataOutput |
336 |
*/ |
*/ |
337 |
public final synchronized char readChar () throws IOException |
public final char readChar () throws IOException |
338 |
{ |
{ |
339 |
readFully (buf, 0, 2); |
return in.readChar(); |
|
|
|
|
return (char) ((buf[0] << 8) | (buf[1] & 0xff)); |
|
340 |
} |
} |
341 |
|
|
342 |
/** |
/** |
363 |
*/ |
*/ |
364 |
public final double readDouble () throws IOException |
public final double readDouble () throws IOException |
365 |
{ |
{ |
366 |
long val = readLong(); |
return in.readDouble (); |
|
|
|
|
return Double.longBitsToDouble(val); |
|
367 |
} |
} |
368 |
|
|
369 |
/** |
/** |
388 |
*/ |
*/ |
389 |
public final float readFloat () throws IOException |
public final float readFloat () throws IOException |
390 |
{ |
{ |
391 |
int val = readInt(); |
return in.readFloat(); |
|
|
|
|
return Float.intBitsToFloat(val); |
|
392 |
} |
} |
393 |
|
|
394 |
/** |
/** |
405 |
*/ |
*/ |
406 |
public final void readFully (byte[] buffer) throws IOException |
public final void readFully (byte[] buffer) throws IOException |
407 |
{ |
{ |
408 |
readFully (buffer, 0, buffer.length); |
in.readFully(buffer); |
409 |
} |
} |
410 |
|
|
411 |
/** |
/** |
425 |
* the buffer |
* the buffer |
426 |
* @exception IOException If any other error occurs |
* @exception IOException If any other error occurs |
427 |
*/ |
*/ |
428 |
public synchronized final void readFully (byte[] buffer, int offset, int len) |
public final void readFully (byte[] buffer, int offset, int count) |
429 |
throws IOException |
throws IOException |
430 |
{ |
{ |
431 |
int total_read = 0; |
in.readFully (buffer, offset, count); |
|
|
|
|
while (total_read < len) |
|
|
{ |
|
|
int bytes_read = read (buffer, offset + total_read, len - total_read); |
|
|
if (bytes_read == -1) |
|
|
throw new EOFException("Unexpected end of stream"); |
|
|
|
|
|
total_read += bytes_read; |
|
|
} |
|
432 |
} |
} |
433 |
|
|
434 |
/** |
/** |
459 |
* |
* |
460 |
* @see DataOutput |
* @see DataOutput |
461 |
*/ |
*/ |
462 |
public final synchronized int readInt() throws IOException |
public final int readInt () throws IOException |
463 |
{ |
{ |
464 |
readFully(buf, 0, 4); |
return in.readInt(); |
|
|
|
|
return (((buf[0] & 0xff) << 24) | ((buf[1] & 0xff) << 16) | |
|
|
((buf[2] & 0xff) << 8) | (buf[3] & 0xff)); |
|
465 |
} |
} |
466 |
|
|
467 |
/** |
/** |
491 |
* |
* |
492 |
* @deprecated |
* @deprecated |
493 |
*/ |
*/ |
494 |
public synchronized final String readLine () throws IOException |
public final String readLine () throws IOException |
495 |
{ |
{ |
496 |
StringBuffer sb = new StringBuffer (""); |
return in.readLine (); |
|
|
|
|
for (;;) |
|
|
{ |
|
|
int byte_read = read (); |
|
|
|
|
|
if (byte_read == -1) |
|
|
return sb.toString(); |
|
|
|
|
|
char c = (char) byte_read; |
|
|
|
|
|
if (c == '\r') |
|
|
{ |
|
|
byte_read = read(); |
|
|
if (((char)byte_read) != '\n') |
|
|
seek (getFilePointer() - 1); |
|
|
|
|
|
return sb.toString(); |
|
|
} |
|
|
|
|
|
if (c == '\n') |
|
|
return sb.toString(); |
|
|
|
|
|
sb.append (c); |
|
|
} |
|
497 |
} |
} |
498 |
|
|
499 |
/** |
/** |
527 |
* |
* |
528 |
* @see DataOutput |
* @see DataOutput |
529 |
*/ |
*/ |
530 |
public final synchronized long readLong() throws IOException |
public final long readLong () throws IOException |
531 |
{ |
{ |
532 |
readFully(buf, 0, 8); |
return in.readLong(); |
|
|
|
|
return (((long)(buf[0] & 0xff) << 56) | |
|
|
((long)(buf[1] & 0xff) << 48) | |
|
|
((long)(buf[2] & 0xff) << 40) | |
|
|
((long)(buf[3] & 0xff) << 32) | |
|
|
((long)(buf[4] & 0xff) << 24) | |
|
|
((long)(buf[5] & 0xff) << 16) | |
|
|
((long)(buf[6] & 0xff) << 8) | |
|
|
((long)(buf[7] & 0xff))); |
|
533 |
} |
} |
534 |
|
|
535 |
/** |
/** |
559 |
* |
* |
560 |
* @see DataOutput |
* @see DataOutput |
561 |
*/ |
*/ |
562 |
public final synchronized short readShort () throws IOException |
public final short readShort () throws IOException |
563 |
{ |
{ |
564 |
readFully (buf, 0, 2); |
return in.readShort(); |
|
|
|
|
return (short) ((buf[0] << 8) | (buf[1] & 0xff)); |
|
565 |
} |
} |
566 |
|
|
567 |
/** |
/** |
582 |
*/ |
*/ |
583 |
public final int readUnsignedByte () throws IOException |
public final int readUnsignedByte () throws IOException |
584 |
{ |
{ |
585 |
int byte_read = read (); |
return in.readUnsignedByte(); |
|
|
|
|
if (byte_read == -1) |
|
|
throw new EOFException ("Unexpected end of stream"); |
|
|
|
|
|
return byte_read & 0xFF; |
|
586 |
} |
} |
587 |
|
|
588 |
/** |
/** |
610 |
* @exception EOFException If end of file is reached before reading the value |
* @exception EOFException If end of file is reached before reading the value |
611 |
* @exception IOException If any other error occurs |
* @exception IOException If any other error occurs |
612 |
*/ |
*/ |
613 |
public final synchronized int readUnsignedShort () |
public final int readUnsignedShort () throws IOException |
|
throws IOException |
|
614 |
{ |
{ |
615 |
readFully(buf, 0, 2); |
return in.readUnsignedShort(); |
|
|
|
|
return (((buf[0] & 0xff) << 8) | (buf[1] & 0xff)); |
|
616 |
} |
} |
617 |
|
|
618 |
/** |
/** |
687 |
* |
* |
688 |
* @see DataOutput |
* @see DataOutput |
689 |
*/ |
*/ |
690 |
public synchronized final String readUTF () throws IOException |
public final String readUTF () throws IOException |
691 |
{ |
{ |
692 |
StringBuffer sb = new StringBuffer(""); |
return in.readUTF(); |
|
|
|
|
int num_bytes = readUnsignedShort(); |
|
|
byte[] buf = new byte[num_bytes]; |
|
|
readFully(buf); |
|
|
|
|
|
// FIXME: Look to migrate to new String(buf, "UTF-8") if performance ok |
|
|
return DataInputStream.convertFromUTF(buf); |
|
693 |
} |
} |
694 |
|
|
695 |
/** |
/** |
744 |
*/ |
*/ |
745 |
public void write (int oneByte) throws IOException |
public void write (int oneByte) throws IOException |
746 |
{ |
{ |
747 |
fd.write (oneByte); |
out.write(oneByte); |
748 |
} |
} |
749 |
|
|
750 |
/** |
/** |
755 |
*/ |
*/ |
756 |
public void write (byte[] buffer) throws IOException |
public void write (byte[] buffer) throws IOException |
757 |
{ |
{ |
758 |
write (buffer, 0, buffer.length); |
out.write(buffer); |
759 |
} |
} |
760 |
|
|
761 |
/** |
/** |
770 |
*/ |
*/ |
771 |
public void write (byte[] buffer, int offset, int len) throws IOException |
public void write (byte[] buffer, int offset, int len) throws IOException |
772 |
{ |
{ |
773 |
fd.write (buffer, offset, len); |
out.write (buffer, offset, len); |
774 |
} |
} |
775 |
|
|
776 |
/** |
/** |
784 |
*/ |
*/ |
785 |
public final void writeBoolean (boolean val) throws IOException |
public final void writeBoolean (boolean val) throws IOException |
786 |
{ |
{ |
787 |
write (val ? 1 : 0); |
out.writeBoolean(val); |
788 |
} |
} |
789 |
|
|
790 |
/** |
/** |
798 |
*/ |
*/ |
799 |
public final void writeByte (int v) throws IOException |
public final void writeByte (int v) throws IOException |
800 |
{ |
{ |
801 |
write (v & 0xFF); |
out.writeByte(v); |
802 |
} |
} |
803 |
|
|
804 |
/** |
/** |
810 |
* |
* |
811 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
812 |
*/ |
*/ |
813 |
public final synchronized void writeShort (int s) throws IOException |
public final void writeShort (int v) throws IOException |
814 |
{ |
{ |
815 |
buf[0] = (byte)((s & 0xFF00) >> 8); |
out.writeShort(v); |
|
buf[1] = (byte)(s & 0x00FF); |
|
|
|
|
|
write(buf, 0, 2); |
|
816 |
} |
} |
817 |
|
|
818 |
/** |
/** |
824 |
* |
* |
825 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
826 |
*/ |
*/ |
827 |
public final synchronized void writeChar (int v) throws IOException |
public final void writeChar (int v) throws IOException |
828 |
{ |
{ |
829 |
buf[0] = (byte)((v & 0xFF00) >> 8); |
out.writeChar(v); |
|
buf[1] = (byte)((int)v & 0x00FF); |
|
|
|
|
|
write(buf, 0, 2); |
|
830 |
} |
} |
831 |
|
|
832 |
/** |
/** |
837 |
* |
* |
838 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
839 |
*/ |
*/ |
840 |
public final synchronized void writeInt (int v) throws IOException |
public final void writeInt (int v) throws IOException |
841 |
{ |
{ |
842 |
buf[0] = (byte)((v & 0xFF000000) >> 24); |
out.writeInt(v); |
|
buf[1] = (byte)((v & 0x00FF0000) >> 16); |
|
|
buf[2] = (byte)((v & 0x0000FF00) >> 8); |
|
|
buf[3] = (byte)(v & 0x000000FF); |
|
|
|
|
|
write (buf, 0, 4); |
|
843 |
} |
} |
844 |
|
|
845 |
/** |
/** |
850 |
* |
* |
851 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
852 |
*/ |
*/ |
853 |
public final synchronized void writeLong (long v) throws IOException |
public final void writeLong (long v) throws IOException |
854 |
{ |
{ |
855 |
buf[0] = (byte)((v & 0xFF00000000000000L) >> 56); |
out.writeLong(v); |
|
buf[1] = (byte)((v & 0x00FF000000000000L) >> 48); |
|
|
buf[2] = (byte)((v & 0x0000FF0000000000L) >> 40); |
|
|
buf[3] = (byte)((v & 0x000000FF00000000L) >> 32); |
|
|
buf[4] = (byte)((v & 0x00000000FF000000L) >> 24); |
|
|
buf[5] = (byte)((v & 0x0000000000FF0000L) >> 16); |
|
|
buf[6] = (byte)((v & 0x000000000000FF00L) >> 8); |
|
|
buf[7] = (byte)(v & 0x00000000000000FFL); |
|
|
|
|
|
write (buf, 0, 8); |
|
856 |
} |
} |
857 |
|
|
858 |
/** |
/** |
871 |
*/ |
*/ |
872 |
public final void writeFloat (float v) throws IOException |
public final void writeFloat (float v) throws IOException |
873 |
{ |
{ |
874 |
int i = Float.floatToIntBits (v); |
out.writeFloat(v); |
|
writeInt (i); |
|
875 |
} |
} |
876 |
|
|
877 |
/** |
/** |
891 |
*/ |
*/ |
892 |
public final void writeDouble (double v) throws IOException |
public final void writeDouble (double v) throws IOException |
893 |
{ |
{ |
894 |
long l = Double.doubleToLongBits (v); |
out.writeDouble(v); |
|
writeLong (l); |
|
895 |
} |
} |
896 |
|
|
897 |
/** |
/** |
905 |
*/ |
*/ |
906 |
public final void writeBytes (String s) throws IOException |
public final void writeBytes (String s) throws IOException |
907 |
{ |
{ |
908 |
int len = s.length(); |
out.writeBytes(s); |
|
|
|
|
if (len == 0) |
|
|
return; |
|
|
|
|
|
byte[] buf = new byte[len]; |
|
|
|
|
|
for (int i = 0; i < len; i++) |
|
|
buf[i] = (byte)(s.charAt(i) & 0xFF); |
|
|
|
|
|
write(buf); |
|
909 |
} |
} |
910 |
|
|
911 |
/** |
/** |
919 |
*/ |
*/ |
920 |
public final void writeChars (String s) throws IOException |
public final void writeChars (String s) throws IOException |
921 |
{ |
{ |
922 |
int len = s.length(); |
out.writeChars(s); |
|
if (len == 0) |
|
|
return; |
|
|
|
|
|
byte[] buf = new byte[len * 2]; |
|
|
|
|
|
for (int i = 0; i < len; i++) |
|
|
{ |
|
|
buf[i * 2] = (byte)((s.charAt(i) & 0xFF00) >> 8); |
|
|
buf[(i * 2) + 1] = (byte)(s.charAt(i) & 0x00FF); |
|
|
} |
|
|
|
|
|
write(buf, 0, buf.length); |
|
923 |
} |
} |
924 |
|
|
925 |
/** |
/** |
953 |
*/ |
*/ |
954 |
public final void writeUTF (String s) throws IOException |
public final void writeUTF (String s) throws IOException |
955 |
{ |
{ |
956 |
// FIXME: Look to migrate to s.getBytes("UTF-8") if performance ok |
out.writeUTF(s); |
|
byte[] buf = DataOutputStream.convertToUTF(s); |
|
|
|
|
|
writeShort(buf.length); |
|
|
write(buf); |
|
957 |
} |
} |
958 |
|
|
959 |
/** |
/** |