/howl/src/java/org/apache/howl/data/ReaderWriter.java

https://github.com/thainb/howl · Java · 179 lines · 128 code · 33 blank · 18 comment · 6 complexity · e198cd8b426f0b838fadb300e27e19c7 MD5 · raw file

  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 org.apache.howl.data;
  19. import java.io.DataInput;
  20. import java.io.DataOutput;
  21. import java.io.IOException;
  22. import java.util.ArrayList;
  23. import java.util.HashMap;
  24. import java.util.Iterator;
  25. import java.util.List;
  26. import java.util.Map;
  27. import java.util.Map.Entry;
  28. import org.apache.hadoop.io.VIntWritable;
  29. import org.apache.hadoop.io.VLongWritable;
  30. public abstract class ReaderWriter {
  31. private static final String UTF8 = "UTF-8";
  32. public static Object readDatum(DataInput in) throws IOException {
  33. byte type = in.readByte();
  34. switch (type) {
  35. case DataType.STRING:
  36. byte[] buffer = new byte[in.readInt()];
  37. in.readFully(buffer);
  38. return new String(buffer,UTF8);
  39. case DataType.INTEGER:
  40. VIntWritable vint = new VIntWritable();
  41. vint.readFields(in);
  42. return vint.get();
  43. case DataType.LONG:
  44. VLongWritable vlong = new VLongWritable();
  45. vlong.readFields(in);
  46. return vlong.get();
  47. case DataType.FLOAT:
  48. return in.readFloat();
  49. case DataType.DOUBLE:
  50. return in.readDouble();
  51. case DataType.BOOLEAN:
  52. return in.readBoolean();
  53. case DataType.BYTE:
  54. return in.readByte();
  55. case DataType.SHORT:
  56. return in.readShort();
  57. case DataType.NULL:
  58. return null;
  59. case DataType.MAP:
  60. int size = in.readInt();
  61. Map<Object,Object> m = new HashMap<Object, Object>(size);
  62. for (int i = 0; i < size; i++) {
  63. m.put(readDatum(in), readDatum(in));
  64. }
  65. return m;
  66. case DataType.LIST:
  67. int sz = in.readInt();
  68. List<Object> list = new ArrayList<Object>(sz);
  69. for(int i=0; i < sz; i++) {
  70. list.add(readDatum(in));
  71. }
  72. return list;
  73. default:
  74. throw new IOException("Unexpected data type " + type +
  75. " found in stream.");
  76. }
  77. }
  78. public static void writeDatum(DataOutput out, Object val) throws IOException {
  79. // write the data type
  80. byte type = DataType.findType(val);
  81. switch (type) {
  82. case DataType.LIST:
  83. out.writeByte(DataType.LIST);
  84. List<?> list = (List<?>)val;
  85. int sz = list.size();
  86. out.writeInt(sz);
  87. for (int i = 0; i < sz; i++) {
  88. writeDatum(out, list.get(i));
  89. }
  90. return;
  91. case DataType.MAP:
  92. out.writeByte(DataType.MAP);
  93. Map<?,?> m = (Map<?, ?>)val;
  94. out.writeInt(m.size());
  95. Iterator<?> i =
  96. m.entrySet().iterator();
  97. while (i.hasNext()) {
  98. Entry<?,?> entry = (Entry<?, ?>) i.next();
  99. writeDatum(out, entry.getKey());
  100. writeDatum(out, entry.getValue());
  101. }
  102. return;
  103. case DataType.INTEGER:
  104. out.writeByte(DataType.INTEGER);
  105. new VIntWritable((Integer)val).write(out);
  106. return;
  107. case DataType.LONG:
  108. out.writeByte(DataType.LONG);
  109. new VLongWritable((Long)val).write(out);
  110. return;
  111. case DataType.FLOAT:
  112. out.writeByte(DataType.FLOAT);
  113. out.writeFloat((Float)val);
  114. return;
  115. case DataType.DOUBLE:
  116. out.writeByte(DataType.DOUBLE);
  117. out.writeDouble((Double)val);
  118. return;
  119. case DataType.BOOLEAN:
  120. out.writeByte(DataType.BOOLEAN);
  121. out.writeBoolean((Boolean)val);
  122. return;
  123. case DataType.BYTE:
  124. out.writeByte(DataType.BYTE);
  125. out.writeByte((Byte)val);
  126. return;
  127. case DataType.SHORT:
  128. out.writeByte(DataType.SHORT);
  129. out.writeShort((Short)val);
  130. return;
  131. case DataType.STRING:
  132. String s = (String)val;
  133. byte[] utfBytes = s.getBytes(ReaderWriter.UTF8);
  134. out.writeByte(DataType.STRING);
  135. out.writeInt(utfBytes.length);
  136. out.write(utfBytes);
  137. return;
  138. case DataType.NULL:
  139. out.writeByte(DataType.NULL);
  140. return;
  141. default:
  142. throw new IOException("Unexpected data type " + type +
  143. " found in stream.");
  144. }
  145. }
  146. }