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.DataInput; |
---|
22 | import java.io.DataOutput; |
---|
23 | import java.io.ByteArrayOutputStream; |
---|
24 | import java.io.IOException; |
---|
25 | import org.apache.hadoop.io.WritableComparable; |
---|
26 | |
---|
27 | /** |
---|
28 | * Abstract class that is extended by generated classes. |
---|
29 | * |
---|
30 | */ |
---|
31 | public abstract class Record implements WritableComparable, Cloneable { |
---|
32 | |
---|
33 | /** |
---|
34 | * Serialize a record with tag (ususally field name) |
---|
35 | * @param rout Record output destination |
---|
36 | * @param tag record tag (Used only in tagged serialization e.g. XML) |
---|
37 | */ |
---|
38 | public abstract void serialize(RecordOutput rout, String tag) |
---|
39 | throws IOException; |
---|
40 | |
---|
41 | /** |
---|
42 | * Deserialize a record with a tag (usually field name) |
---|
43 | * @param rin Record input source |
---|
44 | * @param tag Record tag (Used only in tagged serialization e.g. XML) |
---|
45 | */ |
---|
46 | public abstract void deserialize(RecordInput rin, String tag) |
---|
47 | throws IOException; |
---|
48 | |
---|
49 | // inheric javadoc |
---|
50 | public abstract int compareTo (final Object peer) throws ClassCastException; |
---|
51 | |
---|
52 | /** |
---|
53 | * Serialize a record without a tag |
---|
54 | * @param rout Record output destination |
---|
55 | */ |
---|
56 | public void serialize(RecordOutput rout) throws IOException { |
---|
57 | this.serialize(rout, ""); |
---|
58 | } |
---|
59 | |
---|
60 | /** |
---|
61 | * Deserialize a record without a tag |
---|
62 | * @param rin Record input source |
---|
63 | */ |
---|
64 | public void deserialize(RecordInput rin) throws IOException { |
---|
65 | this.deserialize(rin, ""); |
---|
66 | } |
---|
67 | |
---|
68 | // inherit javadoc |
---|
69 | public void write(final DataOutput out) throws java.io.IOException { |
---|
70 | BinaryRecordOutput bout = BinaryRecordOutput.get(out); |
---|
71 | this.serialize(bout); |
---|
72 | } |
---|
73 | |
---|
74 | // inherit javadoc |
---|
75 | public void readFields(final DataInput din) throws java.io.IOException { |
---|
76 | BinaryRecordInput rin = BinaryRecordInput.get(din); |
---|
77 | this.deserialize(rin); |
---|
78 | } |
---|
79 | |
---|
80 | // inherit javadoc |
---|
81 | public String toString() { |
---|
82 | try { |
---|
83 | ByteArrayOutputStream s = new ByteArrayOutputStream(); |
---|
84 | CsvRecordOutput a = new CsvRecordOutput(s); |
---|
85 | this.serialize(a); |
---|
86 | return new String(s.toByteArray(), "UTF-8"); |
---|
87 | } catch (Throwable ex) { |
---|
88 | throw new RuntimeException(ex); |
---|
89 | } |
---|
90 | } |
---|
91 | } |
---|