Convenient base class to use to implement Serializers, with subclasses only needing to implement #doSerialize(Object, OutputStream). @param the type of object to serialize @since 0.12.0
| 28 | * @since 0.12.0 |
| 29 | */ |
| 30 | public abstract class AbstractSerializer<T> implements Serializer<T> { |
| 31 | |
| 32 | /** |
| 33 | * Default constructor, does not initialize any internal state. |
| 34 | */ |
| 35 | protected AbstractSerializer() { |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * {@inheritDoc} |
| 40 | */ |
| 41 | @Override |
| 42 | public final byte[] serialize(T t) throws SerializationException { |
| 43 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 44 | serialize(t, out); |
| 45 | return out.toByteArray(); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * {@inheritDoc} |
| 50 | */ |
| 51 | @Override |
| 52 | public final void serialize(T t, OutputStream out) throws SerializationException { |
| 53 | try { |
| 54 | doSerialize(t, out); |
| 55 | } catch (Throwable e) { |
| 56 | if (e instanceof SerializationException) { |
| 57 | throw (SerializationException) e; |
| 58 | } |
| 59 | String msg = "Unable to serialize object of type " + Objects.nullSafeClassName(t) + ": " + e.getMessage(); |
| 60 | throw new SerializationException(msg, e); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Converts the specified Java object into a formatted data byte stream, writing the bytes to the specified |
| 66 | * {@code out}put stream. |
| 67 | * |
| 68 | * @param t the object to convert to a byte stream |
| 69 | * @param out the stream to write to |
| 70 | * @throws Exception if there is a problem converting the object to a byte stream or writing the |
| 71 | * bytes to the {@code out}put stream. |
| 72 | * @since 0.12.0 |
| 73 | */ |
| 74 | protected abstract void doSerialize(T t, OutputStream out) throws Exception; |
| 75 | } |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…