Class FileIO


  • public class FileIO
    extends RawIOBase
    Raw I/O implementation for OS files.
    Author:
    Philip Jenvey
    • Constructor Summary

      Constructors 
      Constructor Description
      FileIO​(java.lang.String name, java.lang.String mode)  
      FileIO​(java.nio.channels.FileChannel fileChannel, java.lang.String mode)
      Construct a FileIO instance with the given FileChannel.
      FileIO​(PyString name, java.lang.String mode)
      Construct a FileIO instance for the specified file name, which will be decoded using the nominal Jython file system encoding if it is a str/bytes rather than a unicode.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      PyObject __add__​(PyObject otherObj)  
      PyObject __int__()  
      java.io.InputStream asInputStream()
      Coerce this into an InputStream if possible, or return null.
      java.io.OutputStream asOutputStream()
      Coerce this into an OutputStream if possible, or return null.
      void close()
      Flushes and closes the IO object.
      java.nio.channels.FileChannel getChannel()
      Return the underlying Java nio Channel.
      java.io.FileDescriptor getFD()  
      boolean isatty()
      Returns whether this is an 'interactive' stream.
      boolean readable()
      Return whether this file was opened for reading.
      java.nio.ByteBuffer readall()
      Read until EOF with one readinto() call.
      int readinto​(java.nio.ByteBuffer buf)
      Read up to buf.remaining() bytes into buf.
      long readinto​(java.nio.ByteBuffer[] bufs)
      Read bytes into each of the specified ByteBuffers via scatter i/o.
      long seek​(long pos, int whence)
      Seek to byte offset pos relative to position indicated by whence: 0 Start of stream (the default). pos should be >= 0; 1 Current position - whence may be negative; 2 End of stream - whence usually negative.
      long tell()
      Return the current stream position.
      long truncate​(long size)
      Truncate file to size in bytes.
      boolean writable()
      Return whether this file was opened for writing.
      int write​(java.nio.ByteBuffer buf)
      Write the given ByteBuffer to the IO stream.
      long write​(java.nio.ByteBuffer[] bufs)
      Write bytes from each of the specified ByteBuffers via gather i/o.
      • Methods inherited from class java.lang.Object

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

      • FileIO

        public FileIO​(PyString name,
                      java.lang.String mode)
        Construct a FileIO instance for the specified file name, which will be decoded using the nominal Jython file system encoding if it is a str/bytes rather than a unicode. The mode can be 'r', 'w' or 'a' for reading (default), writing or appending. Add a '+' to the mode to allow simultaneous reading and writing.
        Parameters:
        name - the name of the file
        mode - a raw io file mode String
      • FileIO

        public FileIO​(java.nio.channels.FileChannel fileChannel,
                      java.lang.String mode)
        Construct a FileIO instance with the given FileChannel. The mode can be 'r', 'w' or 'a' for reading (default), writing or appending. Add a '+' to the mode to allow simultaneous reading and writing.
        Parameters:
        fileChannel - a FileChannel object
        mode - a raw io file mode String
    • Method Detail

      • isatty

        public boolean isatty()
        Description copied from class: IOBase
        Returns whether this is an 'interactive' stream. Returns False if we don't know.
        Overrides:
        isatty in class IOBase
        Returns:
        a boolean, true if an 'interactive' stream
      • readinto

        public int readinto​(java.nio.ByteBuffer buf)
        Description copied from class: RawIOBase
        Read up to buf.remaining() bytes into buf. Returns number of bytes read (0 for EOF).
        Overrides:
        readinto in class RawIOBase
        Parameters:
        buf - a ByteBuffer to read bytes into
        Returns:
        the amount of data read as an int
      • readinto

        public long readinto​(java.nio.ByteBuffer[] bufs)
        Read bytes into each of the specified ByteBuffers via scatter i/o. Returns number of bytes read (0 for EOF).
        Overrides:
        readinto in class RawIOBase
        Parameters:
        bufs - an array of ByteBuffers to read bytes into
        Returns:
        the amount of data read as a long
      • readall

        public java.nio.ByteBuffer readall()
        Read until EOF with one readinto() call. Takes advantage of the fact that the underlying file's size is available. However, we have to special case if file size is 0, as seen in the /proc virtual file system - in this case we cannot assume the file is truly empty.
        Overrides:
        readall in class RawIOBase
        Returns:
        a ByteBuffer containing the bytes read
      • write

        public int write​(java.nio.ByteBuffer buf)
        Description copied from class: RawIOBase
        Write the given ByteBuffer to the IO stream. Returns the number of bytes written, which may be less than buf.remaining().
        Overrides:
        write in class RawIOBase
        Parameters:
        buf - a ByteBuffer value
        Returns:
        the number of bytes written as an int
      • write

        public long write​(java.nio.ByteBuffer[] bufs)
        Write bytes from each of the specified ByteBuffers via gather i/o.
        Overrides:
        write in class RawIOBase
        Parameters:
        bufs - an array of ByteBuffers
        Returns:
        the number of bytes written as a long
      • seek

        public long seek​(long pos,
                         int whence)
        Description copied from class: IOBase
        Seek to byte offset pos relative to position indicated by whence: 0 Start of stream (the default). pos should be >= 0; 1 Current position - whence may be negative; 2 End of stream - whence usually negative. Returns the new absolute position.
        Overrides:
        seek in class IOBase
        Parameters:
        pos - a long position value
        whence - an int whence value
        Returns:
        a long position value seeked to
      • tell

        public long tell()
        Description copied from class: IOBase
        Return the current stream position.
        Overrides:
        tell in class IOBase
        Returns:
        a long position value
      • truncate

        public long truncate​(long size)
        Description copied from class: IOBase
        Truncate file to size in bytes. Returns the new size.
        Overrides:
        truncate in class IOBase
        Parameters:
        size - a long size to truncate to
        Returns:
        a long size value the file was truncated to
      • close

        public void close()
        Description copied from class: IOBase
        Flushes and closes the IO object. This must be idempotent. It should also set a flag for the 'closed' property (see below) to test.
        Overrides:
        close in class IOBase
      • asOutputStream

        public java.io.OutputStream asOutputStream()
        Description copied from class: IOBase
        Coerce this into an OutputStream if possible, or return null.
        Overrides:
        asOutputStream in class IOBase
      • asInputStream

        public java.io.InputStream asInputStream()
        Description copied from class: IOBase
        Coerce this into an InputStream if possible, or return null.
        Overrides:
        asInputStream in class IOBase
      • readable

        public boolean readable()
        Description copied from class: IOBase
        Return whether this file was opened for reading.
        Overrides:
        readable in class IOBase
        Returns:
        true if the file was opened for reading
      • writable

        public boolean writable()
        Description copied from class: IOBase
        Return whether this file was opened for writing.
        Overrides:
        writable in class IOBase
        Returns:
        true if the file was opened for writing
      • getChannel

        public java.nio.channels.FileChannel getChannel()
        Description copied from class: RawIOBase
        Return the underlying Java nio Channel.
        Specified by:
        getChannel in class RawIOBase
        Returns:
        the underlying Java nio Channel
      • getFD

        public java.io.FileDescriptor getFD()