1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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);
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
98
99
100
101
102 @Nonnull
103 protected abstract Cell parseCell() throws IOException;
104
105 @Override
106 public Cell current() {
107 return this.current;
108 }
109 }