public ToBytesSerialiser getSerialiserFromValue(final Object object) { return (null == object) ? null : getSerialiserFromKey(getKeyFromValue(object)); }
@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); } }
private void checkBasicPut() throws GafferCheckedException { assertEquals((Object) BYTE, mss.getKeyFromValue(VALUE)); ToBytesSerialiser actualClassFromByte = mss.getSerialiserFromKey(BYTE); assertNotNull("Byte key not found", actualClassFromByte); assertEquals("Wrong SerialiserClass returned for key", SERIALISER_CLASS, actualClassFromByte); ToBytesSerialiser actualClassFromValue = mss.getSerialiserFromValue(Integer.MAX_VALUE); assertNotNull("Value class not found", actualClassFromValue); assertEquals("Wrong SerialiserClass returned for value class", SERIALISER_CLASS, actualClassFromValue); }
@Test public void shouldNotRetainOldSerialiserWhenKeyIsOverWritten() throws Exception { //when mss.put(BYTE, SERIALISER_CLASS, SUPPORTED_CLASS); mss.put(BYTE, SERIALISER_CLASS2, SUPPORTED_CLASS); //then assertNotNull( mss.getKeyFromValue(VALUE)); assertEquals("Wrong key for value", (Object) BYTE, mss.getKeyFromValue(VALUE)); ToBytesSerialiser actualClassFromByte = mss.getSerialiserFromKey(BYTE); assertNotNull("Byte key not found", actualClassFromByte); assertEquals("Wrong new SerialiserClass returned for key", SERIALISER_CLASS2, actualClassFromByte); ToBytesSerialiser actualClassFromValue = mss.getSerialiserFromValue(Integer.MAX_VALUE); assertNotNull("Value class not found", actualClassFromValue); assertEquals("Wrong new SerialiserClass returned for value class", SERIALISER_CLASS2, actualClassFromValue); }
@Test public void shouldUpdateToNewerValueToSerialiser() throws Exception { //give byte serialiserEncoding = BYTE + 1; //when mss.put(serialiserEncoding, SERIALISER_CLASS2, SUPPORTED_CLASS); mss.put(BYTE, SERIALISER_CLASS, SUPPORTED_CLASS); //then checkBasicPut(); assertEquals( BYTE, (byte) mss.getKeyFromValue(VALUE)); ToBytesSerialiser actualClassFromByte2 = mss.getSerialiserFromKey(serialiserEncoding); assertNotNull("Byte key not found", actualClassFromByte2); assertEquals("Wrong SerialiserClass returned for key", SERIALISER_CLASS2, actualClassFromByte2); ToBytesSerialiser actualClassFromValue2 = mss.getSerialiserFromValue(Integer.MAX_VALUE); assertNotNull("Value class not found", actualClassFromValue2); assertEquals("Wrong SerialiserClass, should have updated to newer SerialiserClass", SERIALISER_CLASS, actualClassFromValue2); }
public ToBytesSerialiser getSerialiserFromValue(final Object object) { return (null == object) ? null : getSerialiserFromKey(getKeyFromValue(object)); }
@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); } }