@Override public byte[] serialise(final Object object) throws SerialisationException { try (ByteArrayOutputStream stream = new ByteArrayOutputStream()) { byte key = supportedSerialisers.getKeyFromValue(object); byte[] bytes = nullCheck(supportedSerialisers.getSerialiserFromKey(key)).serialise(object); stream.write(key); stream.write(bytes); return stream.toByteArray(); } catch (final SerialisationException e) { //re-throw SerialisationException throw e; } catch (final Exception e) { //wraps other exceptions. throw new SerialisationException(e.getMessage(), e); } }
@Override public Object deserialise(final byte[] bytes) throws SerialisationException { try { byte keyByte = bytes[0]; ToBytesSerialiser serialiser = nullCheck(supportedSerialisers.getSerialiserFromKey(keyByte)); return serialiser.deserialise(bytes, 1, bytes.length - 1); } catch (final SerialisationException e) { //re-throw SerialisationException throw e; } catch (final Exception e) { //wraps other exceptions. throw new SerialisationException(e.getMessage(), e); } }
@Override public byte[] serialise(final Object object) throws SerialisationException { try (ByteArrayOutputStream stream = new ByteArrayOutputStream()) { byte key = supportedSerialisers.getKeyFromValue(object); byte[] bytes = nullCheck(supportedSerialisers.getSerialiserFromKey(key)).serialise(object); stream.write(key); stream.write(bytes); return stream.toByteArray(); } catch (final SerialisationException e) { //re-throw SerialisationException throw e; } catch (final Exception e) { //wraps other exceptions. throw new SerialisationException(e.getMessage(), e); } }
@Override public Object deserialise(final byte[] bytes) throws SerialisationException { try { byte keyByte = bytes[0]; ToBytesSerialiser serialiser = nullCheck(supportedSerialisers.getSerialiserFromKey(keyByte)); return serialiser.deserialise(bytes, 1, bytes.length - 1); } catch (final SerialisationException e) { //re-throw SerialisationException throw e; } catch (final Exception e) { //wraps other exceptions. throw new SerialisationException(e.getMessage(), e); } }