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.IOException;
022import java.util.TreeMap;
023import java.util.ArrayList;
024import java.io.DataOutput;
025import java.io.DataOutputStream;
026import java.io.OutputStream;
027
028import org.apache.hadoop.classification.InterfaceAudience;
029import org.apache.hadoop.classification.InterfaceStability;
030
031/**
032 * @deprecated Replaced by <a href="https://hadoop.apache.org/avro/">Avro</a>.
033 */
034@Deprecated
035@InterfaceAudience.Public
036@InterfaceStability.Stable
037public class BinaryRecordOutput implements RecordOutput {
038    
039  private DataOutput out;
040    
041  private BinaryRecordOutput() {}
042    
043  private void setDataOutput(DataOutput out) {
044    this.out = out;
045  }
046    
047  private static final ThreadLocal<BinaryRecordOutput> B_OUT =
048      new ThreadLocal<BinaryRecordOutput>() {
049    @Override
050    protected BinaryRecordOutput initialValue() {
051      return new BinaryRecordOutput();
052    }
053  };
054
055  /**
056   * Get a thread-local record output for the supplied DataOutput.
057   * @param out data output stream
058   * @return binary record output corresponding to the supplied DataOutput.
059   */
060  public static BinaryRecordOutput get(DataOutput out) {
061    BinaryRecordOutput bout = B_OUT.get();
062    bout.setDataOutput(out);
063    return bout;
064  }
065    
066  /** Creates a new instance of BinaryRecordOutput */
067  public BinaryRecordOutput(OutputStream out) {
068    this.out = new DataOutputStream(out);
069  }
070    
071  /** Creates a new instance of BinaryRecordOutput */
072  public BinaryRecordOutput(DataOutput out) {
073    this.out = out;
074  }
075    
076    
077  @Override
078  public void writeByte(byte b, String tag) throws IOException {
079    out.writeByte(b);
080  }
081    
082  @Override
083  public void writeBool(boolean b, String tag) throws IOException {
084    out.writeBoolean(b);
085  }
086    
087  @Override
088  public void writeInt(int i, String tag) throws IOException {
089    Utils.writeVInt(out, i);
090  }
091    
092  @Override
093  public void writeLong(long l, String tag) throws IOException {
094    Utils.writeVLong(out, l);
095  }
096    
097  @Override
098  public void writeFloat(float f, String tag) throws IOException {
099    out.writeFloat(f);
100  }
101    
102  @Override
103  public void writeDouble(double d, String tag) throws IOException {
104    out.writeDouble(d);
105  }
106    
107  @Override
108  public void writeString(String s, String tag) throws IOException {
109    Utils.toBinaryString(out, s);
110  }
111    
112  @Override
113  public void writeBuffer(Buffer buf, String tag)
114    throws IOException {
115    byte[] barr = buf.get();
116    int len = buf.getCount();
117    Utils.writeVInt(out, len);
118    out.write(barr, 0, len);
119  }
120    
121  @Override
122  public void startRecord(Record r, String tag) throws IOException {}
123    
124  @Override
125  public void endRecord(Record r, String tag) throws IOException {}
126    
127  @Override
128  public void startVector(ArrayList v, String tag) throws IOException {
129    writeInt(v.size(), tag);
130  }
131    
132  @Override
133  public void endVector(ArrayList v, String tag) throws IOException {}
134    
135  @Override
136  public void startMap(TreeMap v, String tag) throws IOException {
137    writeInt(v.size(), tag);
138  }
139    
140  @Override
141  public void endMap(TreeMap v, String tag) throws IOException {}
142    
143}