Package oracle.spatial.util
Class BufferedRandomAccessFile
- java.lang.Object
-
- java.io.InputStream
-
- java.io.FilterInputStream
-
- java.io.BufferedInputStream
-
- oracle.spatial.util.BufferedRandomAccessFile
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
public class BufferedRandomAccessFile extends java.io.BufferedInputStream
BufferedInputStream
impl with similar facilities as aRandomAccessFile
. The advantage is that this class allows buffering larger byte streams from the file upfront, reducing thus the total IO operations to read an entire file.BufferedInputStream
can be used only for read-operations.
-
-
Field Summary
Fields Modifier and Type Field Description static int
DEFAULT_BUFFER_SIZE_BYTES
-
Constructor Summary
Constructors Constructor Description BufferedRandomAccessFile(java.io.File file)
Same asBufferedRandomAccessFile(String, int)
except that it will use theDEFAULT_BUFFER_SIZE_BYTES
forbufferSize
parameter.BufferedRandomAccessFile(java.io.File file, int bufferSize)
Creates aBufferedRandomAccessFile
with the specified buffer size, and creates an input stream from thefile
argument, which is saved for later use.BufferedRandomAccessFile(java.lang.String file)
Same asBufferedRandomAccessFile(String, int)
except that it will create aFile
object from thefile
string parameter and it will use theDEFAULT_BUFFER_SIZE_BYTES
forbufferSize
parameter.BufferedRandomAccessFile(java.lang.String file, int bufferSize)
Same asBufferedRandomAccessFile(File, int)
except it will create aFile
object from thefile
string parameter.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
bufferSize()
Retrieves the buffer size used by this instance.void
close()
Closes this random access file stream and releases any system resources associated with the stream.java.io.FileDescriptor
getFD()
Returns the opaque file descriptor object associated with this stream.long
getFilePointer()
Returns the current offset in this file.long
length()
Returns the length of this file.int
read()
int
read(byte[] b)
int
read(byte[] b, int off, int len)
int
readInt()
Reads a signed 32-bit integer from this file.void
resetBufferPositions()
Discards the buffer data, by setting the internalpos
andcount
from inherited fromBufferedInputStream
back to0
.void
seek(long newPosition)
Sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs, and resets the internal buffer.long
skip(long n)
-
-
-
Field Detail
-
DEFAULT_BUFFER_SIZE_BYTES
public static final int DEFAULT_BUFFER_SIZE_BYTES
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
BufferedRandomAccessFile
public BufferedRandomAccessFile(java.lang.String file) throws java.io.IOException
Same asBufferedRandomAccessFile(String, int)
except that it will create aFile
object from thefile
string parameter and it will use theDEFAULT_BUFFER_SIZE_BYTES
forbufferSize
parameter.- Throws:
java.io.IOException
-
BufferedRandomAccessFile
public BufferedRandomAccessFile(java.lang.String file, int bufferSize) throws java.io.IOException
Same asBufferedRandomAccessFile(File, int)
except it will create aFile
object from thefile
string parameter.- Throws:
java.io.IOException
-
BufferedRandomAccessFile
public BufferedRandomAccessFile(java.io.File file) throws java.io.IOException
Same asBufferedRandomAccessFile(String, int)
except that it will use theDEFAULT_BUFFER_SIZE_BYTES
forbufferSize
parameter.- Throws:
java.io.IOException
-
BufferedRandomAccessFile
public BufferedRandomAccessFile(java.io.File file, int bufferSize) throws java.io.IOException
Creates aBufferedRandomAccessFile
with the specified buffer size, and creates an input stream from thefile
argument, which is saved for later use. An internal buffer array of lengthbufferSize
is created and stored inbuf
.- Parameters:
file
- the file object from which the underlying input stream will be created.bufferSize
- the buffer size.- Throws:
java.lang.IllegalArgumentException
- iffile
is null or ifbufferSize
is equal to or less than zero.java.io.FileNotFoundException
- if the given file object does not denote an existing regular file, or if some other error occurs while opening the file.java.lang.SecurityException
- if a security manager exists and itscheckRead
method denies read access to the file.java.io.IOException
-
-
Method Detail
-
close
public void close() throws java.io.IOException
Closes this random access file stream and releases any system resources associated with the stream. A closed random access file cannot perform input or output operations and cannot be reopened.If this file has an associated channel then the channel is closed as well.
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Overrides:
close
in classjava.io.BufferedInputStream
- Throws:
java.io.IOException
- if an I/O error occurs.
-
getFilePointer
public long getFilePointer() throws java.io.IOException
Returns the current offset in this file.- Returns:
- the offset from the beginning of the file, in bytes, at which the next read or write occurs.
- Throws:
java.io.IOException
- if an I/O error occurs.
-
getFD
public java.io.FileDescriptor getFD() throws java.io.IOException
Returns the opaque file descriptor object associated with this stream.- Returns:
- the file descriptor object associated with this stream.
- Throws:
java.io.IOException
- if an I/O error occurs.- See Also:
FileDescriptor
-
length
public long length() throws java.io.IOException
Returns the length of this file.- Returns:
- the length of this file, measured in bytes.
- Throws:
java.io.IOException
- if an I/O error occurs.
-
seek
public void seek(long newPosition) throws java.io.IOException
Sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs, and resets the internal buffer. The offset may be set beyond the end of the file. Setting the offset beyond the end of the file does not change the file length. The file length will change only by writing after the offset has been set beyond the end of the file.- Parameters:
newPosition
- the offset position, measured in bytes from the beginning of the file, at which to set the file pointer.- Throws:
java.io.IOException
- ifnewPosition
is less than0
or if an I/O error occurs.
-
resetBufferPositions
public void resetBufferPositions()
Discards the buffer data, by setting the internalpos
andcount
from inherited fromBufferedInputStream
back to0
.
-
bufferSize
public int bufferSize()
Retrieves the buffer size used by this instance.- Returns:
- the number of bytes reserved for buffering.
-
read
public int read() throws java.io.IOException
- Overrides:
read
in classjava.io.BufferedInputStream
- Throws:
java.io.IOException
-
read
public int read(byte[] b) throws java.io.IOException
- Overrides:
read
in classjava.io.FilterInputStream
- Throws:
java.io.IOException
-
read
public int read(byte[] b, int off, int len) throws java.io.IOException
- Overrides:
read
in classjava.io.BufferedInputStream
- Throws:
java.io.IOException
-
skip
public long skip(long n) throws java.io.IOException
- Overrides:
skip
in classjava.io.BufferedInputStream
- Throws:
java.io.IOException
-
readInt
public int readInt() throws java.io.IOException
Reads a signed 32-bit integer from this file. This method reads 4 bytes from the file, starting at the current file pointer. If the bytes read, in order, areb1
,b2
,b3
, andb4
, where0 <= b1, b2, b3, b4 <= 255
, then the result is equal to:(b1 << 24) | (b2 << 16) + (b3 << 8) + b4
This method blocks until the four bytes are read, the end of the stream is detected, or an exception is thrown.
- Returns:
- the next four bytes of this file, interpreted as an
int
. - Throws:
java.io.EOFException
- if this file reaches the end before reading four bytes.java.io.IOException
- if an I/O error occurs.
-
-