Example #1
0
 /**
  * Read a writable from a byte array.
  *
  * @param array byte array with the serialized writable.
  * @param clazz writable class.
  * @return writable deserialized from the byte array.
  */
 public static <T extends Writable> T fromByteArray(byte[] array, Class<T> clazz) {
   try {
     T o = (T) ReflectionUtils.newInstance(clazz, null);
     o.readFields(new DataInputStream(new ByteArrayInputStream(array)));
     return o;
   } catch (IOException ex) {
     throw new RuntimeException(ex);
   }
 }
  private static <T extends Writable> void testWritable(T expected, T actual) throws Exception {
    final ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
    expected.write(new DataOutputStream(bytesOut));

    final byte[] bytes = bytesOut.toByteArray();

    final ByteArrayInputStream bytesIn = new ByteArrayInputStream(bytes);
    actual.readFields(new DataInputStream(bytesIn));

    assertEquals(expected, actual);
  }
 static <T extends Writable> T read(T writable, byte[] bytes) {
   DataInputBuffer buffer = new DataInputBuffer();
   buffer.reset(bytes, bytes.length);
   try {
     writable.readFields(buffer);
     assertThat("Enf of Stream", buffer.read(), is(-1));
   } catch (IOException e) {
     throw new AssertionError(e);
   }
   return writable;
 }
Example #4
0
 /**
  * Read list.
  *
  * @param <T> the generic type
  * @param dataInput the data input
  * @param clazz the clazz
  * @return the list
  * @throws IOException Signals that an I/O exception has occurred.
  */
 public static <T extends Writable> List<T> readList(DataInput dataInput, Class<T> clazz)
     throws IOException {
   List<T> a = new ArrayList<T>();
   int count = dataInput.readInt();
   for (int i = 0; i < count; i++) {
     T o = (T) ReflectionUtils.newInstance(clazz, null);
     o.readFields(dataInput);
     a.add(o);
   }
   return a;
 }
Example #5
0
 /**
  * Read map.
  *
  * @param <T> the generic type
  * @param dataInput the data input
  * @param clazz the clazz
  * @return the map
  * @throws IOException Signals that an I/O exception has occurred.
  */
 public static <T extends Writable> Map<String, T> readMap(DataInput dataInput, Class<T> clazz)
     throws IOException {
   Map<String, T> map = new HashMap<String, T>();
   int count = dataInput.readInt();
   for (int i = 0; i < count; i++) {
     String key = readBytesAsString(dataInput);
     T value = (T) ReflectionUtils.newInstance(clazz, null);
     value.readFields(dataInput);
     map.put(key, value);
   }
   return map;
 }
 @Override
 public T decode(InputStream inStream, Context context) throws IOException {
   try {
     T t = type.getConstructor().newInstance();
     t.readFields(new DataInputStream(inStream));
     return t;
   } catch (NoSuchMethodException | InstantiationException | IllegalAccessException e) {
     throw new CoderException("unable to deserialize record", e);
   } catch (InvocationTargetException ite) {
     throw new CoderException("unable to deserialize record", ite.getCause());
   }
 }
 /**
  * Read an arbitrary Writable
  *
  * @param dataInput DataInput
  * @param <T> Type of writable
  * @return new writable instance
  * @throws IOException I/O errors
  */
 public static <T extends Writable> T readUnknownWritable(DataInput dataInput) throws IOException {
   T writable = readNewInstance(dataInput);
   writable.readFields(dataInput);
   return writable;
 }