Package org.eclipse.jgit.lib
Class ObjectLoader
- java.lang.Object
-
- org.eclipse.jgit.lib.ObjectLoader
-
- Direct Known Subclasses:
LfsBlobLoader
,ObjectLoader.Filter
,ObjectLoader.SmallObject
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
ObjectLoader.Filter
Wraps a delegate ObjectLoader.static class
ObjectLoader.SmallObject
Simple loader around the cached byte array.
-
Constructor Summary
Constructors Constructor Description ObjectLoader()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
copyTo(OutputStream out)
Copy this object to the output stream.byte[]
getBytes()
Obtain a copy of the bytes of this object.byte[]
getBytes(int sizeLimit)
Obtain a copy of the bytes of this object.abstract byte[]
getCachedBytes()
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
getSize()
Get size of object in bytesabstract int
getType()
Get Git in pack object typeboolean
isLarge()
Whether this object is too large to obtain as a byte array.abstract ObjectStream
openStream()
Obtain an input stream to read this object's data.
-
-
-
Method Detail
-
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, becauseisLarge()
returns true. Callers should useopenStream()
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 tosizeLimit
this method will provide it as a byte array, even ifisLarge()
is true. This utility is useful for application code that absolutely must have the object as a single contiguous byte array in memory. UnlikegetCachedBytes(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 thansizeLimit
, or ifOutOfMemoryError
occurs during allocation of the result array. Callers should useopenStream()
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, becauseisLarge()
returns true. Callers should useopenStream()
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 tosizeLimit
this method will provide it as a byte array, even ifisLarge()
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 andisLarge()
is true,LargeObjectException
will be thrown.- Returns:
- the cached bytes of this object. Do not modify it.
- Throws:
LargeObjectException
- if the object is bigger thansizeLimit
, or ifOutOfMemoryError
occurs during allocation of the result array. Callers should useopenStream()
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.
-
-