# Class UnsafeByteOperations (3.19.4)

    public final class UnsafeByteOperations

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](https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.ByteString) to the application.

**DISCLAIMER:** The methods in this class should only be called if it is
guaranteed that the buffer backing the [ByteString](https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.ByteString) will never change! Mutation of a [ByteString](https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.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:

- 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!

## Inheritance

[java.lang.Object](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html) \> UnsafeByteOperations

## Inherited Members

[Object.clone()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#clone--) [Object.equals(Object)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#equals-java.lang.Object-) [Object.finalize()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#finalize--) [Object.getClass()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#getClass--) [Object.hashCode()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#hashCode--) [Object.notify()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notify--) [Object.notifyAll()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notifyAll--) [Object.toString()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#toString--) [Object.wait()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait--) [Object.wait(long)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-) [Object.wait(long,int)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-int-)

## Static Methods

### unsafeWrap(byte\[\] buffer)

    public static ByteString unsafeWrap(byte[] buffer)

An unsafe operation that returns a [ByteString](https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.ByteString) that is backed by the provided buffer.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `buffer` | `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html[]` the buffer to be wrapped |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.ByteString` | a [ByteString](https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.ByteString) backed by the provided buffer |

### unsafeWrap(byte\[\] buffer, int offset, int length)

    public static ByteString unsafeWrap(byte[] buffer, int offset, int length)

An unsafe operation that returns a [ByteString](https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.ByteString) that is backed by a subregion of the
provided buffer.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `buffer` | `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html[]` the buffer to be wrapped |
| `offset` | `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` the offset of the wrapped region |
| `length` | `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` the number of bytes of the wrapped region |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.ByteString` | a [ByteString](https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.ByteString) backed by the provided buffer |

### unsafeWrap(ByteBuffer buffer)

    public static ByteString unsafeWrap(ByteBuffer buffer)

An unsafe operation that returns a [ByteString](https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.ByteString) that is backed by the provided buffer.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `buffer` | `https://docs.oracle.com/javase/8/docs/api/java/nio/ByteBuffer.html` the Java NIO buffer to be wrapped |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.ByteString` | a [ByteString](https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.ByteString) backed by the provided buffer |

### unsafeWriteTo(ByteString bytes, ByteOutput output)

    public static void unsafeWriteTo(ByteString bytes, ByteOutput output)

Writes the given [ByteString](https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.ByteString) to the provided [ByteOutput](https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.ByteOutput). Calling this method may
result in multiple operations on the target [ByteOutput](https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.ByteOutput) (i.e. for roped [ByteString](https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.ByteString)s).

This method exposes the internal backing buffer(s) of the [ByteString](https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.ByteString) to the [ByteOutput](https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.ByteOutput) in order to avoid additional copying overhead. It would be possible for a malicious
[ByteOutput](https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.ByteOutput) to corrupt the [ByteString](https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.ByteString). Use with caution!

NOTE: The [ByteOutput](https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.ByteOutput) **MUST NOT** modify the provided buffers. Doing so
may result in corrupted data, which would be difficult to debug.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `bytes` | `https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.ByteString` the [ByteString](https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.ByteString) to be written |
| `output` | `https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.ByteOutput` the output to receive the bytes |

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/io/IOException.html` | if an I/O error occurs |