Package org.apache.derby.impl.io.vfmem
Class VirtualRandomAccessFile
- java.lang.Object
-
- org.apache.derby.impl.io.vfmem.VirtualRandomAccessFile
-
- All Implemented Interfaces:
java.io.DataInput
,java.io.DataOutput
,StorageRandomAccessFile
public class VirtualRandomAccessFile extends java.lang.Object implements StorageRandomAccessFile
A random access file capable of reading and writing from/into a virtual file whose data is represented by aBlockedByteArray
.If the file is opened in read-only mode and the caller invokes one of the write methods, it will fail with a
NullPointerException
.
-
-
Field Summary
Fields Modifier and Type Field Description private boolean
_readOnly
whether the file is read-onlyprivate BlockedByteArrayInputStream
bIn
Stream used to read from the source entry.private BlockedByteArrayOutputStream
bOut
Stream used to write into the source entry.private java.io.DataInputStream
dIs
Data input stream on top of the source input stream.private java.io.DataOutputStream
dOs
Data output stream on top of the source output stream.private DataStoreEntry
entry
The source entry.private long
fp
Current position / file pointer.
-
Constructor Summary
Constructors Constructor Description VirtualRandomAccessFile(DataStoreEntry entry, boolean readOnly)
Creates a new virtual random access file.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description VirtualRandomAccessFile
clone()
Clone this file abstractionvoid
close()
Closes this file.long
getFilePointer()
Get the current offset in this file.long
length()
Gets the length of this file.int
read(byte[] b, int off, int len)
Reads up tolen
bytes of data from this file into an array of bytes.boolean
readBoolean()
byte
readByte()
char
readChar()
double
readDouble()
float
readFloat()
void
readFully(byte[] b)
void
readFully(byte[] b, int off, int len)
int
readInt()
java.lang.String
readLine()
long
readLong()
short
readShort()
int
readUnsignedByte()
int
readUnsignedShort()
java.lang.String
readUTF()
void
seek(long newFilePointer)
Set the file pointer.void
setLength(long newLength)
Sets the length of this file, either extending or truncating it.int
skipBytes(int n)
void
sync()
Force any changes out to the persistent store.void
write(byte[] b)
void
write(byte[] b, int off, int len)
void
write(int b)
void
writeBoolean(boolean v)
void
writeByte(int v)
void
writeBytes(java.lang.String s)
void
writeChar(int v)
void
writeChars(java.lang.String s)
void
writeDouble(double v)
void
writeFloat(float v)
void
writeInt(int v)
void
writeLong(long v)
void
writeShort(int v)
void
writeUTF(java.lang.String s)
-
-
-
Field Detail
-
entry
private final DataStoreEntry entry
The source entry.
-
_readOnly
private final boolean _readOnly
whether the file is read-only
-
fp
private long fp
Current position / file pointer.
-
bIn
private final BlockedByteArrayInputStream bIn
Stream used to read from the source entry.
-
dIs
private final java.io.DataInputStream dIs
Data input stream on top of the source input stream.
-
bOut
private final BlockedByteArrayOutputStream bOut
Stream used to write into the source entry. Will benull
if the file is opened in read-only mode.
-
dOs
private final java.io.DataOutputStream dOs
Data output stream on top of the source output stream. Will benull
if the file is opened in read-only mode.
-
-
Constructor Detail
-
VirtualRandomAccessFile
public VirtualRandomAccessFile(DataStoreEntry entry, boolean readOnly) throws java.io.FileNotFoundException
Creates a new virtual random access file.- Parameters:
entry
- the source entryreadOnly
- if the file should be opened read-only or not- Throws:
java.io.FileNotFoundException
- if the denoted path is a directory, or the denoted file has been marked read-only and the file is opened for writing
-
-
Method Detail
-
clone
public VirtualRandomAccessFile clone()
Description copied from interface:StorageRandomAccessFile
Clone this file abstraction- Specified by:
clone
in interfaceStorageRandomAccessFile
- Overrides:
clone
in classjava.lang.Object
-
close
public void close() throws java.io.IOException
Description copied from interface:StorageRandomAccessFile
Closes this file.- Specified by:
close
in interfaceStorageRandomAccessFile
- Throws:
java.io.IOException
- - if an I/O error occurs.
-
getFilePointer
public long getFilePointer()
Description copied from interface:StorageRandomAccessFile
Get the current offset in this file.- Specified by:
getFilePointer
in interfaceStorageRandomAccessFile
- Returns:
- the current file pointer.
-
length
public long length()
Description copied from interface:StorageRandomAccessFile
Gets the length of this file.- Specified by:
length
in interfaceStorageRandomAccessFile
- Returns:
- the number of bytes this file.
-
seek
public void seek(long newFilePointer) throws java.io.IOException
Description copied from interface:StorageRandomAccessFile
Set the file pointer. It may be moved beyond the end of the file, but this does not change the length of the file. The length of the file is not changed until data is actually written..- Specified by:
seek
in interfaceStorageRandomAccessFile
- Parameters:
newFilePointer
- the new file pointer, measured in bytes from the beginning of the file.- Throws:
java.io.IOException
- - if newFilePointer is less than 0 or an I/O error occurs.
-
setLength
public void setLength(long newLength)
Description copied from interface:StorageRandomAccessFile
Sets the length of this file, either extending or truncating it.If the file is extended then the contents of the extension are not defined.
If the file is truncated and the file pointer is greater than the new length then the file pointer is set to the new length.
- Specified by:
setLength
in interfaceStorageRandomAccessFile
- Parameters:
newLength
- The new file length.
-
sync
public void sync()
Description copied from interface:StorageRandomAccessFile
Force any changes out to the persistent store. If the database is to be transient, that is, if the database does not survive a restart, then the sync method implementation need not do anything.- Specified by:
sync
in interfaceStorageRandomAccessFile
-
read
public int read(byte[] b, int off, int len) throws java.io.IOException
Description copied from interface:StorageRandomAccessFile
Reads up tolen
bytes of data from this file into an array of bytes. This method blocks until at least one byte of input is available.- Specified by:
read
in interfaceStorageRandomAccessFile
- Parameters:
b
- the buffer into which the data is read.off
- the start offset in arrayb
at which the data is written.len
- the maximum number of bytes read.- Returns:
- the total number of bytes read into the buffer, or
-1
if there is no more data because the end of the file has been reached. - Throws:
java.io.IOException
- If the first byte cannot be read for any reason other than end of file, or if the random access file has been closed, or if some other I/O error occurs.
-
readFully
public void readFully(byte[] b) throws java.io.IOException
- Specified by:
readFully
in interfacejava.io.DataInput
- Throws:
java.io.IOException
-
readFully
public void readFully(byte[] b, int off, int len) throws java.io.IOException
- Specified by:
readFully
in interfacejava.io.DataInput
- Throws:
java.io.IOException
-
skipBytes
public int skipBytes(int n)
- Specified by:
skipBytes
in interfacejava.io.DataInput
-
readBoolean
public boolean readBoolean() throws java.io.IOException
- Specified by:
readBoolean
in interfacejava.io.DataInput
- Throws:
java.io.IOException
-
readByte
public byte readByte() throws java.io.IOException
- Specified by:
readByte
in interfacejava.io.DataInput
- Throws:
java.io.IOException
-
readUnsignedByte
public int readUnsignedByte() throws java.io.IOException
- Specified by:
readUnsignedByte
in interfacejava.io.DataInput
- Throws:
java.io.IOException
-
readShort
public short readShort() throws java.io.IOException
- Specified by:
readShort
in interfacejava.io.DataInput
- Throws:
java.io.IOException
-
readUnsignedShort
public int readUnsignedShort() throws java.io.IOException
- Specified by:
readUnsignedShort
in interfacejava.io.DataInput
- Throws:
java.io.IOException
-
readChar
public char readChar() throws java.io.IOException
- Specified by:
readChar
in interfacejava.io.DataInput
- Throws:
java.io.IOException
-
readInt
public int readInt() throws java.io.IOException
- Specified by:
readInt
in interfacejava.io.DataInput
- Throws:
java.io.IOException
-
readLong
public long readLong() throws java.io.IOException
- Specified by:
readLong
in interfacejava.io.DataInput
- Throws:
java.io.IOException
-
readFloat
public float readFloat() throws java.io.IOException
- Specified by:
readFloat
in interfacejava.io.DataInput
- Throws:
java.io.IOException
-
readDouble
public double readDouble() throws java.io.IOException
- Specified by:
readDouble
in interfacejava.io.DataInput
- Throws:
java.io.IOException
-
readLine
public java.lang.String readLine() throws java.io.IOException
- Specified by:
readLine
in interfacejava.io.DataInput
- Throws:
java.io.IOException
-
readUTF
public java.lang.String readUTF() throws java.io.IOException
- Specified by:
readUTF
in interfacejava.io.DataInput
- Throws:
java.io.IOException
-
write
public void write(int b) throws java.io.IOException
- Specified by:
write
in interfacejava.io.DataOutput
- Throws:
java.io.IOException
-
write
public void write(byte[] b) throws java.io.IOException
- Specified by:
write
in interfacejava.io.DataOutput
- Throws:
java.io.IOException
-
write
public void write(byte[] b, int off, int len) throws java.io.IOException
- Specified by:
write
in interfacejava.io.DataOutput
- Throws:
java.io.IOException
-
writeBoolean
public void writeBoolean(boolean v) throws java.io.IOException
- Specified by:
writeBoolean
in interfacejava.io.DataOutput
- Throws:
java.io.IOException
-
writeByte
public void writeByte(int v) throws java.io.IOException
- Specified by:
writeByte
in interfacejava.io.DataOutput
- Throws:
java.io.IOException
-
writeShort
public void writeShort(int v) throws java.io.IOException
- Specified by:
writeShort
in interfacejava.io.DataOutput
- Throws:
java.io.IOException
-
writeChar
public void writeChar(int v) throws java.io.IOException
- Specified by:
writeChar
in interfacejava.io.DataOutput
- Throws:
java.io.IOException
-
writeInt
public void writeInt(int v) throws java.io.IOException
- Specified by:
writeInt
in interfacejava.io.DataOutput
- Throws:
java.io.IOException
-
writeLong
public void writeLong(long v) throws java.io.IOException
- Specified by:
writeLong
in interfacejava.io.DataOutput
- Throws:
java.io.IOException
-
writeFloat
public void writeFloat(float v) throws java.io.IOException
- Specified by:
writeFloat
in interfacejava.io.DataOutput
- Throws:
java.io.IOException
-
writeDouble
public void writeDouble(double v) throws java.io.IOException
- Specified by:
writeDouble
in interfacejava.io.DataOutput
- Throws:
java.io.IOException
-
writeBytes
public void writeBytes(java.lang.String s) throws java.io.IOException
- Specified by:
writeBytes
in interfacejava.io.DataOutput
- Throws:
java.io.IOException
-
writeChars
public void writeChars(java.lang.String s) throws java.io.IOException
- Specified by:
writeChars
in interfacejava.io.DataOutput
- Throws:
java.io.IOException
-
writeUTF
public void writeUTF(java.lang.String s) throws java.io.IOException
- Specified by:
writeUTF
in interfacejava.io.DataOutput
- Throws:
java.io.IOException
-
-