001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    
019    package org.apache.hadoop.record;
020    
021    import java.io.DataInput;
022    import java.io.IOException;
023    import java.io.DataInputStream;
024    import java.io.InputStream;
025    
026    import org.apache.hadoop.classification.InterfaceAudience;
027    import org.apache.hadoop.classification.InterfaceStability;
028    
029    /**
030     * @deprecated Replaced by <a href="http://hadoop.apache.org/avro/">Avro</a>.
031     */
032    @Deprecated
033    @InterfaceAudience.Public
034    @InterfaceStability.Stable
035    public class BinaryRecordInput implements RecordInput {
036        
037      private DataInput in;
038        
039      static private class BinaryIndex implements Index {
040        private int nelems;
041        private BinaryIndex(int nelems) {
042          this.nelems = nelems;
043        }
044        public boolean done() {
045          return (nelems <= 0);
046        }
047        public void incr() {
048          nelems--;
049        }
050      }
051        
052      private BinaryRecordInput() {}
053        
054      private void setDataInput(DataInput inp) {
055        this.in = inp;
056      }
057        
058      private static ThreadLocal bIn = new ThreadLocal() {
059          protected synchronized Object initialValue() {
060            return new BinaryRecordInput();
061          }
062        };
063        
064      /**
065       * Get a thread-local record input for the supplied DataInput.
066       * @param inp data input stream
067       * @return binary record input corresponding to the supplied DataInput.
068       */
069      public static BinaryRecordInput get(DataInput inp) {
070        BinaryRecordInput bin = (BinaryRecordInput) bIn.get();
071        bin.setDataInput(inp);
072        return bin;
073      }
074        
075      /** Creates a new instance of BinaryRecordInput */
076      public BinaryRecordInput(InputStream strm) {
077        this.in = new DataInputStream(strm);
078      }
079        
080      /** Creates a new instance of BinaryRecordInput */
081      public BinaryRecordInput(DataInput din) {
082        this.in = din;
083      }
084        
085      public byte readByte(final String tag) throws IOException {
086        return in.readByte();
087      }
088        
089      public boolean readBool(final String tag) throws IOException {
090        return in.readBoolean();
091      }
092        
093      public int readInt(final String tag) throws IOException {
094        return Utils.readVInt(in);
095      }
096        
097      public long readLong(final String tag) throws IOException {
098        return Utils.readVLong(in);
099      }
100        
101      public float readFloat(final String tag) throws IOException {
102        return in.readFloat();
103      }
104        
105      public double readDouble(final String tag) throws IOException {
106        return in.readDouble();
107      }
108        
109      public String readString(final String tag) throws IOException {
110        return Utils.fromBinaryString(in);
111      }
112        
113      public Buffer readBuffer(final String tag) throws IOException {
114        final int len = Utils.readVInt(in);
115        final byte[] barr = new byte[len];
116        in.readFully(barr);
117        return new Buffer(barr);
118      }
119        
120      public void startRecord(final String tag) throws IOException {
121        // no-op
122      }
123        
124      public void endRecord(final String tag) throws IOException {
125        // no-op
126      }
127        
128      public Index startVector(final String tag) throws IOException {
129        return new BinaryIndex(readInt(tag));
130      }
131        
132      public void endVector(final String tag) throws IOException {
133        // no-op
134      }
135        
136      public Index startMap(final String tag) throws IOException {
137        return new BinaryIndex(readInt(tag));
138      }
139        
140      public void endMap(final String tag) throws IOException {
141        // no-op
142      }
143    }