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.types;
019
020import org.apache.hadoop.hbase.util.Bytes;
021import org.apache.hadoop.hbase.util.Order;
022import org.apache.hadoop.hbase.util.PositionedByteRange;
023import org.apache.yetus.audience.InterfaceAudience;
024
025/**
026 * An {@code DataType} for interacting with values encoded using
027 * {@link Bytes#putByte(byte[], int, byte)}. Intended to make it easier to transition away from
028 * direct use of {@link Bytes}.
029 * @see Bytes#putByte(byte[], int, byte)
030 */
031@InterfaceAudience.Public
032public class RawByte implements DataType<Byte> {
033
034  @Override
035  public boolean isOrderPreserving() {
036    return false;
037  }
038
039  @Override
040  public Order getOrder() {
041    return null;
042  }
043
044  @Override
045  public boolean isNullable() {
046    return false;
047  }
048
049  @Override
050  public boolean isSkippable() {
051    return true;
052  }
053
054  @Override
055  public int encodedLength(Byte val) {
056    return Bytes.SIZEOF_BYTE;
057  }
058
059  @Override
060  public Class<Byte> encodedClass() {
061    return Byte.class;
062  }
063
064  @Override
065  public int skip(PositionedByteRange src) {
066    src.setPosition(src.getPosition() + Bytes.SIZEOF_BYTE);
067    return Bytes.SIZEOF_BYTE;
068  }
069
070  @Override
071  public Byte decode(PositionedByteRange src) {
072    byte val = src.getBytes()[src.getOffset() + src.getPosition()];
073    skip(src);
074    return val;
075  }
076
077  @Override
078  public int encode(PositionedByteRange dst, Byte val) {
079    Bytes.putByte(dst.getBytes(), dst.getOffset() + dst.getPosition(), val);
080    return skip(dst);
081  }
082
083  /**
084   * Read a {@code byte} value from the buffer {@code buff}.
085   */
086  public byte decodeByte(byte[] buff, int offset) {
087    return buff[offset];
088  }
089
090  /**
091   * Write instance {@code val} into buffer {@code buff}.
092   */
093  public int encodeByte(byte[] buff, int offset, byte val) {
094    return Bytes.putByte(buff, offset, val);
095  }
096}