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 final ThreadLocal<BinaryRecordInput> B_IN =
061      new ThreadLocal<BinaryRecordInput>() {
062      @Override
063      protected BinaryRecordInput initialValue() {
064        return new BinaryRecordInput();
065      }
066    };
067    
068  /**
069   * Get a thread-local record input for the supplied DataInput.
070   * @param inp data input stream
071   * @return binary record input corresponding to the supplied DataInput.
072   */
073  public static BinaryRecordInput get(DataInput inp) {
074    BinaryRecordInput bin = B_IN.get();
075    bin.setDataInput(inp);
076    return bin;
077  }
078    
079  /** Creates a new instance of BinaryRecordInput */
080  public BinaryRecordInput(InputStream strm) {
081    this.in = new DataInputStream(strm);
082  }
083    
084  /** Creates a new instance of BinaryRecordInput */
085  public BinaryRecordInput(DataInput din) {
086    this.in = din;
087  }
088    
089  @Override
090  public byte readByte(final String tag) throws IOException {
091    return in.readByte();
092  }
093    
094  @Override
095  public boolean readBool(final String tag) throws IOException {
096    return in.readBoolean();
097  }
098    
099  @Override
100  public int readInt(final String tag) throws IOException {
101    return Utils.readVInt(in);
102  }
103    
104  @Override
105  public long readLong(final String tag) throws IOException {
106    return Utils.readVLong(in);
107  }
108    
109  @Override
110  public float readFloat(final String tag) throws IOException {
111    return in.readFloat();
112  }
113    
114  @Override
115  public double readDouble(final String tag) throws IOException {
116    return in.readDouble();
117  }
118    
119  @Override
120  public String readString(final String tag) throws IOException {
121    return Utils.fromBinaryString(in);
122  }
123    
124  @Override
125  public Buffer readBuffer(final String tag) throws IOException {
126    final int len = Utils.readVInt(in);
127    final byte[] barr = new byte[len];
128    in.readFully(barr);
129    return new Buffer(barr);
130  }
131    
132  @Override
133  public void startRecord(final String tag) throws IOException {
134    // no-op
135  }
136    
137  @Override
138  public void endRecord(final String tag) throws IOException {
139    // no-op
140  }
141    
142  @Override
143  public Index startVector(final String tag) throws IOException {
144    return new BinaryIndex(readInt(tag));
145  }
146    
147  @Override
148  public void endVector(final String tag) throws IOException {
149    // no-op
150  }
151    
152  @Override
153  public Index startMap(final String tag) throws IOException {
154    return new BinaryIndex(readInt(tag));
155  }
156    
157  @Override
158  public void endMap(final String tag) throws IOException {
159    // no-op
160  }
161}