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.EOFException;
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.io.PushbackInputStream;
24  
25  import javax.annotation.Nonnull;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.apache.hadoop.hbase.Cell;
30  import org.apache.hadoop.hbase.HBaseInterfaceAudience;
31  import org.apache.hadoop.hbase.classification.InterfaceAudience;
32  
33  /**
34   * TODO javadoc
35   */
36  @InterfaceAudience.LimitedPrivate({HBaseInterfaceAudience.COPROC, HBaseInterfaceAudience.PHOENIX})
37  public abstract class BaseDecoder implements Codec.Decoder {
38    protected static final Log LOG = LogFactory.getLog(BaseDecoder.class);
39  
40    protected final InputStream in;
41    private Cell current = null;
42  
43    protected static class PBIS extends PushbackInputStream {
44      public PBIS(InputStream in, int size) {
45        super(in, size);
46      }
47  
48      public void resetBuf(int size) {
49        this.buf = new byte[size];
50        this.pos = size;
51      }
52    }
53  
54    public BaseDecoder(final InputStream in) {
55      this.in = new PBIS(in, 1);
56    }
57  
58    @Override
59    public boolean advance() throws IOException {
60      int firstByte = in.read();
61      if (firstByte == -1) {
62        return false;
63      } else {
64        ((PBIS)in).unread(firstByte);
65      }
66  
67      try {
68        this.current = parseCell();
69      } catch (IOException ioEx) {
70        ((PBIS)in).resetBuf(1); // reset the buffer in case the underlying stream is read from upper layers
71        rethrowEofException(ioEx);
72      }
73      return true;
74    }
75  
76    private void rethrowEofException(IOException ioEx) throws IOException {
77      boolean isEof = false;
78      try {
79        isEof = this.in.available() == 0;
80      } catch (Throwable t) {
81        LOG.trace("Error getting available for error message - ignoring", t);
82      }
83      if (!isEof) throw ioEx;
84      if (LOG.isTraceEnabled()) {
85        LOG.trace("Partial cell read caused by EOF", ioEx);
86      }
87      EOFException eofEx = new EOFException("Partial cell read");
88      eofEx.initCause(ioEx);
89      throw eofEx;
90    }
91  
92    protected InputStream getInputStream() {
93      return in;
94    }
95  
96    /**
97     * Extract a Cell.
98     * @return a parsed Cell or throws an Exception. EOFException or a generic IOException maybe
99     * thrown if EOF is reached prematurely. Does not return null.
100    * @throws IOException
101    */
102   @Nonnull
103   protected abstract Cell parseCell() throws IOException;
104 
105   @Override
106   public Cell current() {
107     return this.current;
108   }
109 }