Class FileStream


  • public class FileStream
    extends RandomAccessStream
    File stream implementation for local files.
    • Constructor Detail

      • FileStream

        public FileStream​(File file,
                          java.lang.String mode)
                   throws java.io.FileNotFoundException
        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 stream
        mode - The mode "r" for read "rw" for write
        Throws:
        java.io.FileNotFoundException - Thrown if file not found
    • Method Detail

      • canRead

        public boolean canRead()
        Check if stream can read from file.
        Specified by:
        canRead in class RandomAccessStream
        Returns:
        True if readable
      • canWrite

        public boolean canWrite()
        Check if stream can write to file.
        Specified by:
        canWrite in class RandomAccessStream
        Returns:
        True if writable
      • canSeek

        public boolean canSeek()
        Check if stream can seek.
        Specified by:
        canSeek in class RandomAccessStream
        Returns:
        True if seekable
      • getLength

        public long getLength()
        Get the length of the stream. This is the same as the backed file.
        Specified by:
        getLength in class RandomAccessStream
        Returns:
        The file stream length
      • getPosition

        public long getPosition()
                         throws java.io.IOException
        Get the current position of the stream.
        Specified by:
        getPosition in class RandomAccessStream
        Returns:
        The current position
        Throws:
        java.io.IOException - Thrown if there is an IO error.
      • setPosition

        public void setPosition​(long value)
                         throws java.io.IOException
        Set the current position of the stream.
        Specified by:
        setPosition in class RandomAccessStream
        Parameters:
        value - The new position.
        Throws:
        java.io.IOException - Thrown if there is an IO error.
      • setLength

        public void setLength​(long value)
                       throws java.io.IOException
        Set the length of the stream. This is applicable for write streams only.
        Specified by:
        setLength in class RandomAccessStream
        Parameters:
        value - The new length.
        Throws:
        java.io.IOException - Thrown if there is an IO error.
      • read

        public int read​(byte[] buffer,
                        int offset,
                        int count)
                 throws java.io.IOException
        Read data from the file stream into the buffer provided.
        Specified by:
        read in class RandomAccessStream
        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:
        java.io.IOException - Thrown if there is an IO error.
      • write

        public void write​(byte[] buffer,
                          int offset,
                          int count)
                   throws java.io.IOException
        Write the data from the buffer provided into the stream.
        Specified by:
        write in class RandomAccessStream
        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:
        java.io.IOException - Thrown if there is an IO error.
      • flush

        public void flush()
        Flush the buffers to the associated file.
        Specified by:
        flush in class RandomAccessStream
      • close

        public void close()
                   throws java.io.IOException
        Close this stream and associated resources.
        Specified by:
        close in class RandomAccessStream
        Throws:
        java.io.IOException - Thrown if there is an IO error.