Class BytePtr

All Implemented Interfaces:
Iterable<BytePtr>

public final class BytePtr
extends Struct<BytePtr>
Points to an 8-bit signed value (char * in C).
  • Constructor Details

    • BytePtr

      public BytePtr()
      Creates a new BytePtr with a value of 0.
    • BytePtr

      public BytePtr​(byte value)
      Creates a new BytePtr and initializes it with the specified value.
      Parameters:
      value - the value.
  • Method Details

    • get

      public byte get()
      Returns the current value.
      Returns:
      the value.
    • set

      public void set​(byte value)
      Sets the value.
      Parameters:
      value - the new value.
    • toStringAsciiZ

      public String toStringAsciiZ()
      Returns a String created from the NUL-terminated C string pointed to by this BytePtr. Non ASCII characters will be replaced with '?' in the result. This method is more efficient than using toStringZ(Charset) with ASCII as Charset.
      Returns:
      a String containing the same characters as the C string pointed to.
    • toStringZ

      public String toStringZ()
      Returns a String created from the NUL-terminated C string pointed to by this BytePtr using the default Charset. Illegal characters will be replaced with '?' in the result. This assumes that the default Charset is an 8-bit encoding or a variable length encoding with 8-bits as smallest bit length such as UTF-8.
      Returns:
      a String converted from the C string bytes.
    • toStringZ

      public String toStringZ​(Charset charset)
      Returns a String created from the NUL-terminated C string pointed to by this BytePtr using the specified Charset. Illegal characters will be replaced with '?' in the result.
      Parameters:
      charset - the Charset to use. Must be an 8-bit or variable length character encoding with 8-bits as smallest value and that can be NUL-terminated (e.g. UTF-8).
      Returns:
      a String converted from the C string bytes.
    • asByteBuffer

      public ByteBuffer asByteBuffer​(int n)
      Returns a ByteBuffer which reads and writes to the same memory location pointed to by this BytePtr.
      Parameters:
      n - the maximum number of bytes the ByteBuffer can read/write. This will be the ByteBuffer's capacity.
      Returns:
      the ByteBuffer.
    • toByteArray

      public byte[] toByteArray​(int n)
      Copies n bytes from the memory pointed to by this BytePtr to a new byte[] instance.
      Parameters:
      n - the number of bytes to copy.
      Returns:
      the byte[].
    • get

      public void get​(byte[] dst)
      Copies dst.length bytes from the memory pointed to by this BytePtr to dst.
      Parameters:
      dst - the destination.
    • get

      public void get​(byte[] dst, int offset, int count)
      Copies count bytes from the memory pointed to by this BytePtr to dst starting at offset offset.
      Parameters:
      dst - the destination.
      offset - the offset within the destination array to start copying to.
      count - the number of elements to copy.
    • set

      public void set​(byte[] src)
      Copies src.length bytes from src to the memory pointed to by this BytePtr.
      Parameters:
      src - the source.
    • set

      public void set​(byte[] src, int offset, int count)
      Copies count bytes from src starting at offset offset to the memory pointed to by this BytePtr.
      Parameters:
      src - the source.
      offset - the offset within the source array to start copying from.
      count - the number of elements to copy.
    • toBytePtrAsciiZ

      public static BytePtr toBytePtrAsciiZ​(String s)
      Converts the specified String to a NUL-terminated C string of ASCII characters. Non ASCII characters will be replaced with '?' in the result. The memory will be allocated on the GCed heaped. This method is more efficient than using toStringZ(Charset) with ASCII as Charset.
      Parameters:
      s - the String to convert.
      Returns:
      a BytePtr which points to the first character in the result.
    • toBytePtrAsciiZ

      public static BytePtr toBytePtrAsciiZ​(String s, boolean useNativeHeap)
      Converts the specified String to a NUL-terminated C string of ASCII characters. Non ASCII characters will be replaced with '?' in the result. This method is more efficient than using toStringZ(Charset) with ASCII as Charset.
      Parameters:
      s - the String to convert.
      useNativeHeap - whether the memory should be allocated on the native heap using malloc() or on the GCed heap.
      Returns:
      a BytePtr which points to the first character in the result.
    • toBytePtrZ

      public static BytePtr toBytePtrZ​(String s)
      Converts the specified String to a NUL-terminated C string using the default Charset. Illegal characters will be replaced with '?' in the result. The memory will be allocated on the GCed heaped. This assumes that the default Charset is an 8-bit encoding or a variable length encoding with 8-bits as smallest bit length such as UTF-8.
      Parameters:
      s - the String to convert.
      Returns:
      a BytePtr which points to the first character in the result.
    • toBytePtrZ

      public static BytePtr toBytePtrZ​(String s, Charset charset)
      Converts the specified String to a NUL-terminated C string using the specified Charset. Illegal characters will be replaced with '?' in the result. The memory will be allocated on the GCed heaped.
      Parameters:
      s - the String to convert.
      charset - the Charset to use. Must be an 8-bit or variable length character encoding with 8-bits as smallest value and that can be NUL-terminated (e.g. UTF-8).
      Returns:
      a BytePtr which points to the first character in the result.
    • toBytePtrZ

      public static BytePtr toBytePtrZ​(String s, Charset charset, boolean useNativeHeap)
      Converts the specified String to a NUL-terminated C string using the specified Charset. Illegal characters will be replaced with '?' in the result.
      Parameters:
      s - the String to convert.
      charset - the Charset to use. Must be an 8-bit or variable length character encoding with 8-bits as smallest value and that can be NUL-terminated (e.g. UTF-8).
      useNativeHeap - whether the memory should be allocated on the native heap using malloc() or on the GCed heap.
      Returns:
      a BytePtr which points to the first character in the result.