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 19 package org.apache.hadoop.record; 20 21 import java.io.DataInput; 22 import java.io.IOException; 23 import java.io.DataInputStream; 24 import java.io.InputStream; 25 26 /** 27 */ 28 public class BinaryRecordInput implements RecordInput { 29 30 private DataInput in; 31 32 static private class BinaryIndex implements Index { 33 private int nelems; BinaryIndex(int nelems)34 private BinaryIndex(int nelems) { 35 this.nelems = nelems; 36 } done()37 public boolean done() { 38 return (nelems <= 0); 39 } incr()40 public void incr() { 41 nelems--; 42 } 43 } 44 BinaryRecordInput()45 private BinaryRecordInput() {} 46 setDataInput(DataInput inp)47 private void setDataInput(DataInput inp) { 48 this.in = inp; 49 } 50 51 private static ThreadLocal bIn = new ThreadLocal() { 52 protected synchronized Object initialValue() { 53 return new BinaryRecordInput(); 54 } 55 }; 56 57 /** 58 * Get a thread-local record input for the supplied DataInput. 59 * @param inp data input stream 60 * @return binary record input corresponding to the supplied DataInput. 61 */ get(DataInput inp)62 public static BinaryRecordInput get(DataInput inp) { 63 BinaryRecordInput bin = (BinaryRecordInput) bIn.get(); 64 bin.setDataInput(inp); 65 return bin; 66 } 67 68 /** Creates a new instance of BinaryRecordInput */ BinaryRecordInput(InputStream strm)69 public BinaryRecordInput(InputStream strm) { 70 this.in = new DataInputStream(strm); 71 } 72 73 /** Creates a new instance of BinaryRecordInput */ BinaryRecordInput(DataInput din)74 public BinaryRecordInput(DataInput din) { 75 this.in = din; 76 } 77 readByte(final String tag)78 public byte readByte(final String tag) throws IOException { 79 return in.readByte(); 80 } 81 readBool(final String tag)82 public boolean readBool(final String tag) throws IOException { 83 return in.readBoolean(); 84 } 85 readInt(final String tag)86 public int readInt(final String tag) throws IOException { 87 return Utils.readVInt(in); 88 } 89 readLong(final String tag)90 public long readLong(final String tag) throws IOException { 91 return Utils.readVLong(in); 92 } 93 readFloat(final String tag)94 public float readFloat(final String tag) throws IOException { 95 return in.readFloat(); 96 } 97 readDouble(final String tag)98 public double readDouble(final String tag) throws IOException { 99 return in.readDouble(); 100 } 101 readString(final String tag)102 public String readString(final String tag) throws IOException { 103 return Utils.fromBinaryString(in); 104 } 105 readBuffer(final String tag)106 public Buffer readBuffer(final String tag) throws IOException { 107 final int len = Utils.readVInt(in); 108 final byte[] barr = new byte[len]; 109 in.readFully(barr); 110 return new Buffer(barr); 111 } 112 startRecord(final String tag)113 public void startRecord(final String tag) throws IOException { 114 // no-op 115 } 116 endRecord(final String tag)117 public void endRecord(final String tag) throws IOException { 118 // no-op 119 } 120 startVector(final String tag)121 public Index startVector(final String tag) throws IOException { 122 return new BinaryIndex(readInt(tag)); 123 } 124 endVector(final String tag)125 public void endVector(final String tag) throws IOException { 126 // no-op 127 } 128 startMap(final String tag)129 public Index startMap(final String tag) throws IOException { 130 return new BinaryIndex(readInt(tag)); 131 } 132 endMap(final String tag)133 public void endMap(final String tag) throws IOException { 134 // no-op 135 } 136 } 137