[120] | 1 | /** |
---|
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
| 3 | * or more contributor license agreements. See the NOTICE file |
---|
| 4 | * distributed with this work for additional information |
---|
| 5 | * regarding copyright ownership. The ASF licenses this file |
---|
| 6 | * to you under the Apache License, Version 2.0 (the |
---|
| 7 | * "License"); you may not use this file except in compliance |
---|
| 8 | * with the License. You may obtain a copy of the License at |
---|
| 9 | * |
---|
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 11 | * |
---|
| 12 | * Unless required by applicable law or agreed to in writing, software |
---|
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 15 | * See the License for the specific language governing permissions and |
---|
| 16 | * limitations under the License. |
---|
| 17 | */ |
---|
| 18 | |
---|
| 19 | package org.apache.hadoop.record; |
---|
| 20 | |
---|
| 21 | import java.io.IOException; |
---|
| 22 | import java.util.TreeMap; |
---|
| 23 | import java.util.ArrayList; |
---|
| 24 | import java.io.DataOutput; |
---|
| 25 | import java.io.DataOutputStream; |
---|
| 26 | import java.io.OutputStream; |
---|
| 27 | |
---|
| 28 | /** |
---|
| 29 | */ |
---|
| 30 | public class BinaryRecordOutput implements RecordOutput { |
---|
| 31 | |
---|
| 32 | private DataOutput out; |
---|
| 33 | |
---|
| 34 | private BinaryRecordOutput() {} |
---|
| 35 | |
---|
| 36 | private void setDataOutput(DataOutput out) { |
---|
| 37 | this.out = out; |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | private static ThreadLocal bOut = new ThreadLocal() { |
---|
| 41 | protected synchronized Object initialValue() { |
---|
| 42 | return new BinaryRecordOutput(); |
---|
| 43 | } |
---|
| 44 | }; |
---|
| 45 | |
---|
| 46 | /** |
---|
| 47 | * Get a thread-local record output for the supplied DataOutput. |
---|
| 48 | * @param out data output stream |
---|
| 49 | * @return binary record output corresponding to the supplied DataOutput. |
---|
| 50 | */ |
---|
| 51 | public static BinaryRecordOutput get(DataOutput out) { |
---|
| 52 | BinaryRecordOutput bout = (BinaryRecordOutput) bOut.get(); |
---|
| 53 | bout.setDataOutput(out); |
---|
| 54 | return bout; |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | /** Creates a new instance of BinaryRecordOutput */ |
---|
| 58 | public BinaryRecordOutput(OutputStream out) { |
---|
| 59 | this.out = new DataOutputStream(out); |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | /** Creates a new instance of BinaryRecordOutput */ |
---|
| 63 | public BinaryRecordOutput(DataOutput out) { |
---|
| 64 | this.out = out; |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | |
---|
| 68 | public void writeByte(byte b, String tag) throws IOException { |
---|
| 69 | out.writeByte(b); |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | public void writeBool(boolean b, String tag) throws IOException { |
---|
| 73 | out.writeBoolean(b); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | public void writeInt(int i, String tag) throws IOException { |
---|
| 77 | Utils.writeVInt(out, i); |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | public void writeLong(long l, String tag) throws IOException { |
---|
| 81 | Utils.writeVLong(out, l); |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | public void writeFloat(float f, String tag) throws IOException { |
---|
| 85 | out.writeFloat(f); |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | public void writeDouble(double d, String tag) throws IOException { |
---|
| 89 | out.writeDouble(d); |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | public void writeString(String s, String tag) throws IOException { |
---|
| 93 | Utils.toBinaryString(out, s); |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | public void writeBuffer(Buffer buf, String tag) |
---|
| 97 | throws IOException { |
---|
| 98 | byte[] barr = buf.get(); |
---|
| 99 | int len = buf.getCount(); |
---|
| 100 | Utils.writeVInt(out, len); |
---|
| 101 | out.write(barr, 0, len); |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | public void startRecord(Record r, String tag) throws IOException {} |
---|
| 105 | |
---|
| 106 | public void endRecord(Record r, String tag) throws IOException {} |
---|
| 107 | |
---|
| 108 | public void startVector(ArrayList v, String tag) throws IOException { |
---|
| 109 | writeInt(v.size(), tag); |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | public void endVector(ArrayList v, String tag) throws IOException {} |
---|
| 113 | |
---|
| 114 | public void startMap(TreeMap v, String tag) throws IOException { |
---|
| 115 | writeInt(v.size(), tag); |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | public void endMap(TreeMap v, String tag) throws IOException {} |
---|
| 119 | |
---|
| 120 | } |
---|