Package com.pdftools.sys
Class FileStream
java.lang.Object
com.pdftools.sys.FileStream
- All Implemented Interfaces:
Stream,AutoCloseable
The stream implementation to read from and write to files.
This implementation is backed by
java.io.RandomAccessFile.-
Nested Class Summary
Nested Classes -
Constructor Summary
ConstructorsConstructorDescriptionFileStream(File file, FileStream.Mode mode) Create a new file stream from a file.FileStream(RandomAccessFile raFile) Create a new file stream from aRandomAccessFile.FileStream(String filename, FileStream.Mode mode) Create a new file stream from a file name. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Close the file stream.voidCopy the content from another stream.longGet the length of the stream in bytes.Get the underlyingRandomAccessFile.intread(byte[] buffer, int offset, int length) Read from the stream.booleanseek(long position) Set byte position.voidsetLength(long length) Set the length of the stream in bytes.longtell()Get current byte position.voidwrite(byte[] buffer, int offset, int length) Write to the stream.
-
Constructor Details
-
FileStream
Create a new file stream from a file. This constructor works similarly to the constructor of the underlyingjava.io.RandomAccessFile.- Parameters:
file- The file to be openedmode- The open mode of the file- Throws:
IOException- See Also:
-
FileStream
Create a new file stream from a file name. This constructor works similarly to the constructor of the underlyingjava.io.RandomAccessFile.- Parameters:
filename- The name of the file to be openedmode- The open mode of the file- Throws:
IOException- See Also:
-
FileStream
Create a new file stream from aRandomAccessFile. ATTENTION: Opening aRandomAccessFilein "rw" mode does not delete the current content of the file, so be sure that you delete it yourself if needed (which is mostly the case).- Parameters:
raFile- The underlyingRandomAccessFile- See Also:
-
-
Method Details
-
GetRandomAccessFile
Get the underlyingRandomAccessFile.- Returns:
- The underlying
RandomAccessFile
-
getLength
Get the length of the stream in bytes.- Specified by:
getLengthin interfaceStream- Returns:
- the length of the stream in bytes
- Throws:
IOException
-
setLength
Set the length of the stream in bytes.- Parameters:
length- the length of the stream in bytes- Throws:
IOException
-
seek
Set byte position.- Specified by:
seekin interfaceStream- Parameters:
position- The new position of the stream (-1 for EOS)- Returns:
- true if successful
- Throws:
IOException
-
tell
Get current byte position.- Specified by:
tellin interfaceStream- Returns:
- byte position, -1 if position unknown
- Throws:
IOException
-
read
Read from the stream.- Specified by:
readin interfaceStream- Parameters:
buffer- The buffer where the data is writtenoffset- The starting element in the bufferlength- The maximum number of bytes to be read- Returns:
- The actual number of bytes read (-1 if EOS)
- Throws:
IOException
-
write
Write to the stream.- Specified by:
writein interfaceStream- Parameters:
buffer- The buffer where the data liesoffset- The starting element in the bufferlength- The maximum number of bytes to be written- Throws:
IOException
-
copy
Copy the content from another stream.- Parameters:
stream- The stream from which the content is copied- Throws:
IOException
-
close
Close the file stream. This closes also the underlyingRandomAccessFile.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceStream- Throws:
IOException
-