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. 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 Type
    Method
    Description
    boolean
    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 least freeBytes bytes of unused space
    int
    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
    writeDouble(double x)
    Write a double value to the buffer
    void
    writeFloat(float x)
    Write a float value to the buffer
    void
    Write the index of the specified constant pool entry, as a u2, to the buffer
    void
    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.
    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

      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(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
      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 patch
      size - the size of the integer value being written, in bytes
      value - 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 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:
      IllegalArgumentException - if the entry has invalid index
    • 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
      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