001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package com.google.protobuf;  // This is a lie.
019
020import org.apache.yetus.audience.InterfaceAudience;
021
022/**
023 * Helper class to extract byte arrays from {@link ByteString} without copy.
024 * <p>
025 * Without this protobufs would force us to copy every single byte array out
026 * of the objects de-serialized from the wire (which already do one copy, on
027 * top of the copies the JVM does to go from kernel buffer to C buffer and
028 * from C buffer to JVM buffer).
029 *
030 * @since 0.96.1
031 */
032@InterfaceAudience.Private
033public final class HBaseZeroCopyByteString extends LiteralByteString {
034  // Gotten from AsyncHBase code base with permission.
035  /** Private constructor so this class cannot be instantiated. */
036  private HBaseZeroCopyByteString() {
037    super(null);
038    throw new UnsupportedOperationException("Should never be here.");
039  }
040
041  /**
042   * Wraps a byte array in a {@link ByteString} without copying it.
043   * @param array array to be wrapped
044   * @return wrapped array
045   */
046  public static ByteString wrap(final byte[] array) {
047    return new LiteralByteString(array);
048  }
049
050  /**
051   * Wraps a subset of a byte array in a {@link ByteString} without copying it.
052   * @param array array to be wrapped
053   * @param offset from
054   * @param length length
055   * @return wrapped array
056   */
057  public static ByteString wrap(final byte[] array, int offset, int length) {
058    return new BoundedByteString(array, offset, length);
059  }
060
061  // TODO:
062  // ZeroCopyLiteralByteString.wrap(this.buf, 0, this.count);
063
064  /**
065   * Extracts the byte array from the given {@link ByteString} without copy.
066   * @param buf A buffer from which to extract the array.  This buffer must be
067   * actually an instance of a {@code LiteralByteString}.
068   * @return byte[] representation
069   */
070  public static byte[] zeroCopyGetBytes(final ByteString buf) {
071    if (buf instanceof LiteralByteString) {
072      return ((LiteralByteString) buf).bytes;
073    }
074    throw new UnsupportedOperationException("Need a LiteralByteString, got a "
075                                            + buf.getClass().getName());
076  }
077}