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.Order;
23  import org.apache.hadoop.hbase.util.PositionedByteRange;
24  
25  /**
26   * An {@code DataType} that encodes variable-length values encoded using
27   * {@link org.apache.hadoop.hbase.util.Bytes#putBytes(byte[], int, byte[], int, int)}. 
28   * Includes a termination marker following the raw {@code byte[]} value. Intended to
29   * make it easier to transition away from direct use of 
30   * {@link org.apache.hadoop.hbase.io.ImmutableBytesWritable}.
31   * @see org.apache.hadoop.hbase.util.Bytes#putBytes(byte[], int, byte[], int, int)
32   * @see RawBytes
33   * @see OrderedBlob
34   */
35  @InterfaceAudience.Public
36  @InterfaceStability.Evolving
37  public class RawBytesTerminated extends TerminatedWrapper<byte[]> {
38  
39    /**
40     * Create a {@code RawBytesTerminated} using the specified terminator and
41     * {@code order}.
42     * @throws IllegalArgumentException if {@code term} is {@code null} or empty.
43     */
44    public RawBytesTerminated(Order order, byte[] term) {
45      super(new RawBytes(order), term);
46    }
47  
48    /**
49     * Create a {@code RawBytesTerminated} using the specified terminator and
50     * {@code order}.
51     * @throws IllegalArgumentException if {@code term} is {@code null} or empty.
52     */
53    public RawBytesTerminated(Order order, String term) {
54      super(new RawBytes(order), term);
55    }
56  
57    /**
58     * Create a {@code RawBytesTerminated} using the specified terminator.
59     * @throws IllegalArgumentException if {@code term} is {@code null} or empty.
60     */
61    public RawBytesTerminated(byte[] term) {
62      super(new RawBytes(), term);
63    }
64  
65    /**
66     * Create a {@code RawBytesTerminated} using the specified terminator.
67     * @throws IllegalArgumentException if {@code term} is {@code null} or empty.
68     */
69    public RawBytesTerminated(String term) {
70      super(new RawBytes(), term);
71    }
72  
73    /**
74     * Read a {@code byte[]} from the buffer {@code src}.
75     */
76    public byte[] decode(PositionedByteRange src, int length) {
77      return ((RawBytes) wrapped).decode(src, length);
78    }
79  
80    /**
81     * Write {@code val} into {@code dst}, respecting {@code offset} and
82     * {@code length}.
83     * @return number of bytes written.
84     */
85    public int encode(PositionedByteRange dst, byte[] val, int voff, int vlen) {
86      return ((RawBytes) wrapped).encode(dst, val, voff, vlen);
87    }
88  }