Class Chunk
- java.lang.Object
-
- com.google.devrel.gmscore.tools.apk.arsc.Chunk
-
- All Implemented Interfaces:
SerializableResource
- Direct Known Subclasses:
ChunkWithChunks,LibraryChunk,StringPoolChunk,TypeChunk,TypeSpecChunk,UnknownChunk,XmlNodeChunk,XmlResourceMapChunk
public abstract class Chunk extends java.lang.Object implements SerializableResource
Represents a generic chunk.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classChunk.TypeTypes of chunks that can exist.
-
Field Summary
Fields Modifier and Type Field Description protected intchunkSizeheaderSize + dataSize.protected intheaderSizeSize of the chunk header in bytes.static intMETADATA_SIZEThe number of bytes in every chunk that describes chunk type, header size, and chunk size.protected intoffsetOffset of this chunk from the start of the file.static intPAD_BOUNDARYThe byte boundary to pad chunks on.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description intgetHeaderSize()Returns the size of this chunk's header.intgetOriginalChunkSize()Returns the size of this chunk when it was first read from a buffer.ChunkgetParent()Returns the parent to this chunk, if any.protected abstract Chunk.TypegetType()protected voidinit(java.nio.ByteBuffer buffer)Finishes initialization of a chunk.static ChunknewInstance(java.nio.ByteBuffer buffer)Creates a new chunk whose contents start atbuffer's current position.static ChunknewInstance(java.nio.ByteBuffer buffer, Chunk parent)Creates a new chunk whose contents start atbuffer'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 voidwriteHeader(java.nio.ByteBuffer output)Writes the remaining header (after the type,headerSize, andchunkSize).protected voidwriteHeader(java.nio.ByteBuffer output, int chunkSize)Writes the type and header size.protected intwritePad(java.io.DataOutput output, int currentLength)PadsoutputuntilcurrentLengthis on a 4-byte boundary.protected voidwritePayload(java.io.DataOutput output, java.nio.ByteBuffer header, boolean shrink)Writes the chunk payload.
-
-
-
Field Detail
-
PAD_BOUNDARY
public static final int PAD_BOUNDARY
The byte boundary to pad chunks on.- See Also:
- Constant Field Values
-
METADATA_SIZE
public static final int METADATA_SIZE
The number of bytes in every chunk that describes chunk type, header size, and chunk size.- See Also:
- Constant Field Values
-
headerSize
protected final int headerSize
Size of the chunk header in bytes.
-
chunkSize
protected final int chunkSize
headerSize + dataSize. The total size of this chunk.
-
offset
protected final int offset
Offset of this chunk from the start of the file.
-
-
Constructor Detail
-
Chunk
protected Chunk(java.nio.ByteBuffer buffer, @Nullable Chunk parent)
-
-
Method Detail
-
init
protected void init(java.nio.ByteBuffer buffer)
Finishes initialization of a chunk. This should be called immediately after the constructor. This is separate from the constructor so that the header of a chunk can be fully initialized before the payload of that chunk is initialized for chunks that require such behavior.- Parameters:
buffer- The buffer that the payload will be initialized from.
-
getParent
@Nullable public Chunk getParent()
Returns the parent to this chunk, if any. A parent is a chunk whose payload contains this chunk. If there's no parent, null is returned.
-
getType
protected abstract Chunk.Type getType()
-
getHeaderSize
public final int getHeaderSize()
Returns the size of this chunk's header.
-
getOriginalChunkSize
public final int getOriginalChunkSize()
Returns the size of this chunk when it was first read from a buffer. A chunk's size can deviate from this value when its data is modified (e.g. adding an entry, changing a string).A chunk's current size can be determined from the length of the byte array returned from
toByteArray().
-
writeHeader
protected final void writeHeader(java.nio.ByteBuffer output, int chunkSize)Writes the type and header size. We don't know how big this chunk will be (it could be different since the last time we checked), so this needs to be passed in.- Parameters:
output- The buffer that will be written to.chunkSize- The total size of this chunk in bytes, including the header.
-
writeHeader
protected void writeHeader(java.nio.ByteBuffer output)
Writes the remaining header (after the type,headerSize, andchunkSize).- Parameters:
output- The buffer that the header will be written to.
-
writePayload
protected void writePayload(java.io.DataOutput output, java.nio.ByteBuffer header, boolean shrink) throws java.io.IOExceptionWrites the chunk payload. The payload is data in a chunk which is not in the firstheaderSizebytes of the chunk.- Parameters:
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.- Throws:
java.io.IOException- Thrown ifoutputcould not be written to (out of memory).
-
writePad
protected int writePad(java.io.DataOutput output, int currentLength) throws java.io.IOExceptionPadsoutputuntilcurrentLengthis on a 4-byte boundary.- Parameters:
output- TheDataOutputthat will be padded.currentLength- The current length, in bytes, ofoutput- Returns:
- The new length of
output - Throws:
java.io.IOException- Thrown ifoutputcould not be written to.
-
toByteArray
public final byte[] toByteArray() throws java.io.IOExceptionDescription copied from interface:SerializableResourceConverts this resource into an array of bytes representation.- Specified by:
toByteArrayin interfaceSerializableResource- Returns:
- An array of bytes representing this resource.
- Throws:
java.io.IOException
-
toByteArray
public final byte[] toByteArray(boolean shrink) throws java.io.IOExceptionConverts this chunk into an array of bytes representation. Normally you will not need to override this method unless your header changes based on the contents / size of the payload.- Specified by:
toByteArrayin interfaceSerializableResource- Parameters:
shrink- True if, when converting to a byte array, this resource can modify the returned bytes in an effort to reduce the size.- Returns:
- An array of bytes representing this resource.
- Throws:
java.io.IOException
-
newInstance
public static Chunk newInstance(java.nio.ByteBuffer buffer)
Creates a new chunk whose contents start atbuffer's current position.- Parameters:
buffer- A buffer positioned at the start of a chunk.- Returns:
- new chunk
-
newInstance
public static Chunk newInstance(java.nio.ByteBuffer buffer, @Nullable Chunk parent)
Creates a new chunk whose contents start atbuffer's current position.- Parameters:
buffer- A buffer positioned at the start of a chunk.parent- The parent to this chunk (or null if there's no parent).- Returns:
- new chunk
-
-