WritableKryoSerializer.java 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package com.sdyc.ndmp.protobuf.serializer;
  2. import com.esotericsoftware.kryo.Kryo;
  3. import com.esotericsoftware.kryo.io.Input;
  4. import com.esotericsoftware.kryo.io.Output;
  5. import org.apache.hadoop.io.Writable;
  6. import java.io.DataInputStream;
  7. import java.io.DataOutputStream;
  8. /**
  9. * <pre>
  10. *
  11. * Created by IntelliJ IDEA.
  12. * User: zhenqin
  13. * Date: 15/1/23
  14. * Time: 10:02
  15. * To change this template use File | Settings | File Templates.
  16. *
  17. * </pre>
  18. *
  19. * @author zhenqin
  20. */
  21. public class WritableKryoSerializer extends com.esotericsoftware.kryo.Serializer<Writable> {
  22. @Override
  23. public void write(Kryo kryo, Output output, Writable object) {
  24. try {
  25. object.write(new DataOutputStream(output));
  26. } catch (Exception ex) {
  27. throw new IllegalArgumentException("Cannot deserialize", ex);
  28. }
  29. }
  30. @Override
  31. public Writable read(Kryo kryo, Input input, Class<Writable> type) {
  32. try {
  33. Writable object = type.newInstance();
  34. object.readFields(new DataInputStream(input));
  35. return object;
  36. } catch (Exception ex) {
  37. throw new IllegalArgumentException("Cannot serialize", ex);
  38. }
  39. }
  40. }