@Override public TypeValue deserialise(final byte[] bytes) throws SerialisationException { int lastDelimiter = 0; TypeValue typeValue = new TypeValue(); for (int i = 0; i < bytes.length; i++) { if (bytes[i] == ByteArrayEscapeUtils.DELIMITER) { if (i > 0) { try { typeValue.setType(new String(ByteArrayEscapeUtils.unEscape(bytes, lastDelimiter, i), CommonConstants.UTF_8)); } catch (final UnsupportedEncodingException e) { throw new SerialisationException("Failed to deserialise the Type from TypeValue Object", e); } } lastDelimiter = i + 1; break; } } if (bytes.length > lastDelimiter) { try { typeValue.setValue(new String(ByteArrayEscapeUtils.unEscape(bytes, lastDelimiter, bytes.length), CommonConstants.UTF_8)); } catch (final UnsupportedEncodingException e) { throw new SerialisationException("Failed to deserialise the Value from TypeValue Object", e); } } return typeValue; }
@Test public void testCanSerialiseDeSerialiseCorrectlyValueOnly() throws SerialisationException { TypeValue typeValue = new TypeValue(); typeValue.setValue("testValue"); byte[] bytes = serialiser.serialise(typeValue); String serialisedForm = new String(bytes); assertEquals("\0testValue", serialisedForm); TypeValue deSerialisedTypeValue = serialiser.deserialise(bytes); assertNull(deSerialisedTypeValue.getType()); assertEquals(typeValue.getValue(), deSerialisedTypeValue.getValue()); assertEquals(typeValue, deSerialisedTypeValue); }
@Override public TypeValue deserialise(final byte[] bytes) throws SerialisationException { int lastDelimiter = 0; TypeValue typeValue = new TypeValue(); for (int i = 0; i < bytes.length; i++) { if (bytes[i] == ByteArrayEscapeUtils.DELIMITER) { if (i > 0) { try { typeValue.setType(new String(ByteArrayEscapeUtils.unEscape(bytes, lastDelimiter, i), CommonConstants.UTF_8)); } catch (final UnsupportedEncodingException e) { throw new SerialisationException("Failed to deserialise the Type from TypeValue Object", e); } } lastDelimiter = i + 1; break; } } if (bytes.length > lastDelimiter) { try { typeValue.setValue(new String(ByteArrayEscapeUtils.unEscape(bytes, lastDelimiter, bytes.length), CommonConstants.UTF_8)); } catch (final UnsupportedEncodingException e) { throw new SerialisationException("Failed to deserialise the Value from TypeValue Object", e); } } return typeValue; }