HBASE-24163 MOB compactor implementations should use format specifiers when calling...
[hbase.git] / hbase-common / src / main / java / org / apache / hadoop / hbase / codec / BaseDecoder.java
blobe1a96bdcf484bf817f5bd691451f6e20ef56799a
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.apache.hadoop.hbase.codec;
20 import java.io.EOFException;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.PushbackInputStream;
25 import edu.umd.cs.findbugs.annotations.NonNull;
27 import org.apache.hadoop.hbase.Cell;
28 import org.apache.hadoop.hbase.HBaseInterfaceAudience;
29 import org.apache.yetus.audience.InterfaceAudience;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 /**
34 * TODO javadoc
36 @InterfaceAudience.LimitedPrivate({HBaseInterfaceAudience.COPROC, HBaseInterfaceAudience.PHOENIX})
37 public abstract class BaseDecoder implements Codec.Decoder {
38 protected static final Logger LOG = LoggerFactory.getLogger(BaseDecoder.class);
40 protected final InputStream in;
41 private Cell current = null;
43 protected static class PBIS extends PushbackInputStream {
44 public PBIS(InputStream in, int size) {
45 super(in, size);
48 public void resetBuf(int size) {
49 this.buf = new byte[size];
50 this.pos = size;
54 public BaseDecoder(final InputStream in) {
55 this.in = new PBIS(in, 1);
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);
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);
73 return true;
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);
83 if (!isEof) throw ioEx;
84 if (LOG.isTraceEnabled()) {
85 LOG.trace("Partial cell read caused by EOF", ioEx);
87 EOFException eofEx = new EOFException("Partial cell read");
88 eofEx.initCause(ioEx);
89 throw eofEx;
92 protected InputStream getInputStream() {
93 return in;
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
102 @NonNull
103 protected abstract Cell parseCell() throws IOException;
105 @Override
106 public Cell current() {
107 return this.current;