Package com.google.protobuf

# Class UnsafeByteOperations

java.lang.Object
com.google.protobuf.UnsafeByteOperations

public final class UnsafeByteOperations extends Object
Provides a number of unsafe byte operations to be used by advanced applications with high performance requirements. These methods are referred to as "unsafe" because 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 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

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!

## Method Summary

Modifier and Type
Method
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(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`.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Method Details

### unsafeWrap

public static ByteString unsafeWrap(byte[] buffer)
An unsafe operation that returns a `ByteString` that is backed by the provided buffer.

Parameters:
  `buffer` - the buffer to be wrapped
Returns:
  a `ByteString` backed by the provided buffer

### unsafeWrap

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` - the buffer to be wrapped
  `offset` - the offset of the wrapped region
  `length` - the number of bytes of the wrapped region
Returns:
  a `ByteString` backed by the provided buffer

### unsafeWrap

public static ByteString unsafeWrap(ByteBuffer buffer)
An unsafe operation that returns a `ByteString` that is backed by the provided buffer.

Parameters:
  `buffer` - the Java NIO buffer to be wrapped
Returns:
  a `ByteString` backed by the provided buffer

### unsafeWriteTo

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 `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.

Parameters:
  `bytes` - the `ByteString` to be written
  `output` - the output to receive the bytes
Throws:
  `IOException` - if an I/O error occurs