Interface BufWriter
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 TypeMethodDescriptionboolean
Returns whether the provided constant pool is index-compatible with this one.Returns the constant pool builder associated with this buffer.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
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.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
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- Throws:
IndexOutOfBoundsException
- if range is outside of the array bounds
-
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- Throws:
IndexOutOfBoundsException
- if patched int is outside of bounds
-
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:
IllegalArgumentException
- if the entry has invalid index
-
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- Throws:
IllegalArgumentException
- if the entry has invalid index
-
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
-
BufWriter
when preview features are enabled.