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 org.apache.hadoop.hbase; 019 020import java.nio.ByteBuffer; 021import org.apache.hadoop.hbase.util.ByteBufferUtils; 022import org.apache.yetus.audience.InterfaceAudience; 023import org.apache.yetus.audience.InterfaceStability; 024 025/** 026 * This is a {@link Tag} implementation in which value is backed by {@link java.nio.ByteBuffer} 027 */ 028@InterfaceAudience.Private 029@InterfaceStability.Evolving 030public class ByteBufferTag implements Tag { 031 032 private ByteBuffer buffer; 033 private int offset, length; 034 private byte type; 035 036 public ByteBufferTag(ByteBuffer buffer, int offset, int length) { 037 this.buffer = buffer; 038 this.offset = offset; 039 this.length = length; 040 this.type = ByteBufferUtils.toByte(buffer, offset + TAG_LENGTH_SIZE); 041 } 042 043 @Override 044 public byte getType() { 045 return this.type; 046 } 047 048 @Override 049 public int getValueOffset() { 050 return this.offset + INFRASTRUCTURE_SIZE; 051 } 052 053 @Override 054 public int getValueLength() { 055 return this.length - INFRASTRUCTURE_SIZE; 056 } 057 058 @Override 059 public boolean hasArray() { 060 return false; 061 } 062 063 @Override 064 public byte[] getValueArray() { 065 throw new UnsupportedOperationException( 066 "Tag is backed by an off heap buffer. Use getValueByteBuffer()"); 067 } 068 069 @Override 070 public ByteBuffer getValueByteBuffer() { 071 return this.buffer; 072 } 073 074 @Override 075 public String toString() { 076 return "[Tag type : " + this.type + ", value : " 077 + ByteBufferUtils.toStringBinary(buffer, getValueOffset(), getValueLength()) + "]"; 078 } 079}