Top Description Constructors Methods
com.google.protobuf

public final Class UnsafeByteOperations

extends Object
Class Inheritance
Annotations
@ExperimentalApi
Imports
java.io.IOException, java.nio.ByteBuffer

Provides a number of unsafe byte operations to be used by advanced applications with high performance requirements. These methods are referred to as "unsafe" due to the fact that they potentially expose the backing buffer of a 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:

Each of these issues will occur in parts of the code base that are entirely distinct from the parts of the code base modifying the buffer. In fact, both parts of the code base may be correct - it is the bridging with the unsafe operations that was in error!

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static ByteString

Returns:

a ByteString backed by the provided buffer
unsafeWrap
(byte[]
the buffer to be wrapped
buffer
)

An unsafe operation that returns a ByteString that is backed by the provided buffer.

public static ByteString

Returns:

a ByteString backed by the provided buffer
unsafeWrap
(byte[]
the buffer to be wrapped
buffer
,
int
the offset of the wrapped region
offset
,
int
the number of bytes of the wrapped region
length
)

An unsafe operation that returns a ByteString that is backed by a subregion of the provided buffer.

public static ByteString

Returns:

a ByteString backed by the provided buffer
unsafeWrap
(ByteBuffer
the Java NIO buffer to be wrapped
buffer
)

An unsafe operation that returns a ByteString that is backed by the provided buffer.

public static void
unsafeWriteTo(ByteString
the ByteString to be written
bytes
,
ByteOutput
the output to receive the bytes
output
)

Writes the given ByteString to the provided ByteOutput.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

UnsafeByteOperationsback to summary
private UnsafeByteOperations()

Method Detail

unsafeWrapback to summary
public static ByteString unsafeWrap(byte[] buffer)

An unsafe operation that returns a ByteString that is backed by the provided buffer.

Parameters
buffer:byte[]

the buffer to be wrapped

Returns:ByteString

a ByteString backed by the provided buffer

unsafeWrapback to summary
public 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.

Parameters
buffer:byte[]

the buffer to be wrapped

offset:int

the offset of the wrapped region

length:int

the number of bytes of the wrapped region

Returns:ByteString

a ByteString backed by the provided buffer

unsafeWrapback to summary
public static ByteString unsafeWrap(ByteBuffer buffer)

An unsafe operation that returns a ByteString that is backed by the provided buffer.

Parameters
buffer:ByteBuffer

the Java NIO buffer to be wrapped

Returns:ByteString

a ByteString backed by the provided buffer

unsafeWriteToback to summary
public static void unsafeWriteTo(ByteString bytes, ByteOutput output) throws IOException

Writes the given ByteString to the provided ByteOutput. Calling this method may result in multiple operations on the target ByteOutput (i.e. for roped ByteStrings).

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.

Parameters
bytes:ByteString

the ByteString to be written

output:ByteOutput

the output to receive the bytes

Exceptions
IOException:
if an I/O error occurs