public sealed interface BufWriter
class file writing support for AttributeMappers. Supports writing portions of a class file to a growable buffer, such as writing various numerical types (e.g., u2, u4), to the end of the buffer, as well as to create constant pool entries. All numeric values in the class file format are big endian. Writing larger numeric values to smaller numeric values are always done with truncation, that the least significant bytes are kept and the other bytes are silently dropped. As a result, numeric value writing methods can write both signed and unsigned values, and users should validate their values before writing if silent dropping of most significant bytes is not the intended behavior.
| Modifier and Type | Method | Description |
|---|---|---|
boolean |
canWriteDirect |
Returns whether the provided constant pool is index-compatible with the constant pool of this buffer. |
ConstantPoolBuilder |
constantPool() |
Returns the constant pool builder associated with this buffer. |
void |
patchInt |
Patches a previously written integer value. |
void |
reserveSpace |
Ensures that the buffer has at least freeBytes bytes of free space in the end of the buffer. |
int |
size() |
Returns the number of bytes that have been written to the buffer. |
void |
writeBytes |
Writes the contents of a byte array to the buffer. |
void |
writeBytes |
Writes a range of a byte array to the buffer. |
void |
writeDouble |
Writes a double value, of 8 bytes, to the buffer. |
void |
writeFloat |
Writes a float value, of 4 bytes, to the buffer. |
void |
writeIndex |
Writes the index of the specified constant pool entry as a u2. |
void |
writeIndexOrZero |
Writes the index of the specified constant pool entry, or the value 0 if the specified entry is null, as a u2. |
void |
writeInt |
Writes 4 bytes, or an int, to the buffer. |
void |
writeIntBytes |
Writes a multibyte value to the buffer. |
void |
writeLong |
Writes 8 bytes, or a long, to the buffer. |
void |
writeU1 |
Writes a byte to the buffer. |
void |
writeU2 |
Writes 2 bytes, or a short, to the buffer. |
ConstantPoolBuilder constantPool()
boolean canWriteDirect(ConstantPool other)
This is a shortcut for constantPool().canWriteDirect(other).
other - the other constant poolvoid reserveSpace(int freeBytes)
freeBytes bytes of free space in the end of the buffer. The writing result is the same without calls to this method, but the writing process may be slower.
freeBytes - the number of bytes to reservevoid writeU1(int x)
x is truncated to a byte and written.x - the value to truncate to a bytevoid writeU2(int x)
x is truncated to two bytes and written.x - the value to truncate to a shortvoid writeInt(int x)
x - the int valuevoid writeFloat(float x)
In the conversions, all NaN values of the float may or may not be collapsed into a single "canonical" NaN value.
x - the float valuevoid writeLong(long x)
x - the long valuevoid writeDouble(double x)
In the conversions, all NaN values of the double may or may not be collapsed into a single "canonical" NaN value.
x - the double valuevoid writeBytes(byte[] arr)
arr - the byte arrayvoid writeBytes(byte[] arr, int start, int length)
arr - the byte arraystart - the start offset of the range within the byte arraylength - the length of the rangeIndexOutOfBoundsException - if range is outside the array boundsvoid patchInt(int offset, int size, int value)
value is truncated to the given size number of bytes and written at the given
offset. The end of this buffer stays unchanged.offset can be obtained by calling size() before writing the previous integer value.offset - the offset in this buffer at which to patchsize - the size of the integer value being written, in bytesvalue - the integer value to be truncatedIndexOutOfBoundsException - if patched int is outside of boundsvoid writeIntBytes(int intSize, long intValue)
intValue is truncated to the given intSize number of bytes and written.intSize - the size of the integer value being written, in bytesintValue - the value to be truncatedvoid writeIndex(PoolEntry entry)
u2. If the entry does not belong to the constant pool of this buffer, it will be converted, and the index of the converted pool entry is written instead.entry - the constant pool entryIllegalArgumentException - if the entry has invalid indexvoid writeIndexOrZero(PoolEntry entry)
0 if the specified entry is null, as a u2. If the entry does not belong to the constant pool of this buffer, it will be converted, and the index of the converted pool entry is written instead.entry - the constant pool entry, may be null
IllegalArgumentException - if the entry is not null and has invalid indexint size()
© 1993, 2025, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/classfile/BufWriter.html