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_readOnlywhether the file is read-onlyprivate BlockedByteArrayInputStreambInStream used to read from the source entry.private BlockedByteArrayOutputStreambOutStream used to write into the source entry.private java.io.DataInputStreamdIsData input stream on top of the source input stream.private java.io.DataOutputStreamdOsData output stream on top of the source output stream.private DataStoreEntryentryThe source entry.private longfpCurrent 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 VirtualRandomAccessFileclone()Clone this file abstractionvoidclose()Closes this file.longgetFilePointer()Get the current offset in this file.longlength()Gets the length of this file.intread(byte[] b, int off, int len)Reads up tolenbytes of data from this file into an array of bytes.booleanreadBoolean()bytereadByte()charreadChar()doublereadDouble()floatreadFloat()voidreadFully(byte[] b)voidreadFully(byte[] b, int off, int len)intreadInt()java.lang.StringreadLine()longreadLong()shortreadShort()intreadUnsignedByte()intreadUnsignedShort()java.lang.StringreadUTF()voidseek(long newFilePointer)Set the file pointer.voidsetLength(long newLength)Sets the length of this file, either extending or truncating it.intskipBytes(int n)voidsync()Force any changes out to the persistent store.voidwrite(byte[] b)voidwrite(byte[] b, int off, int len)voidwrite(int b)voidwriteBoolean(boolean v)voidwriteByte(int v)voidwriteBytes(java.lang.String s)voidwriteChar(int v)voidwriteChars(java.lang.String s)voidwriteDouble(double v)voidwriteFloat(float v)voidwriteInt(int v)voidwriteLong(long v)voidwriteShort(int v)voidwriteUTF(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 benullif 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 benullif 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:StorageRandomAccessFileClone this file abstraction- Specified by:
clonein interfaceStorageRandomAccessFile- Overrides:
clonein classjava.lang.Object
-
close
public void close() throws java.io.IOExceptionDescription copied from interface:StorageRandomAccessFileCloses this file.- Specified by:
closein interfaceStorageRandomAccessFile- Throws:
java.io.IOException- - if an I/O error occurs.
-
getFilePointer
public long getFilePointer()
Description copied from interface:StorageRandomAccessFileGet the current offset in this file.- Specified by:
getFilePointerin interfaceStorageRandomAccessFile- Returns:
- the current file pointer.
-
length
public long length()
Description copied from interface:StorageRandomAccessFileGets the length of this file.- Specified by:
lengthin interfaceStorageRandomAccessFile- Returns:
- the number of bytes this file.
-
seek
public void seek(long newFilePointer) throws java.io.IOExceptionDescription copied from interface:StorageRandomAccessFileSet 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:
seekin 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:StorageRandomAccessFileSets 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:
setLengthin interfaceStorageRandomAccessFile- Parameters:
newLength- The new file length.
-
sync
public void sync()
Description copied from interface:StorageRandomAccessFileForce 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:
syncin interfaceStorageRandomAccessFile
-
read
public int read(byte[] b, int off, int len) throws java.io.IOExceptionDescription copied from interface:StorageRandomAccessFileReads up tolenbytes of data from this file into an array of bytes. This method blocks until at least one byte of input is available.- Specified by:
readin interfaceStorageRandomAccessFile- Parameters:
b- the buffer into which the data is read.off- the start offset in arraybat which the data is written.len- the maximum number of bytes read.- Returns:
- the total number of bytes read into the buffer, or
-1if 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:
readFullyin interfacejava.io.DataInput- Throws:
java.io.IOException
-
readFully
public void readFully(byte[] b, int off, int len) throws java.io.IOException- Specified by:
readFullyin interfacejava.io.DataInput- Throws:
java.io.IOException
-
skipBytes
public int skipBytes(int n)
- Specified by:
skipBytesin interfacejava.io.DataInput
-
readBoolean
public boolean readBoolean() throws java.io.IOException- Specified by:
readBooleanin interfacejava.io.DataInput- Throws:
java.io.IOException
-
readByte
public byte readByte() throws java.io.IOException- Specified by:
readBytein interfacejava.io.DataInput- Throws:
java.io.IOException
-
readUnsignedByte
public int readUnsignedByte() throws java.io.IOException- Specified by:
readUnsignedBytein interfacejava.io.DataInput- Throws:
java.io.IOException
-
readShort
public short readShort() throws java.io.IOException- Specified by:
readShortin interfacejava.io.DataInput- Throws:
java.io.IOException
-
readUnsignedShort
public int readUnsignedShort() throws java.io.IOException- Specified by:
readUnsignedShortin interfacejava.io.DataInput- Throws:
java.io.IOException
-
readChar
public char readChar() throws java.io.IOException- Specified by:
readCharin interfacejava.io.DataInput- Throws:
java.io.IOException
-
readInt
public int readInt() throws java.io.IOException- Specified by:
readIntin interfacejava.io.DataInput- Throws:
java.io.IOException
-
readLong
public long readLong() throws java.io.IOException- Specified by:
readLongin interfacejava.io.DataInput- Throws:
java.io.IOException
-
readFloat
public float readFloat() throws java.io.IOException- Specified by:
readFloatin interfacejava.io.DataInput- Throws:
java.io.IOException
-
readDouble
public double readDouble() throws java.io.IOException- Specified by:
readDoublein interfacejava.io.DataInput- Throws:
java.io.IOException
-
readLine
public java.lang.String readLine() throws java.io.IOException- Specified by:
readLinein interfacejava.io.DataInput- Throws:
java.io.IOException
-
readUTF
public java.lang.String readUTF() throws java.io.IOException- Specified by:
readUTFin interfacejava.io.DataInput- Throws:
java.io.IOException
-
write
public void write(int b) throws java.io.IOException- Specified by:
writein interfacejava.io.DataOutput- Throws:
java.io.IOException
-
write
public void write(byte[] b) throws java.io.IOException- Specified by:
writein interfacejava.io.DataOutput- Throws:
java.io.IOException
-
write
public void write(byte[] b, int off, int len) throws java.io.IOException- Specified by:
writein interfacejava.io.DataOutput- Throws:
java.io.IOException
-
writeBoolean
public void writeBoolean(boolean v) throws java.io.IOException- Specified by:
writeBooleanin interfacejava.io.DataOutput- Throws:
java.io.IOException
-
writeByte
public void writeByte(int v) throws java.io.IOException- Specified by:
writeBytein interfacejava.io.DataOutput- Throws:
java.io.IOException
-
writeShort
public void writeShort(int v) throws java.io.IOException- Specified by:
writeShortin interfacejava.io.DataOutput- Throws:
java.io.IOException
-
writeChar
public void writeChar(int v) throws java.io.IOException- Specified by:
writeCharin interfacejava.io.DataOutput- Throws:
java.io.IOException
-
writeInt
public void writeInt(int v) throws java.io.IOException- Specified by:
writeIntin interfacejava.io.DataOutput- Throws:
java.io.IOException
-
writeLong
public void writeLong(long v) throws java.io.IOException- Specified by:
writeLongin interfacejava.io.DataOutput- Throws:
java.io.IOException
-
writeFloat
public void writeFloat(float v) throws java.io.IOException- Specified by:
writeFloatin interfacejava.io.DataOutput- Throws:
java.io.IOException
-
writeDouble
public void writeDouble(double v) throws java.io.IOException- Specified by:
writeDoublein interfacejava.io.DataOutput- Throws:
java.io.IOException
-
writeBytes
public void writeBytes(java.lang.String s) throws java.io.IOException- Specified by:
writeBytesin interfacejava.io.DataOutput- Throws:
java.io.IOException
-
writeChars
public void writeChars(java.lang.String s) throws java.io.IOException- Specified by:
writeCharsin interfacejava.io.DataOutput- Throws:
java.io.IOException
-
writeUTF
public void writeUTF(java.lang.String s) throws java.io.IOException- Specified by:
writeUTFin interfacejava.io.DataOutput- Throws:
java.io.IOException
-
-