Protocol buffers: get byte array from ByteString without copying

Suppose I have a void foo(byte[] bytes) method that requires a byte array as a parameter. Howerver, the Java type for byte arrays in Protobuf is ByteString .

I can use byte[] toByteArray() to get an array of bytes. But the problem is that this method creates a new array using a copy, which is relatively expensive. I would prefer it to return the underlying array directly, or instead return a view.

Is there any API for this, or is performance limiting acceptable?

+5
source share
2 answers

In general, this is impossible, because such an array cannot exist inside some ByteString subclasses. BoundedByteString may contain a larger array, so copying is required to get an array of the right size. RopeByteString consists of other byte strings, so copying is required to put the contents in one array. LiteralByteString stores its contents in an array of the required size, but does not provide methods for directly accessing it.

But most likely, toByteArray() will be fast enough for your needs, since System.arraycopy() is very fast.

If you really have a performance problem due to copying large arrays and you do not want to pass ByteString directly, see the asReadOnlyByteBuffer() and asReadOnlyByteBufferList() methods. They complete the ByteString contents in the ByteBuffer without copying.

+4
source

You cannot get byte[] from ByteString without copying, as this will allow you to modify the contents of ByteString , which means that ByteString can no longer guarantee that it is immutable. This is very similar to why you cannot get char[] from String without copying, although String is actually supported by char[] under the hood. This can be a security issue: when transferring a String or ByteString between security domains, it is important that the recipient be aware that the sender does not modify the string from under them while they use it.

You can, however, call asReadOnlyByteBufferList() to get a ByteBuffer representing the underlying data without a copy, since ByteBuffer provides immutability.

+2
source

All Articles