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