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
019package org.apache.hadoop.record;
020
021import java.io.DataInput;
022import java.io.IOException;
023import java.io.DataInputStream;
024import java.io.InputStream;
025
026import org.apache.hadoop.classification.InterfaceAudience;
027import org.apache.hadoop.classification.InterfaceStability;
028
029/**
030 * @deprecated Replaced by <a href="https://hadoop.apache.org/avro/">Avro</a>.
031 */
032@Deprecated
033@InterfaceAudience.Public
034@InterfaceStability.Stable
035public 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    @Override
045    public boolean done() {
046      return (nelems <= 0);
047    }
048    @Override
049    public void incr() {
050      nelems--;
051    }
052  }
053    
054  private BinaryRecordInput() {}
055    
056  private void setDataInput(DataInput inp) {
057    this.in = inp;
058  }
059    
060  private static ThreadLocal bIn = new ThreadLocal() {
061      @Override
062      protected synchronized Object initialValue() {
063        return new BinaryRecordInput();
064      }
065    };
066    
067  /**
068   * Get a thread-local record input for the supplied DataInput.
069   * @param inp data input stream
070   * @return binary record input corresponding to the supplied DataInput.
071   */
072  public static BinaryRecordInput get(DataInput inp) {
073    BinaryRecordInput bin = (BinaryRecordInput) bIn.get();
074    bin.setDataInput(inp);
075    return bin;
076  }
077    
078  /** Creates a new instance of BinaryRecordInput */
079  public BinaryRecordInput(InputStream strm) {
080    this.in = new DataInputStream(strm);
081  }
082    
083  /** Creates a new instance of BinaryRecordInput */
084  public BinaryRecordInput(DataInput din) {
085    this.in = din;
086  }
087    
088  @Override
089  public byte readByte(final String tag) throws IOException {
090    return in.readByte();
091  }
092    
093  @Override
094  public boolean readBool(final String tag) throws IOException {
095    return in.readBoolean();
096  }
097    
098  @Override
099  public int readInt(final String tag) throws IOException {
100    return Utils.readVInt(in);
101  }
102    
103  @Override
104  public long readLong(final String tag) throws IOException {
105    return Utils.readVLong(in);
106  }
107    
108  @Override
109  public float readFloat(final String tag) throws IOException {
110    return in.readFloat();
111  }
112    
113  @Override
114  public double readDouble(final String tag) throws IOException {
115    return in.readDouble();
116  }
117    
118  @Override
119  public String readString(final String tag) throws IOException {
120    return Utils.fromBinaryString(in);
121  }
122    
123  @Override
124  public Buffer readBuffer(final String tag) throws IOException {
125    final int len = Utils.readVInt(in);
126    final byte[] barr = new byte[len];
127    in.readFully(barr);
128    return new Buffer(barr);
129  }
130    
131  @Override
132  public void startRecord(final String tag) throws IOException {
133    // no-op
134  }
135    
136  @Override
137  public void endRecord(final String tag) throws IOException {
138    // no-op
139  }
140    
141  @Override
142  public Index startVector(final String tag) throws IOException {
143    return new BinaryIndex(readInt(tag));
144  }
145    
146  @Override
147  public void endVector(final String tag) throws IOException {
148    // no-op
149  }
150    
151  @Override
152  public Index startMap(final String tag) throws IOException {
153    return new BinaryIndex(readInt(tag));
154  }
155    
156  @Override
157  public void endMap(final String tag) throws IOException {
158    // no-op
159  }
160}