public sealed interface BufWriter
BufWriter
is a preview API of the Java platform.
Preview features may be removed in a future release, or upgraded to permanent features of the Java platform.
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 aByteBuffer
view of the bytes in the buffer.boolean
Returns whether the provided constant pool is index-compatible with this one.Returns the constant pool builder associated with this buffer.void
copyTo
(byte[] array, int bufferOffset) Copy the contents of the buffer into a byte array.void
patchInt
(int offset, int size, int value) Patch a previously written integer value.void
reserveSpace
(int freeBytes) Ensure that the buffer has at leastfreeBytes
bytes of unused spaceint
size()
Returns the number of bytes that have been written to the buffer.void
writeBytes
(byte[] arr) Write the contents of a byte array to the buffervoid
writeBytes
(byte[] arr, int start, int length) Write a range of a byte array to the buffervoid
writeBytes
(BufWriterPREVIEW other) void
writeDouble
(double x) Write a double value to the buffervoid
writeFloat
(float x) Write a float value to the buffervoid
writeIndex
(PoolEntryPREVIEW entry) Write the index of the specified constant pool entry, as au2
, to the buffervoid
writeIndexOrZero
(PoolEntryPREVIEW entry) Write the index of the specified constant pool entry, as au2
, to the buffer, or zero if the entry is nullvoid
writeInt
(int x) Write a signed int to the buffervoid
writeIntBytes
(int intSize, long intValue) Write a 1, 2, 4, or 8 byte integer value to the buffer.<T extends WritableElementPREVIEW<?>>
voidWrite a list of entities to the buffer.void
writeListIndices
(List<? extends PoolEntryPREVIEW> list) Write a list of constant pool entry indexes to the buffer.void
writeLong
(long x) Write a long value to the buffervoid
writeU1
(int x) Write an unsigned byte to the buffervoid
writeU2
(int x) Write an unsigned short to the buffer
-
Method Details
-
constantPool
ConstantPoolBuilderPREVIEW 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 leastfreeBytes
bytes 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
-
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
-
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
-
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 aByteBuffer
view of the bytes in the buffer.- Returns:
- a
ByteBuffer
view 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
-
BufWriter
when preview features are enabled.