- All Known Implementing Classes:
BufWriterImpl
Supports writing portions of a classfile to a growable buffer. Methods
are provided to write various standard entities (e.g.,
u2, u4)
to the end of the buffer, as well as to create constant pool entries.- Since:
- 22
-
Method Summary
Modifier and TypeMethodDescriptionReturns aByteBufferview of the bytes in the buffer.booleancanWriteDirect(ConstantPool other) Returns whether the provided constant pool is index-compatible with this one.Returns the constant pool builder associated with this buffer.voidcopyTo(byte[] array, int bufferOffset) Copy the contents of the buffer into a byte array.voidpatchInt(int offset, int size, int value) Patch a previously written integer value.voidreserveSpace(int freeBytes) Ensure that the buffer has at leastfreeBytesbytes of unused spaceintsize()Returns the number of bytes that have been written to the buffer.voidwriteBytes(byte[] arr) Write the contents of a byte array to the buffervoidwriteBytes(byte[] arr, int start, int length) Write a range of a byte array to the buffervoidwriteBytes(BufWriter other) Write the contents of anotherBufWriterto the buffervoidwriteDouble(double x) Write a double value to the buffervoidwriteFloat(float x) Write a float value to the buffervoidwriteIndex(PoolEntry entry) Write the index of the specified constant pool entry, as au2, to the buffervoidwriteIndexOrZero(PoolEntry entry) Write the index of the specified constant pool entry, as au2, to the buffer, or zero if the entry is nullvoidwriteInt(int x) Write a signed int to the buffervoidwriteIntBytes(int intSize, long intValue) Write a 1, 2, 4, or 8 byte integer value to the buffer.<T extends WritableElement<?>>
voidWrite a list of entities to the buffer.voidwriteListIndices(List<? extends PoolEntry> list) Write a list of constant pool entry indexes to the buffer.voidwriteLong(long x) Write a long value to the buffervoidwriteU1(int x) Write an unsigned byte to the buffervoidwriteU2(int x) Write an unsigned short to the buffer
-
Method Details
-
constantPool
ConstantPoolBuilder constantPool()Returns the constant pool builder associated with this buffer.- Returns:
- the constant pool builder associated with this buffer
-
canWriteDirect
Returns whether the provided constant pool is index-compatible with this one. This may be because they are the same constant pool, or because this constant pool was copied from the other.- Parameters:
other- the other constant pool- Returns:
- whether the provided constant pool is index-compatible with this one
-
reserveSpace
void reserveSpace(int freeBytes) Ensure that the buffer has at leastfreeBytesbytes of unused space- Parameters:
freeBytes- the number of bytes to reserve
-
writeU1
void writeU1(int x) Write an unsigned byte to the buffer- Parameters:
x- the byte value
-
writeU2
void writeU2(int x) Write an unsigned short to the buffer- Parameters:
x- the short value
-
writeInt
void writeInt(int x) Write a signed int to the buffer- Parameters:
x- the int value
-
writeFloat
void writeFloat(float x) Write a float value to the buffer- Parameters:
x- the float value
-
writeLong
void writeLong(long x) Write a long value to the buffer- Parameters:
x- the long value
-
writeDouble
void writeDouble(double x) Write a double value to the buffer- Parameters:
x- the int value
-
writeBytes
void writeBytes(byte[] arr) Write the contents of a byte array to the buffer- Parameters:
arr- the byte array
-
writeBytes
Write the contents of anotherBufWriterto the buffer- Parameters:
other- the other BufWriter
-
writeBytes
void writeBytes(byte[] arr, int start, int length) Write a range of a byte array to the buffer- Parameters:
arr- the byte arraystart- the offset within the byte array of the rangelength- the length of the range
-
patchInt
void patchInt(int offset, int size, int value) Patch a previously written integer value. Depending on the specified size, the entire value, or the low 1 or 2 bytes, may be written.- Parameters:
offset- the offset at which to patchsize- the size of the integer value being written, in bytesvalue- the integer value
-
writeIntBytes
void writeIntBytes(int intSize, long intValue) Write a 1, 2, 4, or 8 byte integer value to the buffer. Depending on the specified size, the entire value, or the low 1, 2, or 4 bytes, may be written.- Parameters:
intSize- the size of the integer value being written, in bytesintValue- the integer value
-
writeIndex
Write the index of the specified constant pool entry, as au2, to the buffer- Parameters:
entry- the constant pool entry- Throws:
NullPointerException- if the entry is null
-
writeIndexOrZero
Write the index of the specified constant pool entry, as au2, to the buffer, or zero if the entry is null- Parameters:
entry- the constant pool entry
-
writeList
Write a list of entities to the buffer. The length of the list is written as au2, followed by the bytes corresponding to each element in the list. Writing of the entities is delegated to the entry.- Type Parameters:
T- the type of entity- Parameters:
list- the entities
-
writeListIndices
Write a list of constant pool entry indexes to the buffer. The length of the list is written as au2, followed by au2for each entry in the list.- Parameters:
list- the list of entries
-
size
int size()Returns the number of bytes that have been written to the buffer.- Returns:
- the number of bytes that have been written to the buffer
-
asByteBuffer
ByteBuffer asByteBuffer()Returns aByteBufferview of the bytes in the buffer.- Returns:
- a
ByteBufferview of the bytes in the buffer
-
copyTo
void copyTo(byte[] array, int bufferOffset) Copy the contents of the buffer into a byte array.- Parameters:
array- the byte arraybufferOffset- the offset into the array at which to write the contents of the buffer
-