Class Bytes

java.lang.Object
dev.gmitch215.bytebox.js.Bytes

public final class Bytes extends Object
Converts between a Java byte[] and the runtime's buffers.

Every platform API that carries bytes carries an ArrayBuffer, because that is what the runtime speaks. Java code holds a byte[]. This is the seam, and the methods that take bytes accept both so most code never needs it.

 byte[] digest = Bytes.fromBuffer(Crypto.sha256("hello"));
 String base64 = Text.toBase64(digest);
 byte[] raw = Bytes.fromBuffer(Text.fromBase64(base64));

Both directions copy. A WebAssembly garbage-collected array and a JavaScript buffer are separate objects in separate heaps, so there is no view of one that is also the other, and a conversion is proportional to the length rather than free.

Since:
1.0.0
  • Method Summary

    Modifier and Type
    Method
    Description
    static byte[]
    fromBuffer(org.teavm.jso.typedarrays.ArrayBuffer buffer)
    A buffer as a Java array.
    static byte[]
    fromView(org.teavm.jso.typedarrays.ArrayBufferView view)
    A view as a Java array.
    static org.teavm.jso.typedarrays.ArrayBuffer
    toBuffer(byte[] bytes)
    A Java array as a buffer.
    static org.teavm.jso.typedarrays.Uint8Array
    toUnsignedView(byte[] bytes)
    A Java array as an unsigned byte view.
    static org.teavm.jso.typedarrays.Int8Array
    toView(byte[] bytes)
    A Java array as a signed byte view.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • toBuffer

      public static org.teavm.jso.typedarrays.ArrayBuffer toBuffer(byte[] bytes)
      A Java array as a buffer.
      Parameters:
      bytes - the bytes
      Returns:
      a buffer holding a copy of them
    • toView

      public static org.teavm.jso.typedarrays.Int8Array toView(byte[] bytes)
      A Java array as a signed byte view.

      The view a JavaScript API wants when it takes a TypedArray rather than a buffer. toUnsignedView(byte[]) is the same bytes read as unsigned, which is what most of them document.

      Parameters:
      bytes - the bytes
      Returns:
      a view over a copy of them
    • toUnsignedView

      public static org.teavm.jso.typedarrays.Uint8Array toUnsignedView(byte[] bytes)
      A Java array as an unsigned byte view.

      The same bytes as toView(byte[]): the difference is how JavaScript reads element 0x80, not what is stored. Java has no unsigned byte, so a value above 127 arrives negative and comes back negative.

      Parameters:
      bytes - the bytes
      Returns:
      a view over a copy of them
    • fromBuffer

      public static byte[] fromBuffer(org.teavm.jso.typedarrays.ArrayBuffer buffer)
      A buffer as a Java array.
      Parameters:
      buffer - the buffer
      Returns:
      a copy of its bytes
    • fromView

      public static byte[] fromView(org.teavm.jso.typedarrays.ArrayBufferView view)
      A view as a Java array.

      Reads the region the view covers rather than the whole buffer behind it, so a view onto part of a larger buffer converts to that part.

      Parameters:
      view - the view
      Returns:
      a copy of the bytes it covers