Package com.mku.fs.streams
Class FileStream
java.lang.Object
RandomAccessStream
com.mku.fs.streams.FileStream
public class FileStream
extends RandomAccessStream
File stream implementation for local files.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleancanRead()Check if stream can read from file.booleancanSeek()Check if stream can seek.booleancanWrite()Check if stream can write to file.voidclose()Close this stream and associated resources.voidflush()Flush the buffers to the associated file.longGet the length of the stream.longGet the current position of the stream.intread(byte[] buffer, int offset, int count) Read data from the file stream into the buffer provided.longseek(long offset, SeekOrigin origin) Seek to the offset provided.voidsetLength(long value) Set the length of the stream.voidsetPosition(long value) Set the current position of the stream.voidwrite(byte[] buffer, int offset, int count) Write the data from the buffer provided into the stream.
-
Constructor Details
-
FileStream
Construct a file stream from a JavaFile. This will create a wrapper stream that will route read() and write() to the FileChannel- Parameters:
file- The JavaFile that will be used to get the read/write streammode- The mode "r" for read "rw" for write- Throws:
FileNotFoundException- Thrown if file not found
-
-
Method Details
-
canRead
public boolean canRead()Check if stream can read from file.- Returns:
- True if readable
-
canWrite
public boolean canWrite()Check if stream can write to file.- Returns:
- True if writable
-
canSeek
public boolean canSeek()Check if stream can seek.- Returns:
- True if seekable
-
getLength
public long getLength()Get the length of the stream. This is the same as the backed file.- Returns:
- The file stream length
-
getPosition
Get the current position of the stream.- Returns:
- The current position
- Throws:
IOException- Thrown if there is an IO error.
-
setPosition
Set the current position of the stream.- Parameters:
value- The new position.- Throws:
IOException- Thrown if there is an IO error.
-
setLength
Set the length of the stream. This is applicable for write streams only.- Parameters:
value- The new length.- Throws:
IOException- Thrown if there is an IO error.
-
read
Read data from the file stream into the buffer provided.- Parameters:
buffer- The buffer to write the data.offset- The offset of the buffer to start writing the data.count- The maximum number of bytes to read from.- Returns:
- The bytes read
- Throws:
IOException- Thrown if there is an IO error.
-
write
Write the data from the buffer provided into the stream.- Parameters:
buffer- The buffer to read the data from.offset- The offset of the buffer to start reading the data.count- The maximum number of bytes to read from the buffer.- Throws:
IOException- Thrown if there is an IO error.
-
seek
Seek to the offset provided.- Parameters:
offset- The position to seek to.origin- The type of originRandomAccessStream.SeekOrigin- Returns:
- The new position after seeking.
- Throws:
IOException- Thrown if there is an IO error.
-
flush
public void flush()Flush the buffers to the associated file. -
close
Close this stream and associated resources.- Throws:
IOException- Thrown if there is an IO error.
-