View Javadoc

1   /**
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
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
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.
17   */
18  package org.apache.hadoop.hbase.types;
19  
20  import org.apache.hadoop.hbase.classification.InterfaceAudience;
21  import org.apache.hadoop.hbase.classification.InterfaceStability;
22  import org.apache.hadoop.hbase.util.Bytes;
23  import org.apache.hadoop.hbase.util.Order;
24  import org.apache.hadoop.hbase.util.PositionedByteRange;
25  
26  /**
27   * An {@code DataType} for interacting with values encoded using
28   * {@link Bytes#putByte(byte[], int, byte)}. Intended to make it easier to
29   * transition away from direct use of {@link Bytes}.
30   * @see Bytes#putByte(byte[], int, byte)
31   */
32  @InterfaceAudience.Public
33  @InterfaceStability.Evolving
34  public class RawByte implements DataType<Byte> {
35  
36    @Override
37    public boolean isOrderPreserving() { return false; }
38  
39    @Override
40    public Order getOrder() { return null; }
41  
42    @Override
43    public boolean isNullable() { return false; }
44  
45    @Override
46    public boolean isSkippable() { return true; }
47  
48    @Override
49    public int encodedLength(Byte val) { return Bytes.SIZEOF_BYTE; }
50  
51    @Override
52    public Class<Byte> encodedClass() { return Byte.class; }
53  
54    @Override
55    public int skip(PositionedByteRange src) {
56      src.setPosition(src.getPosition() + Bytes.SIZEOF_BYTE);
57      return Bytes.SIZEOF_BYTE;
58    }
59  
60    @Override
61    public Byte decode(PositionedByteRange src) {
62      byte val = src.getBytes()[src.getOffset() + src.getPosition()];
63      skip(src);
64      return val;
65    }
66  
67    @Override
68    public int encode(PositionedByteRange dst, Byte val) {
69      Bytes.putByte(dst.getBytes(), dst.getOffset() + dst.getPosition(), val);
70      return skip(dst);
71    }
72  
73    /**
74     * Read a {@code byte} value from the buffer {@code buff}.
75     */
76    public byte decodeByte(byte[] buff, int offset) {
77      return buff[offset];
78    }
79  
80    /**
81     * Write instance {@code val} into buffer {@code buff}.
82     */
83    public int encodeByte(byte[] buff, int offset, byte val) {
84      return Bytes.putByte(buff, offset, val);
85    }
86  }