APIdia Launch Partner
Top Description Methods
java.lang.classfile

public sealed Interface BufWriter

permits BufWriterImpl
Known Direct Implementers
jdk.internal.classfile.impl.BufWriterImpl
Imports
java.lang.classfile.constantpool.ConstantPool, .ConstantPoolBuilder, .PoolEntry, java.nio.ByteOrder, jdk.internal.classfile.impl.BufWriterImpl

Advanced 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.

Since
24
See Also
AttributeMapper#writeAttribute(BufWriter, Attribute)

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

whether the provided constant pool is index-compatible with the constant pool of this buffer
canWriteDirect
(ConstantPool
the other constant pool
other
)

Returns whether the provided constant pool is index-compatible with the constant pool of this buffer.

public ConstantPoolBuilder

Returns:

the constant pool builder associated with this buffer
constantPool
()

Returns the constant pool builder associated with this buffer.

public void
patchInt(int
the offset in this buffer at which to patch
offset
,
int
the size of the integer value being written, in bytes
size
,
int
the integer value to be truncated
value
)

Patches a previously written integer value.

public void
reserveSpace(int
the number of bytes to reserve
freeBytes
)

Ensures that the buffer has at least freeBytes bytes of free space in the end of the buffer.

public int

Returns:

the number of bytes that have been written to the buffer
size
()

Returns the number of bytes that have been written to the buffer.

public void
writeBytes(byte[]
the byte array
arr
)

Writes the contents of a byte array to the buffer.

public void
writeBytes(byte[]
the byte array
arr
,
int
the start offset of the range within the byte array
start
,
int
the length of the range
length
)

Writes a range of a byte array to the buffer.

public void
writeDouble(double
the double value
x
)

Writes a double value, of 8 bytes, to the buffer.

public void
writeFloat(float
the float value
x
)

Writes a float value, of 4 bytes, to the buffer.

public void
writeIndex(PoolEntry
the constant pool entry
entry
)

Writes the index of the specified constant pool entry as a u2.

public void
writeIndexOrZero(PoolEntry
the constant pool entry, may be null
entry
)

Writes the index of the specified constant pool entry, or the value 0 if the specified entry is null, as a u2.

public void
writeInt(int
the int value
x
)

Writes 4 bytes, or an int, to the buffer.

public void
writeIntBytes(int
the size of the integer value being written, in bytes
intSize
,
long
the value to be truncated
intValue
)

Writes a multibyte value to the buffer.

public void
writeLong(long
the long value
x
)

Writes 8 bytes, or a long, to the buffer.

public void
writeU1(int
the value to truncate to a byte
x
)

Writes a byte to the buffer.

public void
writeU2(int
the value to truncate to a short
x
)

Writes 2 bytes, or a short, to the buffer.

Method Detail

canWriteDirectback to summary
public boolean canWriteDirect(ConstantPool other)

Returns whether the provided constant pool is index-compatible with the constant pool of this buffer.

This is a shortcut for constantPool().canWriteDirect(other).

Parameters
other:ConstantPool

the other constant pool

Returns:boolean

whether the provided constant pool is index-compatible with the constant pool of this buffer

See Also
ConstantPoolBuilder#canWriteDirect(ConstantPool)
constantPoolback to summary
public ConstantPoolBuilder constantPool()

Returns the constant pool builder associated with this buffer.

Returns:ConstantPoolBuilder

the constant pool builder associated with this buffer

See Also
ClassFileBuilder#constantPool()
patchIntback to summary
public void patchInt(int offset, int size, int value)

Patches a previously written integer value. value is truncated to the given size number of bytes and written at the given offset. The end of this buffer stays unchanged.

API Note

The offset can be obtained by calling size() before writing the previous integer value.

Parameters
offset:int

the offset in this buffer at which to patch

size:int

the size of the integer value being written, in bytes

value:int

the integer value to be truncated

Exceptions
IndexOutOfBoundsException:
if patched int is outside of bounds
See Also
size()
reserveSpaceback to summary
public void reserveSpace(int freeBytes)

Ensures that the buffer has at least 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.

API Note

This is a hint that changes no visible state of the buffer; it helps to reduce reallocation of the underlying storage by allocating sufficient space at once.

Parameters
freeBytes:int

the number of bytes to reserve

sizeback to summary
public int size()

Returns the number of bytes that have been written to the buffer.

Returns:int

the number of bytes that have been written to the buffer

See Also
patchInt(int, int, int)
writeBytesback to summary
public void writeBytes(byte[] arr)

Writes the contents of a byte array to the buffer.

Parameters
arr:byte[]

the byte array

writeBytesback to summary
public void writeBytes(byte[] arr, int start, int length)

Writes a range of a byte array to the buffer.

Parameters
arr:byte[]

the byte array

start:int

the start offset of the range within the byte array

length:int

the length of the range

Exceptions
IndexOutOfBoundsException:
if range is outside the array bounds
writeDoubleback to summary
public void writeDouble(double x)

Writes a double value, of 8 bytes, to the buffer.

In the conversions, all NaN values of the double may or may not be collapsed into a single "canonical" NaN value.

Parameters
x:double

the double value

writeFloatback to summary
public void writeFloat(float x)

Writes a float value, of 4 bytes, to the buffer.

In the conversions, all NaN values of the float may or may not be collapsed into a single "canonical" NaN value.

Parameters
x:float

the float value

writeIndexback to summary
public void writeIndex(PoolEntry entry)

Writes the index of the specified constant pool entry 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.

Parameters
entry:PoolEntry

the constant pool entry

Exceptions
IllegalArgumentException:
if the entry has invalid index
writeIndexOrZeroback to summary
public void writeIndexOrZero(PoolEntry entry)

Writes the index of the specified constant pool entry, or the value 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.

Parameters
entry:PoolEntry

the constant pool entry, may be null

Exceptions
IllegalArgumentException:
if the entry is not null and has invalid index
writeIntback to summary
public void writeInt(int x)

Writes 4 bytes, or an int, to the buffer.

Parameters
x:int

the int value

writeIntBytesback to summary
public void writeIntBytes(int intSize, long intValue)

Writes a multibyte value to the buffer. intValue is truncated to the given intSize number of bytes and written.

Parameters
intSize:int

the size of the integer value being written, in bytes

intValue:long

the value to be truncated

writeLongback to summary
public void writeLong(long x)

Writes 8 bytes, or a long, to the buffer.

Parameters
x:long

the long value

writeU1back to summary
public void writeU1(int x)

Writes a byte to the buffer. x is truncated to a byte and written.

Parameters
x:int

the value to truncate to a byte

writeU2back to summary
public void writeU2(int x)

Writes 2 bytes, or a short, to the buffer. x is truncated to two bytes and written.

Parameters
x:int

the value to truncate to a short