Class Files.FileByteSource

  • Enclosing class:
    Files

    private static final class Files.FileByteSource
    extends ByteSource
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.io.File file  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private FileByteSource​(java.io.File file)  
    • Field Detail

      • file

        private final java.io.File file
    • Constructor Detail

      • FileByteSource

        private FileByteSource​(java.io.File file)
    • Method Detail

      • openStream

        public java.io.FileInputStream openStream()
                                           throws java.io.IOException
        Description copied from class: ByteSource
        Opens a new InputStream for reading from this source. This method should return a new, independent stream each time it is called.

        The caller is responsible for ensuring that the returned stream is closed.

        Specified by:
        openStream in class ByteSource
        Throws:
        java.io.IOException - if an I/O error occurs in the process of opening the stream
      • sizeIfKnown

        public Optional<java.lang.Long> sizeIfKnown()
        Description copied from class: ByteSource
        Returns the size of this source in bytes, if the size can be easily determined without actually opening the data stream.

        The default implementation returns Optional.absent(). Some sources, such as a file, may return a non-absent value. Note that in such cases, it is possible that this method will return a different number of bytes than would be returned by reading all of the bytes (for example, some special files may return a size of 0 despite actually having content when read).

        Additionally, for mutable sources such as files, a subsequent read may return a different number of bytes if the contents are changed.

        Overrides:
        sizeIfKnown in class ByteSource
      • size

        public long size()
                  throws java.io.IOException
        Description copied from class: ByteSource
        Returns the size of this source in bytes, even if doing so requires opening and traversing an entire stream. To avoid a potentially expensive operation, see ByteSource.sizeIfKnown().

        The default implementation calls ByteSource.sizeIfKnown() and returns the value if present. If absent, it will fall back to a heavyweight operation that will open a stream, read (or skip, if possible) to the end of the stream and return the total number of bytes that were read.

        Note that for some sources that implement ByteSource.sizeIfKnown() to provide a more efficient implementation, it is possible that this method will return a different number of bytes than would be returned by reading all of the bytes (for example, some special files may return a size of 0 despite actually having content when read).

        In either case, for mutable sources such as files, a subsequent read may return a different number of bytes if the contents are changed.

        Overrides:
        size in class ByteSource
        Throws:
        java.io.IOException - if an I/O error occurs in the process of reading the size of this source
      • read

        public byte[] read()
                    throws java.io.IOException
        Description copied from class: ByteSource
        Reads the full contents of this byte source as a byte array.
        Overrides:
        read in class ByteSource
        Throws:
        java.io.IOException - if an I/O error occurs in the process of reading from this source
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object