CsvInputArchive.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. package com.yahoo.jute;
  19. import java.io.InputStreamReader;
  20. import java.io.InputStream;
  21. import java.io.IOException;
  22. import java.io.ByteArrayOutputStream;
  23. import java.io.PushbackReader;
  24. import java.io.UnsupportedEncodingException;
  25. /**
  26. *
  27. * @author Milind Bhandarkar
  28. */
  29. class CsvInputArchive implements InputArchive {
  30. private PushbackReader stream;
  31. private class CsvIndex implements Index {
  32. public boolean done() {
  33. char c = '\0';
  34. try {
  35. c = (char) stream.read();
  36. stream.unread(c);
  37. } catch (IOException ex) {
  38. }
  39. return (c == '}') ? true : false;
  40. }
  41. public void incr() {}
  42. }
  43. private void throwExceptionOnError(String tag) throws IOException {
  44. throw new IOException("Error deserializing "+tag);
  45. }
  46. private String readField(String tag) throws IOException {
  47. try {
  48. StringBuffer buf = new StringBuffer();
  49. while (true) {
  50. char c = (char) stream.read();
  51. switch (c) {
  52. case ',':
  53. return buf.toString();
  54. case '}':
  55. case '\n':
  56. case '\r':
  57. stream.unread(c);
  58. return buf.toString();
  59. default:
  60. buf.append(c);
  61. }
  62. }
  63. } catch (IOException ex) {
  64. throw new IOException("Error reading "+tag);
  65. }
  66. }
  67. static CsvInputArchive getArchive(InputStream strm)
  68. throws UnsupportedEncodingException {
  69. return new CsvInputArchive(strm);
  70. }
  71. /** Creates a new instance of CsvInputArchive */
  72. public CsvInputArchive(InputStream in)
  73. throws UnsupportedEncodingException {
  74. stream = new PushbackReader(new InputStreamReader(in, "UTF-8"));
  75. }
  76. public byte readByte(String tag) throws IOException {
  77. return (byte) readLong(tag);
  78. }
  79. public boolean readBool(String tag) throws IOException {
  80. String sval = readField(tag);
  81. return "T".equals(sval) ? true : false;
  82. }
  83. public int readInt(String tag) throws IOException {
  84. return (int) readLong(tag);
  85. }
  86. public long readLong(String tag) throws IOException {
  87. String sval = readField(tag);
  88. try {
  89. long lval = Long.parseLong(sval);
  90. return lval;
  91. } catch (NumberFormatException ex) {
  92. throw new IOException("Error deserializing "+tag);
  93. }
  94. }
  95. public float readFloat(String tag) throws IOException {
  96. return (float) readDouble(tag);
  97. }
  98. public double readDouble(String tag) throws IOException {
  99. String sval = readField(tag);
  100. try {
  101. double dval = Double.parseDouble(sval);
  102. return dval;
  103. } catch (NumberFormatException ex) {
  104. throw new IOException("Error deserializing "+tag);
  105. }
  106. }
  107. public String readString(String tag) throws IOException {
  108. String sval = readField(tag);
  109. return Utils.fromCSVString(sval);
  110. }
  111. public byte[] readBuffer(String tag) throws IOException {
  112. String sval = readField(tag);
  113. return Utils.fromCSVBuffer(sval);
  114. }
  115. public void readRecord(Record r, String tag) throws IOException {
  116. r.deserialize(this, tag);
  117. }
  118. public void startRecord(String tag) throws IOException {
  119. if (tag != null && !"".equals(tag)) {
  120. char c1 = (char) stream.read();
  121. char c2 = (char) stream.read();
  122. if (c1 != 's' || c2 != '{') {
  123. throw new IOException("Error deserializing "+tag);
  124. }
  125. }
  126. }
  127. public void endRecord(String tag) throws IOException {
  128. char c = (char) stream.read();
  129. if (tag == null || "".equals(tag)) {
  130. if (c != '\n' && c != '\r') {
  131. throw new IOException("Error deserializing record.");
  132. } else {
  133. return;
  134. }
  135. }
  136. if (c != '}') {
  137. throw new IOException("Error deserializing "+tag);
  138. }
  139. c = (char) stream.read();
  140. if (c != ',') {
  141. stream.unread(c);
  142. }
  143. return;
  144. }
  145. public Index startVector(String tag) throws IOException {
  146. char c1 = (char) stream.read();
  147. char c2 = (char) stream.read();
  148. if (c1 != 'v' || c2 != '{') {
  149. throw new IOException("Error deserializing "+tag);
  150. }
  151. return new CsvIndex();
  152. }
  153. public void endVector(String tag) throws IOException {
  154. char c = (char) stream.read();
  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. return;
  163. }
  164. public Index startMap(String tag) throws IOException {
  165. char c1 = (char) stream.read();
  166. char c2 = (char) stream.read();
  167. if (c1 != 'm' || c2 != '{') {
  168. throw new IOException("Error deserializing "+tag);
  169. }
  170. return new CsvIndex();
  171. }
  172. public void endMap(String tag) throws IOException {
  173. char c = (char) stream.read();
  174. if (c != '}') {
  175. throw new IOException("Error deserializing "+tag);
  176. }
  177. c = (char) stream.read();
  178. if (c != ',') {
  179. stream.unread(c);
  180. }
  181. return;
  182. }
  183. }