@ExperimentalApi public final class UnsafeByteOperations extends java.lang.Object
ByteString
to the application.
DISCLAIMER: The methods in this class should only be called if it is
guaranteed that the buffer backing the ByteString
will never change! Mutation of a ByteString
can lead to unexpected and undesirable consequences in your application, and will
likely be difficult to debug. Proceed with caution!
This can have a number of significant side affects that have spooky-action-at-a-distance-like behavior. In particular, if the bytes value changes out from under a Protocol Buffer:
Modifier and Type | Method and Description |
---|---|
static ByteString |
unsafeWrap(byte[] buffer)
An unsafe operation that returns a
ByteString that is backed by the provided buffer. |
static ByteString |
unsafeWrap(byte[] buffer,
int offset,
int length)
An unsafe operation that returns a
ByteString that is backed by a subregion of the
provided buffer. |
static ByteString |
unsafeWrap(java.nio.ByteBuffer buffer)
An unsafe operation that returns a
ByteString that is backed by the provided buffer. |
static void |
unsafeWriteTo(ByteString bytes,
ByteOutput output)
Writes the given
ByteString to the provided ByteOutput . |
public static ByteString unsafeWrap(byte[] buffer)
ByteString
that is backed by the provided buffer.buffer
- the buffer to be wrappedByteString
backed by the provided bufferpublic static ByteString unsafeWrap(byte[] buffer, int offset, int length)
ByteString
that is backed by a subregion of the
provided buffer.buffer
- the buffer to be wrappedoffset
- the offset of the wrapped regionlength
- the number of bytes of the wrapped regionByteString
backed by the provided bufferpublic static ByteString unsafeWrap(java.nio.ByteBuffer buffer)
ByteString
that is backed by the provided buffer.buffer
- the Java NIO buffer to be wrappedByteString
backed by the provided bufferpublic static void unsafeWriteTo(ByteString bytes, ByteOutput output) throws java.io.IOException
ByteString
to the provided ByteOutput
. Calling this method may
result in multiple operations on the target ByteOutput
(i.e. for roped ByteString
s).
This method exposes the internal backing buffer(s) of the ByteString
to the ByteOutput
in order to avoid additional copying overhead. It would be possible for a malicious
ByteOutput
to corrupt the ByteString
. Use with caution!
NOTE: The ByteOutput
MUST NOT modify the provided buffers. Doing so
may result in corrupted data, which would be difficult to debug.
bytes
- the ByteString
to be writtenoutput
- the output to receive the bytesjava.io.IOException
- if an I/O error occurs