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.codec;
19  
20  import java.io.DataInputStream;
21  import java.io.DataOutputStream;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.io.OutputStream;
25  
26  import org.apache.hadoop.hbase.KeyValue;
27  
28  /**
29   * Codec that does KeyValue version 1 serialization.
30   * <p>Encodes by casting Cell to KeyValue and writing out the backing array with a length prefix.
31   * This is how KVs were serialized in Puts, Deletes and Results pre-0.96.  Its what would
32   * happen if you called the Writable#write KeyValue implementation.  This encoder will fail
33   * if the passed Cell is not an old-school pre-0.96 KeyValue.  Does not copy bytes writing.
34   * It just writes them direct to the passed stream.
35   * <p>If you wrote two KeyValues to this encoder, it would look like this in the stream:
36   * <pre>
37   * length-of-KeyValue1 // A java int with the length of KeyValue1 backing array
38   * KeyValue1 backing array filled with a KeyValue serialized in its particular format
39   * length-of-KeyValue2
40   * KeyValue2 backing array
41   * </pre>
42   */
43  public class KeyValueCodec implements Codec {
44    public static class KeyValueEncoder extends BaseEncoder {
45      public KeyValueEncoder(final DataOutputStream out) {
46        super(out);
47      }
48  
49      @Override
50      public void write(KeyValue kv) throws IOException {
51        checkFlushed();
52        kv.write((DataOutputStream) out);
53      }
54    }
55  
56    public static class KeyValueDecoder extends BaseDecoder{
57      public KeyValueDecoder(final DataInputStream in) {
58        super(in);
59      }
60  
61      @Override
62      protected KeyValue parseCell() throws IOException {
63        KeyValue kv = new KeyValue();
64        kv.readFields((DataInputStream) this.in);
65        return kv;
66      }
67    }
68  
69    /**
70     * Implementation depends on {@link InputStream#available()}
71     * <p>
72     * Must be passed a {@link DataInputStream} so KeyValues can be derserialized with the usual
73     * Writable mechanisms
74     */
75    @Override
76    public Decoder getDecoder(InputStream is) {
77      return new KeyValueDecoder((DataInputStream) is);
78    }
79  
80    /**
81     * Must be passed a {@link DataOutputStream} so KeyValues can be serialized using the usual
82     * Writable mechanisms
83     */
84    @Override
85    public Encoder getEncoder(OutputStream os) {
86      return new KeyValueEncoder((DataOutputStream) os);
87    }
88  }