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.PrintStream;
25 import java.io.OutputStream;
26 import java.io.UnsupportedEncodingException;
27 
28 /**
29  */
30 public class CsvRecordOutput implements RecordOutput {
31 
32   private PrintStream stream;
33   private boolean isFirst = true;
34 
throwExceptionOnError(String tag)35   private void throwExceptionOnError(String tag) throws IOException {
36     if (stream.checkError()) {
37       throw new IOException("Error serializing "+tag);
38     }
39   }
40 
printCommaUnlessFirst()41   private void printCommaUnlessFirst() {
42     if (!isFirst) {
43       stream.print(",");
44     }
45     isFirst = false;
46   }
47 
48   /** Creates a new instance of CsvRecordOutput */
CsvRecordOutput(OutputStream out)49   public CsvRecordOutput(OutputStream out) {
50     try {
51       stream = new PrintStream(out, true, "UTF-8");
52     } catch (UnsupportedEncodingException ex) {
53       throw new RuntimeException(ex);
54     }
55   }
56 
writeByte(byte b, String tag)57   public void writeByte(byte b, String tag) throws IOException {
58     writeLong((long)b, tag);
59   }
60 
writeBool(boolean b, String tag)61   public void writeBool(boolean b, String tag) throws IOException {
62     printCommaUnlessFirst();
63     String val = b ? "T" : "F";
64     stream.print(val);
65     throwExceptionOnError(tag);
66   }
67 
writeInt(int i, String tag)68   public void writeInt(int i, String tag) throws IOException {
69     writeLong((long)i, tag);
70   }
71 
writeLong(long l, String tag)72   public void writeLong(long l, String tag) throws IOException {
73     printCommaUnlessFirst();
74     stream.print(l);
75     throwExceptionOnError(tag);
76   }
77 
writeFloat(float f, String tag)78   public void writeFloat(float f, String tag) throws IOException {
79     writeDouble((double)f, tag);
80   }
81 
writeDouble(double d, String tag)82   public void writeDouble(double d, String tag) throws IOException {
83     printCommaUnlessFirst();
84     stream.print(d);
85     throwExceptionOnError(tag);
86   }
87 
writeString(String s, String tag)88   public void writeString(String s, String tag) throws IOException {
89     printCommaUnlessFirst();
90     stream.print(Utils.toCSVString(s));
91     throwExceptionOnError(tag);
92   }
93 
writeBuffer(Buffer buf, String tag)94   public void writeBuffer(Buffer buf, String tag)
95     throws IOException {
96     printCommaUnlessFirst();
97     stream.print(Utils.toCSVBuffer(buf));
98     throwExceptionOnError(tag);
99   }
100 
startRecord(Record r, String tag)101   public void startRecord(Record r, String tag) throws IOException {
102     if (tag != null && !"".equals(tag)) {
103       printCommaUnlessFirst();
104       stream.print("s{");
105       isFirst = true;
106     }
107   }
108 
endRecord(Record r, String tag)109   public void endRecord(Record r, String tag) throws IOException {
110     if (tag == null || "".equals(tag)) {
111       stream.print("\n");
112       isFirst = true;
113     } else {
114       stream.print("}");
115       isFirst = false;
116     }
117   }
118 
startVector(ArrayList v, String tag)119   public void startVector(ArrayList v, String tag) throws IOException {
120     printCommaUnlessFirst();
121     stream.print("v{");
122     isFirst = true;
123   }
124 
endVector(ArrayList v, String tag)125   public void endVector(ArrayList v, String tag) throws IOException {
126     stream.print("}");
127     isFirst = false;
128   }
129 
startMap(TreeMap v, String tag)130   public void startMap(TreeMap v, String tag) throws IOException {
131     printCommaUnlessFirst();
132     stream.print("m{");
133     isFirst = true;
134   }
135 
endMap(TreeMap v, String tag)136   public void endMap(TreeMap v, String tag) throws IOException {
137     stream.print("}");
138     isFirst = false;
139   }
140 }
141