Module java.base
Package java.lang.classfilePREVIEW

# Interface BufWriter

public sealed interface BufWriter
`BufWriter` is a preview API of the Java platform.
Programs can only use `BufWriter` when preview features are enabled.
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. Method 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 Type
  Method
  Description
  `ByteBuffer`
  `asByteBuffer()`
  Returns a `ByteBuffer` view of the bytes in the buffer.
  `boolean`
  `canWriteDirect(ConstantPoolPREVIEW other)`
  Returns whether the provided constant pool is index-compatible with this one.
  `ConstantPoolBuilderPREVIEW`
  `constantPool()`
  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 least `freeBytes` bytes of unused space
  `int`
  `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 buffer
  `void`
  `writeBytes(byte[] arr, int start, int length)`
  Write a range of a byte array to the buffer
  `void`
  `writeBytes(BufWriterPREVIEW other)`
  Write the contents of another `BufWriter`PREVIEW to the buffer
  `void`
  `writeDouble(double x)`
  Write a double value to the buffer
  `void`
  `writeFloat(float x)`
  Write a float value to the buffer
  `void`
  `writeIndex(PoolEntryPREVIEW entry)`
  Write the index of the specified constant pool entry, as a `u2`, to the buffer
  `void`
  `writeIndexOrZero(PoolEntryPREVIEW entry)`
  Write the index of the specified constant pool entry, as a `u2`, to the buffer, or zero if the entry is null
  `void`
  `writeInt(int x)`
  Write a signed int to the buffer
  `void`
  `writeIntBytes(int intSize, long intValue)`
  Write a 1, 2, 4, or 8 byte integer value to the buffer.
  `<T extends WritableElementPREVIEW<?>>
  void`
  `writeList(List<T> list)`
  Write 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 buffer
  `void`
  `writeU1(int x)`
  Write an unsigned byte to the buffer
  `void`
  `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

    boolean canWriteDirect(ConstantPoolPREVIEW other)
    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 least `freeBytes` 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(BufWriterPREVIEW other)
    Write the contents of another `BufWriter`PREVIEW to the buffer
    Parameters:
      `other` - the other BufWriterPREVIEW

  - ### writeBytes

    void writeBytes(byte[] arr, int start, int length)
    Write a range of a byte array to the buffer
    Parameters:
      `arr` - the byte array
      `start` - the offset within the byte array of the range
      `length` - 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 patch
      `size` - the size of the integer value being written, in bytes
      `value` - 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 bytes
      `intValue` - the integer value

  - ### writeIndex

    void writeIndex(PoolEntryPREVIEW entry)
    Write the index of the specified constant pool entry, as a `u2`, to the buffer
    Parameters:
      `entry` - the constant pool entry
    Throws:
      `NullPointerException` - if the entry is null

  - ### writeIndexOrZero

    void writeIndexOrZero(PoolEntryPREVIEW entry)
    Write the index of the specified constant pool entry, as a `u2`, to the buffer, or zero if the entry is null
    Parameters:
      `entry` - the constant pool entry

  - ### writeList

    <T extends WritableElementPREVIEW<?>> void writeList(List<T> list)
    Write a list of entities to the buffer. The length of the list is written as a `u2`, 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

    void writeListIndices(List<? extends PoolEntryPREVIEW> list)
    Write a list of constant pool entry indexes to the buffer. The length of the list is written as a `u2`, followed by a `u2` for 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 a `ByteBuffer` 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 array
      `bufferOffset` - the offset into the array at which to write the contents of the buffer