/** * @param allBytes The bytes to be decoded into characters * @param offset The index of the first byte to decode * @param length The number of bytes to decode * @return T the deserialised object * @throws SerialisationException issues during deserialisation */ default T deserialise(final byte[] allBytes, final int offset, final int length) throws SerialisationException { final byte[] selection = new byte[length]; try { System.arraycopy(allBytes, offset, selection, 0, length); } catch (final NullPointerException e) { throw new SerialisationException(String.format("Deserialising with giving range caused ArrayIndexOutOfBoundsException. byte[].size:%d startPos:%d length:%d", allBytes.length, 0, length), e); } return deserialise(selection); }
public static ByteArrayOutputStream appendLengthValueFromObjectToByteStream(final ByteArrayOutputStream byteOut, final ToBytesSerialiser serialiser, final Object object) throws SerialisationException { return appendLengthValueFromBytesToByteStream(byteOut, serialiser.serialise(object)); }
private static <T> T getValue(final ToBytesSerialiser<T> serialiser, final byte[] valueBytes) throws SerialisationException { if (0 == valueBytes.length) { return serialiser.deserialiseEmpty(); } return serialiser.deserialise(valueBytes); }
public static <T> byte[] getValueBytes(final ToBytesSerialiser<T> serialiser, final T value) throws SerialisationException { final byte[] valueBytes; if (null == serialiser) { valueBytes = EMPTY_BYTES; } else if (null == value) { valueBytes = serialiser.serialiseNull(); } else { valueBytes = serialiser.serialise(value); } return valueBytes; }
private void validatePutParams(final ToBytesSerialiser serialiser, final Class supportedClass) throws GafferCheckedException { if (null == supportedClass) { throw new GafferCheckedException(String.format("Can not add null supportedClass to MultiSerialiserStorage")); } if (null == serialiser) { throw new GafferCheckedException(String.format("Can not add null serialiser to MultiSerialiserStorage")); } if (!serialiser.canHandle(supportedClass)) { throw new GafferCheckedException(String.format("%s does not handle %s", serialiser.getClass(), supportedClass)); } }
private boolean continuesToPreserveOrdering(final ToBytesSerialiser toBytesSerialiser) { return preservesObjectOrdering && toBytesSerialiser.preservesObjectOrdering(); }
private boolean continuesToBeConsistant(final ToBytesSerialiser toBytesSerialiser) { return consistent && toBytesSerialiser.isConsistent(); }
public static <T> byte[] getValueBytes(final ToBytesSerialiser<T> serialiser, final T value) throws SerialisationException { final byte[] valueBytes; if (null == serialiser) { valueBytes = EMPTY_BYTES; } else if (null == value) { valueBytes = serialiser.serialiseNull(); } else { valueBytes = serialiser.serialise(value); } return valueBytes; }
private void validatePutParams(final ToBytesSerialiser serialiser, final Class supportedClass) throws GafferCheckedException { if (null == supportedClass) { throw new GafferCheckedException(String.format("Can not add null supportedClass to MultiSerialiserStorage")); } if (null == serialiser) { throw new GafferCheckedException(String.format("Can not add null serialiser to MultiSerialiserStorage")); } if (!serialiser.canHandle(supportedClass)) { throw new GafferCheckedException(String.format("%s does not handle %s", serialiser.getClass(), supportedClass)); } }
private boolean continuesToPreserveOrdering(final ToBytesSerialiser toBytesSerialiser) { return preservesObjectOrdering && toBytesSerialiser.preservesObjectOrdering(); }
private boolean continuesToBeConsistant(final ToBytesSerialiser toBytesSerialiser) { return consistent && toBytesSerialiser.isConsistent(); }
public static <T> ObjectCarriage<T> deserialiseNextObject(final ToBytesSerialiser<T> serialiser, final int currentCarriage, final byte[] bytes) throws SerialisationException { int rtn = currentCarriage; int numBytesForLength = CompactRawSerialisationUtils.decodeVIntSize(bytes[rtn]); int currentPropLength = getCurrentPropLength(bytes, rtn, numBytesForLength); int from = rtn += numBytesForLength; int to = rtn += currentPropLength; T object = serialiser.deserialise(Arrays.copyOfRange(bytes, from, to)); return new ObjectCarriage<T>(object, rtn); }
private static <T> T getValue(final ToBytesSerialiser<T> serialiser, final byte[] valueBytes) throws SerialisationException { if (0 == valueBytes.length) { return serialiser.deserialiseEmpty(); } return serialiser.deserialise(valueBytes); }
@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); } }
protected void serialiseSizeAndPropertyValue(final String propertyName, final SchemaElementDefinition elementDefinition, final Properties properties, final ByteArrayOutputStream stream) { try { final TypeDefinition typeDefinition = elementDefinition.getPropertyTypeDef(propertyName); final ToBytesSerialiser serialiser = (null == typeDefinition) ? null : (ToBytesSerialiser) typeDefinition.getSerialiser(); byte[] bytes; if (null == serialiser) { bytes = AccumuloStoreConstants.EMPTY_BYTES; } else { Object value = properties.get(propertyName); //serialiseNull could be different to AccumuloStoreConstants.EMPTY_BYTES bytes = (null == value) ? serialiser.serialiseNull() : serialiser.serialise(value); } writeBytes(bytes, stream); } catch (final IOException e) { throw new AccumuloElementConversionException("Failed to write serialised property to ByteArrayOutputStream" + propertyName, 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 Object getDeserialisedObject(final ToBytesSerialiser serialiser, final byte[] bytes, final int from, final int length) throws SerialisationException { //Don't initialise with #deserialiseEmpty() as this might initialise an complex empty structure to be immediately overwritten e.g. TreeSet<String> Object deserialisedObject; if (length > 0) { deserialisedObject = serialiser.deserialise(bytes, from, length); } else { deserialisedObject = serialiser.deserialiseEmpty(); } return deserialisedObject; }
public static ByteArrayOutputStream appendLengthValueFromObjectToByteStream(final ByteArrayOutputStream byteOut, final ToBytesSerialiser serialiser, final Object object) throws SerialisationException { return appendLengthValueFromBytesToByteStream(byteOut, serialiser.serialise(object)); }
@Override public byte[] buildColumnVisibility(final String group, final Properties properties) { byte[] rtn = AccumuloStoreConstants.EMPTY_BYTES; final SchemaElementDefinition elementDefinition = getSchemaElementDefinition(group); if (null != schema.getVisibilityProperty()) { final TypeDefinition propertyDef = elementDefinition.getPropertyTypeDef(schema.getVisibilityProperty()); if (null != propertyDef) { final Object property = properties.get(schema.getVisibilityProperty()); final ToBytesSerialiser serialiser = (ToBytesSerialiser) propertyDef.getSerialiser(); if (null != property) { try { rtn = serialiser.serialise(property); } catch (final SerialisationException e) { throw new AccumuloElementConversionException(e.getMessage(), e); } } else { rtn = serialiser.serialiseNull(); } } } return rtn; }
private void test(final long value) throws SerialisationException { final byte[] b = serialiser.serialise(value); final Object o = ((ToBytesSerialiser) serialiser).deserialise(b, 0, b.length); assertEquals(Long.class, o.getClass()); assertEquals(value, o); final ByteArrayOutputStream stream = new ByteArrayOutputStream(); CompactRawSerialisationUtils.write(value, new DataOutputStream(stream)); final long result = CompactRawSerialisationUtils.read(new DataInputStream(new ByteArrayInputStream(stream.toByteArray()))); assertEquals(result, value); }