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.InputStreamReader;
022import java.io.InputStream;
023import java.io.IOException;
024import java.io.PushbackReader;
025import java.io.UnsupportedEncodingException;
026
027import org.apache.hadoop.classification.InterfaceAudience;
028import org.apache.hadoop.classification.InterfaceStability;
029
030/**
031 * @deprecated Replaced by <a href="http://hadoop.apache.org/avro/">Avro</a>.
032 */
033@Deprecated
034@InterfaceAudience.Public
035@InterfaceStability.Stable
036public class CsvRecordInput implements RecordInput {
037    
038  private PushbackReader stream;
039    
040  private class CsvIndex implements Index {
041    public boolean done() {
042      char c = '\0';
043      try {
044        c = (char) stream.read();
045        stream.unread(c);
046      } catch (IOException ex) {
047      }
048      return (c == '}') ? true : false;
049    }
050    public void incr() {}
051  }
052    
053  private void throwExceptionOnError(String tag) throws IOException {
054    throw new IOException("Error deserializing "+tag);
055  }
056    
057  private String readField(String tag) throws IOException {
058    try {
059      StringBuilder buf = new StringBuilder();
060      while (true) {
061        char c = (char) stream.read();
062        switch (c) {
063        case ',':
064          return buf.toString();
065        case '}':
066        case '\n':
067        case '\r':
068          stream.unread(c);
069          return buf.toString();
070        default:
071          buf.append(c);
072        }
073      }
074    } catch (IOException ex) {
075      throw new IOException("Error reading "+tag);
076    }
077  }
078    
079  /** Creates a new instance of CsvRecordInput */
080  public CsvRecordInput(InputStream in) {
081    try {
082      stream = new PushbackReader(new InputStreamReader(in, "UTF-8"));
083    } catch (UnsupportedEncodingException ex) {
084      throw new RuntimeException(ex);
085    }
086  }
087    
088  public byte readByte(String tag) throws IOException {
089    return (byte) readLong(tag);
090  }
091    
092  public boolean readBool(String tag) throws IOException {
093    String sval = readField(tag);
094    return "T".equals(sval) ? true : false;
095  }
096    
097  public int readInt(String tag) throws IOException {
098    return (int) readLong(tag);
099  }
100    
101  public long readLong(String tag) throws IOException {
102    String sval = readField(tag);
103    try {
104      long lval = Long.parseLong(sval);
105      return lval;
106    } catch (NumberFormatException ex) {
107      throw new IOException("Error deserializing "+tag);
108    }
109  }
110    
111  public float readFloat(String tag) throws IOException {
112    return (float) readDouble(tag);
113  }
114    
115  public double readDouble(String tag) throws IOException {
116    String sval = readField(tag);
117    try {
118      double dval = Double.parseDouble(sval);
119      return dval;
120    } catch (NumberFormatException ex) {
121      throw new IOException("Error deserializing "+tag);
122    }
123  }
124    
125  public String readString(String tag) throws IOException {
126    String sval = readField(tag);
127    return Utils.fromCSVString(sval);
128  }
129    
130  public Buffer readBuffer(String tag) throws IOException {
131    String sval = readField(tag);
132    return Utils.fromCSVBuffer(sval);
133  }
134    
135  public void startRecord(String tag) throws IOException {
136    if (tag != null && !"".equals(tag)) {
137      char c1 = (char) stream.read();
138      char c2 = (char) stream.read();
139      if (c1 != 's' || c2 != '{') {
140        throw new IOException("Error deserializing "+tag);
141      }
142    }
143  }
144    
145  public void endRecord(String tag) throws IOException {
146    char c = (char) stream.read();
147    if (tag == null || "".equals(tag)) {
148      if (c != '\n' && c != '\r') {
149        throw new IOException("Error deserializing record.");
150      } else {
151        return;
152      }
153    }
154        
155    if (c != '}') {
156      throw new IOException("Error deserializing "+tag);
157    }
158    c = (char) stream.read();
159    if (c != ',') {
160      stream.unread(c);
161    }
162        
163    return;
164  }
165    
166  public Index startVector(String tag) throws IOException {
167    char c1 = (char) stream.read();
168    char c2 = (char) stream.read();
169    if (c1 != 'v' || c2 != '{') {
170      throw new IOException("Error deserializing "+tag);
171    }
172    return new CsvIndex();
173  }
174    
175  public void endVector(String tag) throws IOException {
176    char c = (char) stream.read();
177    if (c != '}') {
178      throw new IOException("Error deserializing "+tag);
179    }
180    c = (char) stream.read();
181    if (c != ',') {
182      stream.unread(c);
183    }
184    return;
185  }
186    
187  public Index startMap(String tag) throws IOException {
188    char c1 = (char) stream.read();
189    char c2 = (char) stream.read();
190    if (c1 != 'm' || c2 != '{') {
191      throw new IOException("Error deserializing "+tag);
192    }
193    return new CsvIndex();
194  }
195    
196  public void endMap(String tag) throws IOException {
197    char c = (char) stream.read();
198    if (c != '}') {
199      throw new IOException("Error deserializing "+tag);
200    }
201    c = (char) stream.read();
202    if (c != ',') {
203      stream.unread(c);
204    }
205    return;
206  }
207}