2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package com
.google
.protobuf
; // This is a lie.
20 import org
.apache
.yetus
.audience
.InterfaceAudience
;
23 * Helper class to extract byte arrays from {@link ByteString} without copy.
25 * Without this protobufs would force us to copy every single byte array out
26 * of the objects de-serialized from the wire (which already do one copy, on
27 * top of the copies the JVM does to go from kernel buffer to C buffer and
28 * from C buffer to JVM buffer).
32 @InterfaceAudience.Private
33 public final class HBaseZeroCopyByteString
extends LiteralByteString
{
34 // Gotten from AsyncHBase code base with permission.
35 /** Private constructor so this class cannot be instantiated. */
36 private HBaseZeroCopyByteString() {
38 throw new UnsupportedOperationException("Should never be here.");
42 * Wraps a byte array in a {@link ByteString} without copying it.
43 * @param array array to be wrapped
44 * @return wrapped array
46 public static ByteString
wrap(final byte[] array
) {
47 return new LiteralByteString(array
);
51 * Wraps a subset of a byte array in a {@link ByteString} without copying it.
52 * @param array array to be wrapped
54 * @param length length
55 * @return wrapped array
57 public static ByteString
wrap(final byte[] array
, int offset
, int length
) {
58 return new BoundedByteString(array
, offset
, length
);
62 // ZeroCopyLiteralByteString.wrap(this.buf, 0, this.count);
65 * Extracts the byte array from the given {@link ByteString} without copy.
66 * @param buf A buffer from which to extract the array. This buffer must be
67 * actually an instance of a {@code LiteralByteString}.
68 * @return byte[] representation
70 public static byte[] zeroCopyGetBytes(final ByteString buf
) {
71 if (buf
instanceof LiteralByteString
) {
72 return ((LiteralByteString
) buf
).bytes
;
74 throw new UnsupportedOperationException("Need a LiteralByteString, got a "
75 + buf
.getClass().getName());