java.lang.Object
i2p.susi.util.FileBuffer
All Implemented Interfaces:
Buffer
Direct Known Subclasses:
GzipFileBuffer

public class FileBufferextends Object implements Buffer
Buffer implementation backed by a file.
  • Field Details

    • _file

      protected final File _file
      The file.
    • _offset

      protected final int _offset
      The offset.
    • _sublen

      protected final int _sublen
      The sub-length.
    • _is

      private InputStream _is
      Stream the bytes are read from.
    • _os

      private OutputStream _os
      Stream the bytes are written to.
  • Constructor Details

    • FileBuffer

      public FileBuffer(File file)
      Create a FileBuffer for the entire file.
      Parameters:
      file - the file to buffer
    • FileBuffer

      public FileBuffer(File file, int offset, int sublen)
      Create a FileBuffer for a portion of the file.
      Parameters:
      file - the file to buffer
      offset - the byte offset to start reading from
      sublen - the number of bytes to read, or 0 for the entire file
  • Method Details

    • getFile

      public File getFile()
      Get the file being buffered.
      Returns:
      the underlying file
    • getInputStream

      public InputStream getInputStream() throws IOException
      Get an InputStream for the file, skipping to the offset if needed. Caller must call readComplete(boolean).
      Specified by:
      getInputStream in interface Buffer
      Returns:
      a stream positioned at the offset, reusing the open one
      Throws:
      IOException - on I/O error
    • getOutputStream

      public OutputStream getOutputStream() throws IOException
      Get an OutputStream for the file. Caller must call writeComplete(boolean).
      Specified by:
      getOutputStream in interface Buffer
      Returns:
      a file output stream, reusing the open one
      Throws:
      IOException - on I/O error
    • readComplete

      public void readComplete(boolean success)
      Close the input stream.
      Specified by:
      readComplete in interface Buffer
      Parameters:
      success - whether the read succeeded; the stream is closed either way
    • writeComplete

      public void writeComplete(boolean success)
      Close the output stream and delete the file if success is false.
      Specified by:
      writeComplete in interface Buffer
      Parameters:
      success - if false, deletes the file
    • getLength

      public int getLength()
      Get the length of data in this buffer. Returns the sublen if set, otherwise the file length.
      Specified by:
      getLength in interface Buffer
      Returns:
      length in bytes
    • getOffset

      public int getOffset()
      Get the byte offset into the file.
      Specified by:
      getOffset in interface Buffer
      Returns:
      offset in bytes
    • toString

      public String toString()
      Overrides:
      toString in class Object