CsvOutputArchive.java 4.3 KB

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