public class FileRandomStream extends RandomStream
READ_ONLY, READ_WRITE| Constructor and Description |
|---|
FileRandomStream(File file,
String mode) |
FileRandomStream(File file,
String mode) |
FileRandomStream(String filePath,
String mode) |
| Modifier and Type | Method and Description |
|---|---|
boolean |
canSetLength()
Return if this Stream has the capability to set the stream length in Read/Write mode.
|
void |
close()
This method closes the stream.
|
void |
flush()
This method forces any data that may have been buffered to be written
to the underlying output device.
|
long |
getLength()
Return the full length of the Stream.
|
long |
getPosition()
Return the current read/write stream pointer.
|
int |
read()
This method reads an unsigned byte from the input stream and returns it
as an int in the range of 0-255.
|
int |
read(byte[] dest,
int offset,
int count)
This method read bytes from a stream and stores them into a
caller supplied buffer.
|
void |
setLength(long length)
Attempt to set the length of the stream if this is supported.
|
void |
setPosition(long newPosition)
Set the Stream position.
|
static RandomStream |
toRandomStream(RandomAccessFile raf,
String mode)
Convert a RandomAccessFile to a RandomStream using the most efficient method.
|
void |
write(byte[] src,
int offset,
int count)
This method writes
len bytes from the specified array
b starting at index off into the array. |
void |
write(int ch)
This method writes a single byte to the output stream.
|
isOpenForWriting, isReadWrite, mark, markSupported, reset, setMode, skip, throwCantWrite, toOutputStream, validateMode, writeavailable, finalize, hashCode, readpublic FileRandomStream(String filePath, String mode) throws IllegalArgumentException, FileNotFoundException
filePath - mode - should be either "r" or "rw"IllegalArgumentExceptionFileNotFoundExceptionpublic FileRandomStream(File file, String mode) throws IllegalArgumentException, FileNotFoundException
file - mode - should be either "r" or "rw"IllegalArgumentExceptionFileNotFoundExceptionpublic FileRandomStream(File file, String mode) throws IllegalArgumentException, FileNotFoundException
file - mode - should be either "r" or "rw"IllegalArgumentExceptionFileNotFoundExceptionpublic int read()
throws IOException
InputStreamThis method will block until the byte can be read.
read in class InputStreamIOException - If an error occurspublic int read(byte[] dest,
int offset,
int count)
throws IOException
InputStreamoff into the buffer and attempts to read
len bytes. This method can return before reading the
number of bytes requested. The actual number of bytes read is
returned as an int. A -1 is returned to indicate the end of the
stream.
This method will block until some data can be read.
This method operates by calling the single byte read() method
in a loop until the desired number of bytes are read. The read loop
stops short if the end of the stream is encountered or if an IOException
is encountered on any read operation except the first. If the first
attempt to read a bytes fails, the IOException is allowed to propagate
upward. And subsequent IOException is caught and treated identically
to an end of stream condition. Subclasses can (and should if possible)
override this method to provide a more efficient implementation.
read in class InputStreamdest - The array into which the bytes read should be storedoffset - The offset into the array to start storing bytescount - The requested number of bytes to readIOException - If an error occurs.public void write(int ch)
throws IOException
RandomStreamint passed as the argument.
Subclasses must provide an implementation of this method.
write in class RandomStreamch - The byte to be written to the output stream, passed as
the low eight bits of an intIOException - If an error occurspublic void write(byte[] src,
int offset,
int count)
throws IOException
RandomStreamlen bytes from the specified array
b starting at index off into the array.
This method in this class calls the single byte write()
method in a loop until all bytes have been written. Subclasses should
override this method if possible in order to provide a more efficent
implementation.
write in class RandomStreamsrc - The array of bytes to write fromoffset - The index into the array to start writing fromcount - The number of bytes to writeIOException - If an error occurspublic long getLength()
throws IOException
getLength in class RandomStreamIOException - if an IO error occured.public void setLength(long length)
throws IOException
setLength in class RandomStreamIOException - if the length cannot be set or if there was an error.public long getPosition()
throws IOException
getPosition in class RandomStreamIOException - on error.public void setPosition(long newPosition)
throws IOException
setPosition in class RandomStreamnewPosition - the new position to set.IOException - on error.public boolean canSetLength()
canSetLength in class RandomStreampublic void flush()
throws IOException
RandomStreamThis method in this class does nothing.
flush in class RandomStreamIOException - If an error occurspublic static RandomStream toRandomStream(RandomAccessFile raf, String mode) throws IllegalArgumentException, IOException
raf - the already open RandomAccessFile.mode - the mode the RandomAccessFile is open for (either "r" or "rw").IllegalArgumentException - if the mode is not "r" or "rw"IOException - if the conversion could not be made for any reason.public void close()
throws IOException
InputStreamIOException
This method does nothing in this class, but subclasses may override this method in order to provide additional functionality.
close in class InputStreamIOException - If an error occurs, which can only happen
in a subclass