Class UnsafeByteOperations
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 effects that have spooky-action-at-a-distance-like behavior. In particular, if the bytes value changes out from under a Protocol Buffer:
- serialization may throw
- serialization may succeed but the wrong bytes may be written out
- messages are no longer threadsafe
- hashCode may be incorrect
- can result in a permanent memory leak when used as a key in a long-lived HashMap
- the semantics of many programs may be violated if this is the case
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic ByteStringunsafeWrap(byte[] buffer) An unsafe operation that returns aByteStringthat is backed by the provided buffer.static ByteStringunsafeWrap(byte[] buffer, int offset, int length) An unsafe operation that returns aByteStringthat is backed by a subregion of the provided buffer.static ByteStringunsafeWrap(ByteBuffer buffer) An unsafe operation that returns aByteStringthat is backed by the provided buffer.static voidunsafeWriteTo(ByteString bytes, ByteOutput output) Writes the givenByteStringto the providedByteOutput.
-
Constructor Details
-
UnsafeByteOperations
private UnsafeByteOperations()
-
-
Method Details
-
unsafeWrap
An unsafe operation that returns aByteStringthat is backed by the provided buffer.- Parameters:
buffer- the buffer to be wrapped- Returns:
- a
ByteStringbacked by the provided buffer
-
unsafeWrap
An unsafe operation that returns aByteStringthat is backed by a subregion of the provided buffer.- Parameters:
buffer- the buffer to be wrappedoffset- the offset of the wrapped regionlength- the number of bytes of the wrapped region- Returns:
- a
ByteStringbacked by the provided buffer
-
unsafeWrap
An unsafe operation that returns aByteStringthat is backed by the provided buffer.- Parameters:
buffer- the Java NIO buffer to be wrapped- Returns:
- a
ByteStringbacked by the provided buffer
-
unsafeWriteTo
Writes the givenByteStringto the providedByteOutput. Calling this method may result in multiple operations on the targetByteOutput(i.e. for ropedByteStrings).This method exposes the internal backing buffer(s) of the
ByteStringto theByteOutputin order to avoid additional copying overhead. It would be possible for a maliciousByteOutputto corrupt theByteString. Use with caution!NOTE: The
ByteOutputMUST NOT modify the provided buffers. Doing so may result in corrupted data, which would be difficult to debug.- Parameters:
bytes- theByteStringto be writtenoutput- the output to receive the bytes- Throws:
IOException- if an I/O error occurs
-