Class FileStream

  • All Implemented Interfaces:
    Stream, java.lang.AutoCloseable

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

      Nested Classes 
      Modifier and Type Class Description
      static class  FileStream.Mode
      The file access mode.
    • Constructor Summary

      Constructors 
      Constructor Description
      FileStream​(java.io.File file, FileStream.Mode mode)
      Create a new file stream from a file.
      FileStream​(java.io.RandomAccessFile raFile)
      Create a new file stream from a RandomAccessFile.
      FileStream​(java.lang.String filename, FileStream.Mode mode)
      Create a new file stream from a file name.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Close the file stream.
      void copy​(Stream stream)
      Copy the content from another stream.
      long getLength()
      Get the length of the stream in bytes.
      java.io.RandomAccessFile GetRandomAccessFile()
      Get the underlying RandomAccessFile.
      int read​(byte[] buffer, int offset, int length)
      Read from the stream.
      boolean seek​(long position)
      Set byte position.
      void setLength​(long length)
      Set the length of the stream in bytes.
      long tell()
      Get current byte position.
      void write​(byte[] buffer, int offset, int length)
      Write to the stream.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • FileStream

        public FileStream​(java.io.File file,
                          FileStream.Mode mode)
                   throws java.io.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:
        java.io.IOException
        See Also:
        RandomAccessFile(java.io.File, String)
      • FileStream

        public FileStream​(java.lang.String filename,
                          FileStream.Mode mode)
                   throws java.io.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:
        java.io.IOException
        See Also:
        RandomAccessFile(java.io.File, String)
      • FileStream

        public FileStream​(java.io.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:
        RandomAccessFile
    • Method Detail

      • GetRandomAccessFile

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

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

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

        public boolean seek​(long position)
                     throws java.io.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:
        java.io.IOException
      • tell

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

        public int read​(byte[] buffer,
                        int offset,
                        int length)
                 throws java.io.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:
        java.io.IOException
      • write

        public void write​(byte[] buffer,
                          int offset,
                          int length)
                   throws java.io.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:
        java.io.IOException
      • copy

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

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