public class DeltaIndex extends Object
The index can be passed a result buffer, and output an instruction sequence
that transforms the source buffer used by the index into the result buffer.
The instruction sequence can be executed by
BinaryDelta
to recreate the
result buffer.
An index stores the entire contents of the source buffer, but also a table of
block identities mapped to locations where the block appears in the source
buffer. The mapping table uses 12 bytes for every 16 bytes of source buffer,
and is therefore ~75% of the source buffer size. The overall index is ~1.75x
the size of the source buffer. This relationship holds for any JVM, as only a
constant number of objects are allocated per index. Callers can use the
method getIndexSize()
to obtain a reasonably accurate estimate of
the complete heap space used by this index.
A DeltaIndex
is thread-safe. Concurrent threads can use the same
index to encode delta instructions for different result buffers.
Constructor and Description |
---|
DeltaIndex(byte[] sourceBuffer)
Construct an index from the source file.
|
Modifier and Type | Method and Description |
---|---|
void |
encode(OutputStream out,
byte[] res)
Generate a delta sequence to recreate the result buffer.
|
boolean |
encode(OutputStream out,
byte[] res,
int deltaSizeLimit)
Generate a delta sequence to recreate the result buffer.
|
static long |
estimateIndexSize(int sourceLength)
Estimate the size of an index for a given source.
|
long |
getIndexSize()
Get an estimate of the memory required by this index.
|
long |
getSourceSize()
Get size of the source buffer this index has scanned.
|
String |
toString() |
public DeltaIndex(byte[] sourceBuffer)
sourceBuffer
- the source file's raw contents. The buffer will be held by the
index instance to facilitate matching, and therefore must not
be modified by the caller.public static long estimateIndexSize(int sourceLength)
This is roughly a worst-case estimate. The actual index may be smaller.
sourceLength
- length of the source, in bytes.1.75 * sourceLength
.public long getSourceSize()
public long getIndexSize()
getSourceSize()
, as well as a rough approximation of JVM
object overheads.public void encode(OutputStream out, byte[] res) throws IOException
There is no limit on the size of the delta sequence created. This is the
same as encode(out, res, 0)
.
out
- stream to receive the delta instructions that can transform
this index's source buffer into res
. This stream
should be buffered, as instructions are written directly to it
in small bursts.res
- the desired result buffer. The generated instructions will
recreate this buffer when applied to the source buffer stored
within this index.IOException
- the output stream refused to write the instructions.public boolean encode(OutputStream out, byte[] res, int deltaSizeLimit) throws IOException
out
- stream to receive the delta instructions that can transform
this index's source buffer into res
. This stream
should be buffered, as instructions are written directly to it
in small bursts. If the caller might need to discard the
instructions (such as when deltaSizeLimit would be exceeded)
the caller is responsible for discarding or rewinding the
stream when this method returns false.res
- the desired result buffer. The generated instructions will
recreate this buffer when applied to the source buffer stored
within this index.deltaSizeLimit
- maximum number of bytes that the delta instructions can
occupy. If the generated instructions would be longer than
this amount, this method returns false. If 0, there is no
limit on the length of delta created.IOException
- the output stream refused to write the instructions.Copyright © 2017 Eclipse JGit Project. All rights reserved.