Class ObjectLoader

java.lang.Object
org.eclipse.jgit.lib.ObjectLoader
Direct Known Subclasses:
ApplyCommand.StreamLoader, DfsInserter.StreamLoader, LargePackedWholeObject, LargePackedWholeObject, LfsBlobLoader, ObjectLoader.Filter, ObjectLoader.SmallObject, PackInserter.Reader.StreamLoader, UnpackedObject.LargeObject

public abstract class ObjectLoader extends Object
Base class for a set of loaders for different representations of Git objects. New loaders are constructed for every object.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    Wraps a delegate ObjectLoader.
    static class 
    Simple loader around the cached byte array.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    private static byte[]
    cloneArray(byte[] data)
     
    void
    Copy this object to the output stream.
    final byte[]
    Obtain a copy of the bytes of this object.
    final byte[]
    getBytes(int sizeLimit)
    Obtain a copy of the bytes of this object.
    abstract byte[]
    Obtain a reference to the (possibly cached) bytes of this object.
    byte[]
    getCachedBytes(int sizeLimit)
    Obtain a reference to the (possibly cached) bytes of this object.
    abstract long
    Get size of object in bytes
    abstract int
    Get Git in pack object type
    boolean
    Whether this object is too large to obtain as a byte array.
    abstract ObjectStream
    Obtain an input stream to read this object's data.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ObjectLoader

      public ObjectLoader()
  • Method Details

    • getType

      public abstract int getType()
      Get Git in pack object type
      Returns:
      Git in pack object type, see Constants.
    • getSize

      public abstract long getSize()
      Get size of object in bytes
      Returns:
      size of object in bytes
    • isLarge

      public boolean isLarge()
      Whether this object is too large to obtain as a byte array.
      Returns:
      true if this object is too large to obtain as a byte array. Objects over a certain threshold should be accessed only by their openStream() to prevent overflowing the JVM heap.
    • getBytes

      public final byte[] getBytes() throws LargeObjectException
      Obtain a copy of the bytes of this object.

      Unlike getCachedBytes() this method returns an array that might be modified by the caller.

      Returns:
      the bytes of this object.
      Throws:
      LargeObjectException - if the object won't fit into a byte array, because isLarge() returns true. Callers should use openStream() instead to access the contents.
    • getBytes

      public final byte[] getBytes(int sizeLimit) throws LargeObjectException, MissingObjectException, IOException
      Obtain a copy of the bytes of this object. If the object size is less than or equal to sizeLimit this method will provide it as a byte array, even if isLarge() is true. This utility is useful for application code that absolutely must have the object as a single contiguous byte array in memory. Unlike getCachedBytes(int) this method returns an array that might be modified by the caller.
      Parameters:
      sizeLimit - maximum number of bytes to return. If the object is larger than this limit, LargeObjectException will be thrown.
      Returns:
      the bytes of this object.
      Throws:
      LargeObjectException - if the object is bigger than sizeLimit, or if OutOfMemoryError occurs during allocation of the result array. Callers should use openStream() instead to access the contents.
      MissingObjectException - the object is large, and it no longer exists.
      IOException - the object store cannot be accessed.
    • getCachedBytes

      public abstract byte[] getCachedBytes() throws LargeObjectException
      Obtain a reference to the (possibly cached) bytes of this object.

      This method offers direct access to the internal caches, potentially saving on data copies between the internal cache and higher level code. Callers who receive this reference must not modify its contents. Changes (if made) will affect the cache but not the repository itself.

      Returns:
      the cached bytes of this object. Do not modify it.
      Throws:
      LargeObjectException - if the object won't fit into a byte array, because isLarge() returns true. Callers should use openStream() instead to access the contents.
    • getCachedBytes

      public byte[] getCachedBytes(int sizeLimit) throws LargeObjectException, MissingObjectException, IOException
      Obtain a reference to the (possibly cached) bytes of this object. If the object size is less than or equal to sizeLimit this method will provide it as a byte array, even if isLarge() is true. This utility is useful for application code that absolutely must have the object as a single contiguous byte array in memory. This method offers direct access to the internal caches, potentially saving on data copies between the internal cache and higher level code. Callers who receive this reference must not modify its contents. Changes (if made) will affect the cache but not the repository itself.
      Parameters:
      sizeLimit - maximum number of bytes to return. If the object size is larger than this limit and isLarge() is true, LargeObjectException will be thrown.
      Returns:
      the cached bytes of this object. Do not modify it.
      Throws:
      LargeObjectException - if the object is bigger than sizeLimit, or if OutOfMemoryError occurs during allocation of the result array. Callers should use openStream() instead to access the contents.
      MissingObjectException - the object is large, and it no longer exists.
      IOException - the object store cannot be accessed.
    • openStream

      public abstract ObjectStream openStream() throws MissingObjectException, IOException
      Obtain an input stream to read this object's data.
      Returns:
      a stream of this object's data. Caller must close the stream when through with it. The returned stream is buffered with a reasonable buffer size.
      Throws:
      MissingObjectException - the object no longer exists.
      IOException - the object store cannot be accessed.
    • copyTo

      public void copyTo(OutputStream out) throws MissingObjectException, IOException
      Copy this object to the output stream.

      For some object store implementations, this method may be more efficient than reading from openStream() into a temporary byte array, then writing to the destination stream.

      The default implementation of this method is to copy with a temporary byte array for large objects, or to pass through the cached byte array for small objects.

      Parameters:
      out - stream to receive the complete copy of this object's data. Caller is responsible for flushing or closing this stream after this method returns.
      Throws:
      MissingObjectException - the object no longer exists.
      IOException - the object store cannot be accessed, or the stream cannot be written to.
    • cloneArray

      private static byte[] cloneArray(byte[] data)