Class FileStream

java.lang.Object
com.pdftools.sys.FileStream
All Implemented Interfaces:
Stream, AutoCloseable

public class FileStream extends Object implements Stream
The stream implementation to read from and write to files. This implementation is backed by java.io.RandomAccessFile.
  • Constructor Details

    • FileStream

      public FileStream(File file, FileStream.Mode mode) throws IOException
      Create a new file stream from a file. This constructor works similarly to the constructor of the underlying java.io.RandomAccessFile.
      Parameters:
      file - The file to be opened
      mode - The open mode of the file
      Throws:
      IOException
      See Also:
    • FileStream

      public FileStream(String filename, FileStream.Mode mode) throws IOException
      Create a new file stream from a file name. This constructor works similarly to the constructor of the underlying java.io.RandomAccessFile.
      Parameters:
      filename - The name of the file to be opened
      mode - The open mode of the file
      Throws:
      IOException
      See Also:
    • FileStream

      public FileStream(RandomAccessFile raFile)
      Create a new file stream from a RandomAccessFile. ATTENTION: Opening a RandomAccessFile in "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 underlying RandomAccessFile
      See Also:
  • Method Details

    • GetRandomAccessFile

      public RandomAccessFile GetRandomAccessFile()
      Get the underlying RandomAccessFile.
      Returns:
      The underlying RandomAccessFile
    • getLength

      public long getLength() throws IOException
      Get the length of the stream in bytes.
      Specified by:
      getLength in interface Stream
      Returns:
      the length of the stream in bytes
      Throws:
      IOException
    • setLength

      public void setLength(long length) throws IOException
      Set the length of the stream in bytes.
      Parameters:
      length - the length of the stream in bytes
      Throws:
      IOException
    • seek

      public boolean seek(long position) throws IOException
      Set byte position.
      Specified by:
      seek in interface Stream
      Parameters:
      position - The new position of the stream (-1 for EOS)
      Returns:
      true if successful
      Throws:
      IOException
    • tell

      public long tell() throws IOException
      Get current byte position.
      Specified by:
      tell in interface Stream
      Returns:
      byte position, -1 if position unknown
      Throws:
      IOException
    • read

      public int read(byte[] buffer, int offset, int length) throws IOException
      Read from the stream.
      Specified by:
      read in interface Stream
      Parameters:
      buffer - The buffer where the data is written
      offset - The starting element in the buffer
      length - The maximum number of bytes to be read
      Returns:
      The actual number of bytes read (-1 if EOS)
      Throws:
      IOException
    • write

      public void write(byte[] buffer, int offset, int length) throws IOException
      Write to the stream.
      Specified by:
      write in interface Stream
      Parameters:
      buffer - The buffer where the data lies
      offset - The starting element in the buffer
      length - The maximum number of bytes to be written
      Throws:
      IOException
    • copy

      public void copy(Stream stream) throws IOException
      Copy the content from another stream.
      Parameters:
      stream - The stream from which the content is copied
      Throws:
      IOException
    • close

      public void close() throws IOException
      Close the file stream. This closes also the underlying RandomAccessFile.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Stream
      Throws:
      IOException