Methods Summary |
---|
public void | close()Closes this file.
// BEGIN android-changed
synchronized (this) {
if (channel != null && channel.isOpen()) {
channel.close();
channel = null;
}
if (fd != null && fd.descriptor >= 0) {
fileSystem.close(fd.descriptor);
fd.descriptor = -1;
}
}
// END android-changed
|
public final java.nio.channels.FileChannel | getChannel()Gets this file's {@link FileChannel} object.
The file channel's {@link FileChannel.#position() position} is the same
as this file's file pointer offset (see {@link #getFilePointer()}). Any
changes made to this file's file pointer offset are also visible in the
file channel's position and vice versa.
synchronized(this) {
if(channel == null) {
channel = FileChannelFactory.getFileChannel(this, fd.descriptor,
options);
}
return channel;
}
|
public final java.io.FileDescriptor | getFD()Gets this file's {@link FileDescriptor}. This represents the operating
system resource for this random access file.
return fd;
|
public long | getFilePointer()Gets the current position within this file. All reads and
writes take place at the current file pointer position.
openCheck();
return fileSystem.seek(fd.descriptor, 0L, IFileSystem.SEEK_CUR);
|
public long | length()Returns the length of this file in bytes.
openCheck();
synchronized (repositionLock) {
long currentPosition = fileSystem.seek(fd.descriptor, 0L,
IFileSystem.SEEK_CUR);
long endOfFilePosition = fileSystem.seek(fd.descriptor, 0L,
IFileSystem.SEEK_END);
fileSystem.seek(fd.descriptor, currentPosition,
IFileSystem.SEEK_SET);
return endOfFilePosition;
}
|
private synchronized void | openCheck()Checks to see if the file is currently open. Returns silently if it is,
and throws an exception if it is not.
if (fd.descriptor < 0) {
throw new IOException();
}
|
public int | read(byte[] buffer)Reads bytes from the current position in this file and stores them in the
byte array {@code buffer}. The maximum number of bytes read corresponds
to the size of {@code buffer}. Blocks until at least one byte has been
read.
return read(buffer, 0, buffer.length);
|
public int | read(byte[] buffer, int offset, int count)Reads at most {@code count} bytes from the current position in this file
and stores them in the byte array {@code buffer} starting at {@code
offset}. Blocks until {@code count} bytes have been read, the end of the
file is reached or an exception is thrown.
// have to have four comparisions to not miss integer overflow cases
// BEGIN android-changed
// Exception priorities (in case of multiple errors) differ from
// RI, but are spec-compliant.
// made implicit null check explicit, used (offset | count) < 0
// instead of (offset < 0) || (count < 0) to safe one operation
if (buffer == null) {
throw new NullPointerException(Msg.getString("K0047")); //$NON-NLS-1$
}
if ((offset | count) < 0 || count > buffer.length - offset) {
throw new IndexOutOfBoundsException(Msg.getString("K002f")); //$NON-NLS-1$
}
// END android-changed
if (0 == count) {
return 0;
}
openCheck();
synchronized (repositionLock) {
return (int) fileSystem.read(fd.descriptor, buffer, offset, count);
}
|
public int | read()Reads a single byte from the current position in this file and returns it
as an integer in the range from 0 to 255. Returns -1 if the end of the
file has been reached. Blocks until one byte has been read, the end of
the file is detected or an exception is thrown.
openCheck();
byte[] bytes = new byte[1];
synchronized (repositionLock) {
long readed = fileSystem.read(fd.descriptor, bytes, 0, 1);
return readed == -1 ? -1 : bytes[0] & 0xff;
}
|
public final boolean | readBoolean()Reads a boolean from the current position in this file. Blocks until one
byte has been read, the end of the file is reached or an exception is
thrown.
int temp = this.read();
if (temp < 0) {
throw new EOFException();
}
return temp != 0;
|
public final byte | readByte()Reads an 8-bit byte from the current position in this file. Blocks until
one byte has been read, the end of the file is reached or an exception is
thrown.
int temp = this.read();
if (temp < 0) {
throw new EOFException();
}
return (byte) temp;
|
public final char | readChar()Reads a 16-bit character from the current position in this file. Blocks until
two bytes have been read, the end of the file is reached or an exception is
thrown.
byte[] buffer = new byte[2];
if (read(buffer, 0, buffer.length) != buffer.length) {
throw new EOFException();
}
return (char) (((buffer[0] & 0xff) << 8) + (buffer[1] & 0xff));
|
public final double | readDouble()Reads a 64-bit double from the current position in this file. Blocks
until eight bytes have been read, the end of the file is reached or an
exception is thrown.
return Double.longBitsToDouble(readLong());
|
public final float | readFloat()Reads a 32-bit float from the current position in this file. Blocks
until four bytes have been read, the end of the file is reached or an
exception is thrown.
return Float.intBitsToFloat(readInt());
|
public final void | readFully(byte[] buffer)Reads bytes from this file into {@code buffer}. Blocks until {@code
buffer.length} number of bytes have been read, the end of the file is
reached or an exception is thrown.
readFully(buffer, 0, buffer.length);
|
public final void | readFully(byte[] buffer, int offset, int count)Read bytes from this file into {@code buffer} starting at offset {@code
offset}. This method blocks until {@code count} number of bytes have been
read.
if (buffer == null) {
throw new NullPointerException(Msg.getString("K0047")); //$NON-NLS-1$
}
// avoid int overflow
// BEGIN android-changed
// Exception priorities (in case of multiple errors) differ from
// RI, but are spec-compliant.
// removed redundant check, used (offset | count) < 0
// instead of (offset < 0) || (count < 0) to safe one operation
if ((offset | count) < 0 || count > buffer.length - offset) {
throw new IndexOutOfBoundsException(Msg.getString("K002f")); //$NON-NLS-1$
}
// END android-changed
while (count > 0) {
int result = read(buffer, offset, count);
if (result < 0) {
throw new EOFException();
}
offset += result;
count -= result;
}
|
public final int | readInt()Reads a 32-bit integer from the current position in this file. Blocks
until four bytes have been read, the end of the file is reached or an
exception is thrown.
byte[] buffer = new byte[4];
if (read(buffer, 0, buffer.length) != buffer.length) {
throw new EOFException();
}
return ((buffer[0] & 0xff) << 24) + ((buffer[1] & 0xff) << 16)
+ ((buffer[2] & 0xff) << 8) + (buffer[3] & 0xff);
|
public final java.lang.String | readLine()Reads a line of text form the current position in this file. A line is
represented by zero or more characters followed by {@code '\n'}, {@code
'\r'}, {@code "\r\n"} or the end of file marker. The string does not
include the line terminating sequence.
Blocks until a line terminating sequence has been read, the end of the
file is reached or an exception is thrown.
StringBuilder line = new StringBuilder(80); // Typical line length
boolean foundTerminator = false;
long unreadPosition = 0;
while (true) {
int nextByte = read();
switch (nextByte) {
case -1:
return line.length() != 0 ? line.toString() : null;
case (byte) '\r":
if (foundTerminator) {
seek(unreadPosition);
return line.toString();
}
foundTerminator = true;
/* Have to be able to peek ahead one byte */
unreadPosition = getFilePointer();
break;
case (byte) '\n":
return line.toString();
default:
if (foundTerminator) {
seek(unreadPosition);
return line.toString();
}
line.append((char) nextByte);
}
}
|
public final long | readLong()Reads a 64-bit long from the current position in this file. Blocks until
eight bytes have been read, the end of the file is reached or an
exception is thrown.
byte[] buffer = new byte[8];
if (read(buffer, 0, buffer.length) != buffer.length) {
throw new EOFException();
}
return ((long) (((buffer[0] & 0xff) << 24) + ((buffer[1] & 0xff) << 16)
+ ((buffer[2] & 0xff) << 8) + (buffer[3] & 0xff)) << 32)
+ ((long) (buffer[4] & 0xff) << 24)
+ ((buffer[5] & 0xff) << 16)
+ ((buffer[6] & 0xff) << 8)
+ (buffer[7] & 0xff);
|
public final short | readShort()Reads a 16-bit short from the current position in this file. Blocks until
two bytes have been read, the end of the file is reached or an exception
is thrown.
byte[] buffer = new byte[2];
if (read(buffer, 0, buffer.length) != buffer.length) {
throw new EOFException();
}
return (short) (((buffer[0] & 0xff) << 8) + (buffer[1] & 0xff));
|
public final java.lang.String | readUTF()Reads a string that is encoded in {@link DataInput modified UTF-8} from
this file. The number of bytes that must be read for the complete string
is determined by the first two bytes read from the file. Blocks until all
required bytes have been read, the end of the file is reached or an
exception is thrown.
int utfSize = readUnsignedShort();
if (utfSize == 0) {
return ""; //$NON-NLS-1$
}
byte[] buf = new byte[utfSize];
if (read(buf, 0, buf.length) != buf.length) {
throw new EOFException();
}
return Util.convertFromUTF8(buf, 0, utfSize);
|
public final int | readUnsignedByte()Reads an unsigned 8-bit byte from the current position in this file and
returns it as an integer. Blocks until one byte has been read, the end of
the file is reached or an exception is thrown.
int temp = this.read();
if (temp < 0) {
throw new EOFException();
}
return temp;
|
public final int | readUnsignedShort()Reads an unsigned 16-bit short from the current position in this file and
returns it as an integer. Blocks until two bytes have been read, the end of
the file is reached or an exception is thrown.
byte[] buffer = new byte[2];
if (read(buffer, 0, buffer.length) != buffer.length) {
throw new EOFException();
}
return ((buffer[0] & 0xff) << 8) + (buffer[1] & 0xff);
|
public void | seek(long pos)Moves this file's file pointer to a new position, from where following
{@code read}, {@code write} or {@code skip} operations are done. The
position may be greater than the current length of the file, but the
file's length will only change if the moving of the pointer is followed
by a {@code write} operation.
if (pos < 0) {
// seek position is negative
throw new IOException(Msg.getString("K0347")); //$NON-NLS-1$
}
openCheck();
synchronized (repositionLock) {
fileSystem.seek(fd.descriptor, pos, IFileSystem.SEEK_SET);
}
|
public void | setLength(long newLength)Sets the length of this file to {@code newLength}. If the current file is
smaller, it is expanded but the contents from the previous end of the
file to the new end are undefined. The file is truncated if its current
size is bigger than {@code newLength}. If the current file pointer
position is in the truncated part, it is set to the end of the file.
openCheck();
if (newLength < 0) {
throw new IllegalArgumentException();
}
synchronized (repositionLock) {
long position = fileSystem.seek(fd.descriptor, 0,
IFileSystem.SEEK_CUR);
fileSystem.truncate(fd.descriptor, newLength);
seek(position > newLength ? newLength : position);
}
// if we are in "rws" mode, attempt to sync file+metadata
if (syncMetadata) {
fd.sync();
}
|
public int | skipBytes(int count)Skips over {@code count} bytes in this file. Less than {@code count}
bytes are skipped if the end of the file is reached or an exception is
thrown during the operation. Nothing is done if {@code count} is
negative.
if (count > 0) {
long currentPos = getFilePointer(), eof = length();
int newCount = (int) ((currentPos + count > eof) ? eof - currentPos
: count);
seek(currentPos + newCount);
return newCount;
}
return 0;
|
public void | write(byte[] buffer)Writes the entire contents of the byte array {@code buffer} to this file,
starting at the current file pointer.
write(buffer, 0, buffer.length);
|
public void | write(byte[] buffer, int offset, int count)Writes {@code count} bytes from the byte array {@code buffer} to this
file, starting at the current file pointer and using {@code offset} as
the first position within {@code buffer} to get bytes.
// BEGIN android-changed
// Exception priorities (in case of multiple errors) differ from
// RI, but are spec-compliant.
// made implicit null check explicit,
// removed redundant check, used (offset | count) < 0
// instead of (offset < 0) || (count < 0) to safe one operation
if (buffer == null) {
throw new NullPointerException(Msg.getString("K0047")); //$NON-NLS-1$
}
if ((offset | count) < 0 || count > buffer.length - offset) {
throw new IndexOutOfBoundsException(Msg.getString("K002f")); //$NON-NLS-1$
}
// END android-changed
if (count == 0) {
return;
}
// BEGIN android-added
openCheck();
// END android-added
synchronized (repositionLock) {
fileSystem.write(fd.descriptor, buffer, offset, count);
}
// if we are in "rws" mode, attempt to sync file+metadata
if (syncMetadata) {
fd.sync();
}
|
public void | write(int oneByte)Writes a byte to this file, starting at the current file pointer. Only
the least significant byte of the integer {@code oneByte} is written.
openCheck();
byte[] bytes = new byte[1];
bytes[0] = (byte) (oneByte & 0xff);
synchronized (repositionLock) {
fileSystem.write(fd.descriptor, bytes, 0, 1);
}
// if we are in "rws" mode, attempt to sync file+metadata
if (syncMetadata) {
fd.sync();
}
|
public final void | writeBoolean(boolean val)Writes a boolean to this file, starting at the current file pointer.
write(val ? 1 : 0);
|
public final void | writeByte(int val)Writes an 8-bit byte to this file, starting at the current file pointer.
Only the least significant byte of the integer {@code val} is written.
write(val & 0xFF);
|
public final void | writeBytes(java.lang.String str)Writes the low order 8-bit bytes from a string to this file, starting at
the current file pointer.
byte bytes[] = new byte[str.length()];
for (int index = 0; index < str.length(); index++) {
bytes[index] = (byte) (str.charAt(index) & 0xFF);
}
write(bytes);
|
public final void | writeChar(int val)Writes a 16-bit character to this file, starting at the current file
pointer. Only the two least significant bytes of the integer {@code val}
are written, with the high byte first.
byte[] buffer = new byte[2];
buffer[0] = (byte) (val >> 8);
buffer[1] = (byte) val;
write(buffer, 0, buffer.length);
|
public final void | writeChars(java.lang.String str)Writes the 16-bit characters from a string to this file, starting at the
current file pointer. Each character is written in the same way as with
{@link #writeChar(int)}, with its high byte first.
byte newBytes[] = new byte[str.length() * 2];
for (int index = 0; index < str.length(); index++) {
int newIndex = index == 0 ? index : index * 2;
newBytes[newIndex] = (byte) ((str.charAt(index) >> 8) & 0xFF);
newBytes[newIndex + 1] = (byte) (str.charAt(index) & 0xFF);
}
write(newBytes);
|
public final void | writeDouble(double val)Writes a 64-bit double to this file, starting at the current file
pointer. The eight bytes returned by
{@link Double#doubleToLongBits(double)} are written to this file.
writeLong(Double.doubleToLongBits(val));
|
public final void | writeFloat(float val)Writes a 32-bit float to this file, starting at the current file pointer.
The four bytes returned by {@link Float#floatToIntBits(float)} are
written to this file.
writeInt(Float.floatToIntBits(val));
|
public final void | writeInt(int val)Writes a 32-bit integer to this file, starting at the current file
pointer. The four bytes of the integer are written with the highest byte
first.
byte[] buffer = new byte[4];
buffer[0] = (byte) (val >> 24);
buffer[1] = (byte) (val >> 16);
buffer[2] = (byte) (val >> 8);
buffer[3] = (byte) val;
write(buffer, 0, buffer.length);
|
public final void | writeLong(long val)Writes a 64-bit long to this file, starting at the current file
pointer. The eight bytes of the long are written with the highest byte
first.
byte[] buffer = new byte[8];
int t = (int) (val >> 32);
buffer[0] = (byte) (t >> 24);
buffer[1] = (byte) (t >> 16);
buffer[2] = (byte) (t >> 8);
buffer[3] = (byte) t;
buffer[4] = (byte) (val >> 24);
buffer[5] = (byte) (val >> 16);
buffer[6] = (byte) (val >> 8);
buffer[7] = (byte) val;
write(buffer, 0, buffer.length);
|
public final void | writeShort(int val)Writes a 16-bit short to this file, starting at the current file
pointer. Only the two least significant bytes of the integer {@code val}
are written, with the high byte first.
writeChar(val);
|
public final void | writeUTF(java.lang.String str)Writes a string encoded with {@link DataInput modified UTF-8} to this
file, starting at the current file pointer.
int utfCount = 0, length = str.length();
for (int i = 0; i < length; i++) {
int charValue = str.charAt(i);
if (charValue > 0 && charValue <= 127) {
utfCount++;
} else if (charValue <= 2047) {
utfCount += 2;
} else {
utfCount += 3;
}
}
if (utfCount > 65535) {
throw new UTFDataFormatException(Msg.getString("K0068")); //$NON-NLS-1$
}
byte utfBytes[] = new byte[utfCount + 2];
int utfIndex = 2;
for (int i = 0; i < length; i++) {
int charValue = str.charAt(i);
if (charValue > 0 && charValue <= 127) {
utfBytes[utfIndex++] = (byte) charValue;
} else if (charValue <= 2047) {
utfBytes[utfIndex++] = (byte) (0xc0 | (0x1f & (charValue >> 6)));
utfBytes[utfIndex++] = (byte) (0x80 | (0x3f & charValue));
} else {
utfBytes[utfIndex++] = (byte) (0xe0 | (0x0f & (charValue >> 12)));
utfBytes[utfIndex++] = (byte) (0x80 | (0x3f & (charValue >> 6)));
utfBytes[utfIndex++] = (byte) (0x80 | (0x3f & charValue));
}
}
utfBytes[0] = (byte) (utfCount >> 8);
utfBytes[1] = (byte) utfCount;
write(utfBytes);
|