public abstract class Chunk extends java.lang.Object implements SerializableResource
| Modifier and Type | Class and Description |
|---|---|
static class |
Chunk.Type
Types of chunks that can exist.
|
| Modifier and Type | Field and Description |
|---|---|
protected int |
chunkSize
headerSize + dataSize.
|
protected int |
headerSize
Size of the chunk header in bytes.
|
static int |
METADATA_SIZE
The number of bytes in every chunk that describes chunk type, header size, and chunk size.
|
protected int |
offset
Offset of this chunk from the start of the file.
|
static int |
PAD_BOUNDARY
The byte boundary to pad chunks on.
|
| Modifier | Constructor and Description |
|---|---|
protected |
Chunk(java.nio.ByteBuffer buffer,
Chunk parent) |
| Modifier and Type | Method and Description |
|---|---|
int |
getHeaderSize()
Returns the size of this chunk's header.
|
int |
getOriginalChunkSize()
Returns the size of this chunk when it was first read from a buffer.
|
Chunk |
getParent()
Returns the parent to this chunk, if any.
|
protected abstract Chunk.Type |
getType() |
protected void |
init(java.nio.ByteBuffer buffer)
Finishes initialization of a chunk.
|
static Chunk |
newInstance(java.nio.ByteBuffer buffer)
Creates a new chunk whose contents start at
buffer's current position. |
static Chunk |
newInstance(java.nio.ByteBuffer buffer,
Chunk parent)
Creates a new chunk whose contents start at
buffer's current position. |
byte[] |
toByteArray()
Converts this resource into an array of bytes representation.
|
byte[] |
toByteArray(boolean shrink)
Converts this chunk into an array of bytes representation.
|
protected void |
writeHeader(java.nio.ByteBuffer output)
Writes the remaining header (after the type,
headerSize, and chunkSize). |
protected void |
writeHeader(java.nio.ByteBuffer output,
int chunkSize)
Writes the type and header size.
|
protected int |
writePad(java.io.DataOutput output,
int currentLength)
Pads
output until currentLength is on a 4-byte boundary. |
protected void |
writePayload(java.io.DataOutput output,
java.nio.ByteBuffer header,
boolean shrink)
Writes the chunk payload.
|
public static final int PAD_BOUNDARY
public static final int METADATA_SIZE
protected final int headerSize
protected final int chunkSize
protected final int offset
protected Chunk(java.nio.ByteBuffer buffer,
@Nullable
Chunk parent)
protected void init(java.nio.ByteBuffer buffer)
buffer - The buffer that the payload will be initialized from.@Nullable public Chunk getParent()
protected abstract Chunk.Type getType()
public final int getHeaderSize()
public final int getOriginalChunkSize()
A chunk's current size can be determined from the length of the byte array returned from
toByteArray().
protected final void writeHeader(java.nio.ByteBuffer output,
int chunkSize)
output - The buffer that will be written to.chunkSize - The total size of this chunk in bytes, including the header.protected void writeHeader(java.nio.ByteBuffer output)
headerSize, and chunkSize).output - The buffer that the header will be written to.protected void writePayload(java.io.DataOutput output,
java.nio.ByteBuffer header,
boolean shrink)
throws java.io.IOException
headerSize bytes of the chunk.output - The stream that the payload will be written to.header - The already-written header. This can be modified to fix payload offsets.shrink - True if this payload should be optimized for size.java.io.IOException - Thrown if output could not be written to (out of memory).protected int writePad(java.io.DataOutput output,
int currentLength)
throws java.io.IOException
output until currentLength is on a 4-byte boundary.output - The DataOutput that will be padded.currentLength - The current length, in bytes, of outputoutputjava.io.IOException - Thrown if output could not be written to.public final byte[] toByteArray()
throws java.io.IOException
SerializableResourcetoByteArray in interface SerializableResourcejava.io.IOExceptionpublic final byte[] toByteArray(boolean shrink)
throws java.io.IOException
toByteArray in interface SerializableResourceshrink - True if, when converting to a byte array, this resource can modify the returned
bytes in an effort to reduce the size.java.io.IOExceptionpublic static Chunk newInstance(java.nio.ByteBuffer buffer)
buffer's current position.buffer - A buffer positioned at the start of a chunk.public static Chunk newInstance(java.nio.ByteBuffer buffer, @Nullable Chunk parent)
buffer's current position.buffer - A buffer positioned at the start of a chunk.parent - The parent to this chunk (or null if there's no parent).