Class Slice

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable
    Direct Known Subclasses:
    ArraySlice, FileSlice, PathSlice

    public abstract class Slice
    extends java.lang.Object
    implements java.io.Closeable
    A slice of a File, ByteBuffer or InputStream. A single Slice instance should only be used by a single thread.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected Slice​(long length, boolean isDeflatedZipEntry, long inflatedLengthHint, NestedJarHandler nestedJarHandler)
      Constructor.
      protected Slice​(Slice parentSlice, long offset, long length, boolean isDeflatedZipEntry, long inflatedLengthHint, NestedJarHandler nestedJarHandler)
      Constructor for treating a range of a slice as a sub-slice.
    • Field Detail

      • parentSlice

        protected final Slice parentSlice
        The parent slice.
      • sliceStartPos

        public final long sliceStartPos
        The start position of the slice.
      • sliceLength

        public long sliceLength
        The length of the slice, or -1L if unknown (for InputStream).
      • isDeflatedZipEntry

        public final boolean isDeflatedZipEntry
        If true, the slice is a deflated zip entry, and needs to be inflated to access the content.
      • inflatedLengthHint

        public final long inflatedLengthHint
        If the slice is a deflated zip entry, this is the expected uncompressed length, or -1L if unknown.
      • hashCode

        private int hashCode
        The cached hashCode.
    • Constructor Detail

      • Slice

        protected Slice​(Slice parentSlice,
                        long offset,
                        long length,
                        boolean isDeflatedZipEntry,
                        long inflatedLengthHint,
                        NestedJarHandler nestedJarHandler)
        Constructor for treating a range of a slice as a sub-slice.
        Parameters:
        parentSlice - the parent slice
        offset - the offset of the sub-slice within the parent slice
        length - the length of the sub-slice
        isDeflatedZipEntry - true if this is a deflated zip entry
        inflatedLengthHint - the uncompressed size of a deflated zip entry, or -1 if unknown, or 0 of this is not a deflated zip entry.
        nestedJarHandler - the nested jar handler
      • Slice

        protected Slice​(long length,
                        boolean isDeflatedZipEntry,
                        long inflatedLengthHint,
                        NestedJarHandler nestedJarHandler)
        Constructor.
        Parameters:
        length - the length
        isDeflatedZipEntry - true if this is a deflated zip entry
        inflatedLengthHint - the uncompressed size of a deflated zip entry, or -1 if unknown, or 0 of this is not a deflated zip entry.
        nestedJarHandler - the nested jar handler
    • Method Detail

      • slice

        public abstract Slice slice​(long offset,
                                    long length,
                                    boolean isDeflatedZipEntry,
                                    long inflatedLengthHint)
        Get a child Slice from this parent Slice. The child slice must be smaller than the parent slice, and completely contained within it.
        Parameters:
        offset - The offset to start slicing from, relative to this parent slice's start position.
        length - The length of the slice.
        isDeflatedZipEntry - true if this is a deflated zip entry
        inflatedLengthHint - the uncompressed size of a deflated zip entry, or -1 if unknown, or 0 of this is not a deflated zip entry.
        Returns:
        The child slice.
      • open

        public java.io.InputStream open()
                                 throws java.io.IOException
        Open this Slice as an InputStream.
        Returns:
        the input stream
        Throws:
        java.io.IOException - if an inflater cannot be created for this Slice.
      • open

        public java.io.InputStream open​(Resource resourceToClose)
                                 throws java.io.IOException
        Open this Slice as an InputStream.
        Parameters:
        resourceToClose - the Resource to close when the returned InputStream is closed, or null if none.
        Returns:
        the input stream
        Throws:
        java.io.IOException - if an inflater cannot be created for this Slice.
      • load

        public abstract byte[] load()
                             throws java.io.IOException
        Load the slice as a byte array.
        Returns:
        the byte[]
        Throws:
        java.io.IOException - Signals that an I/O exception has occurred.
      • loadAsString

        public java.lang.String loadAsString()
                                      throws java.io.IOException
        Load the slice as a string.
        Returns:
        the string
        Throws:
        java.io.IOException - if slice cannot be read.
      • read

        public java.nio.ByteBuffer read()
                                 throws java.io.IOException
        Read the slice into a ByteBuffer.
        Returns:
        the byte buffer
        Throws:
        java.io.IOException - Signals that an I/O exception has occurred.
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Throws:
        java.io.IOException
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • equals

        public boolean equals​(java.lang.Object o)
        Overrides:
        equals in class java.lang.Object