Class PackInserter.PackStream

  • All Implemented Interfaces:
    java.io.Closeable, java.io.Flushable, java.lang.AutoCloseable
    Enclosing class:
    PackInserter

    private class PackInserter.PackStream
    extends java.io.OutputStream
    Stream that writes to a pack file.

    Backed by two views of the same open file descriptor: a random-access file, and an output stream. Seeking in the file causes subsequent writes to the output stream to occur wherever the file pointer is pointing, so we need to take care to always seek to the end of the file before writing a new object.

    Callers should always use seek(long) to seek, rather than reaching into the file member. As long as this contract is followed, calls to write(byte[], int, int) are guaranteed to write at the end of the file, even if there have been intermediate seeks.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private boolean atEnd  
      (package private) java.util.zip.DeflaterOutputStream compress  
      (package private) java.util.zip.CRC32 crc32  
      private java.util.zip.Deflater deflater  
      private java.io.RandomAccessFile file  
      (package private) byte[] hdrBuf  
      private CountingOutputStream out  
    • Constructor Summary

      Constructors 
      Constructor Description
      PackStream​(java.io.File pack)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      (package private) void beginObject​(int objectType, long length)  
      void close()  
      private int encodeTypeSize​(int type, long rawLength)  
      (package private) byte[] finishPack()  
      (package private) long getOffset()  
      (package private) byte[] inflate​(long filePos, int len)  
      (package private) void seek​(long offset)  
      private int setInput​(long filePos, java.util.zip.Inflater inf, byte[] buf)  
      void write​(byte[] data, int off, int len)  
      void write​(int b)  
      • Methods inherited from class java.io.OutputStream

        flush, write
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • hdrBuf

        final byte[] hdrBuf
      • crc32

        final java.util.zip.CRC32 crc32
      • compress

        final java.util.zip.DeflaterOutputStream compress
      • file

        private final java.io.RandomAccessFile file
      • deflater

        private final java.util.zip.Deflater deflater
      • atEnd

        private boolean atEnd
    • Constructor Detail

      • PackStream

        PackStream​(java.io.File pack)
            throws java.io.IOException
        Throws:
        java.io.IOException
    • Method Detail

      • getOffset

        long getOffset()
      • seek

        void seek​(long offset)
           throws java.io.IOException
        Throws:
        java.io.IOException
      • beginObject

        void beginObject​(int objectType,
                         long length)
                  throws java.io.IOException
        Throws:
        java.io.IOException
      • encodeTypeSize

        private int encodeTypeSize​(int type,
                                   long rawLength)
      • write

        public void write​(int b)
                   throws java.io.IOException
        Specified by:
        write in class java.io.OutputStream
        Throws:
        java.io.IOException
      • write

        public void write​(byte[] data,
                          int off,
                          int len)
                   throws java.io.IOException
        Overrides:
        write in class java.io.OutputStream
        Throws:
        java.io.IOException
      • finishPack

        byte[] finishPack()
                   throws java.io.IOException
        Throws:
        java.io.IOException
      • close

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

        byte[] inflate​(long filePos,
                       int len)
                throws java.io.IOException,
                       java.util.zip.DataFormatException
        Throws:
        java.io.IOException
        java.util.zip.DataFormatException
      • setInput

        private int setInput​(long filePos,
                             java.util.zip.Inflater inf,
                             byte[] buf)
                      throws java.io.IOException
        Throws:
        java.io.IOException