001/**
002 * Copyright The Apache Software Foundation
003 *
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *     http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 */
020package org.apache.hadoop.hbase;
021
022import java.nio.ByteBuffer;
023
024import org.apache.hadoop.hbase.util.ByteBufferUtils;
025import org.apache.yetus.audience.InterfaceAudience;
026import org.apache.yetus.audience.InterfaceStability;
027
028/**
029 * This is a {@link Tag} implementation in which value is backed by
030 * {@link java.nio.ByteBuffer}
031 */
032@InterfaceAudience.Private
033@InterfaceStability.Evolving
034public class ByteBufferTag implements Tag {
035
036  private ByteBuffer buffer;
037  private int offset, length;
038  private byte type;
039
040  public ByteBufferTag(ByteBuffer buffer, int offset, int length) {
041    this.buffer = buffer;
042    this.offset = offset;
043    this.length = length;
044    this.type = ByteBufferUtils.toByte(buffer, offset + TAG_LENGTH_SIZE);
045  }
046
047  @Override
048  public byte getType() {
049    return this.type;
050  }
051
052  @Override
053  public int getValueOffset() {
054    return this.offset + INFRASTRUCTURE_SIZE;
055  }
056
057  @Override
058  public int getValueLength() {
059    return this.length - INFRASTRUCTURE_SIZE;
060  }
061
062  @Override
063  public boolean hasArray() {
064    return false;
065  }
066
067  @Override
068  public byte[] getValueArray() {
069    throw new UnsupportedOperationException(
070        "Tag is backed by an off heap buffer. Use getValueByteBuffer()");
071  }
072
073  @Override
074  public ByteBuffer getValueByteBuffer() {
075    return this.buffer;
076  }
077
078  @Override
079  public String toString() {
080    return "[Tag type : " + this.type + ", value : "
081        + ByteBufferUtils.toStringBinary(buffer, getValueOffset(), getValueLength()) + "]";
082  }
083}