@Override public byte[] serialise(final FreqMap map) throws SerialisationException { Set<Entry<String, Long>> entrySet = map.entrySet(); ByteArrayOutputStream out = new ByteArrayOutputStream(); boolean isFirst = true; for (final Entry<String, Long> entry : entrySet) { if (null != entry.getKey() && null != entry.getValue()) { if (isFirst) { isFirst = false; } else { out.write(ByteArrayEscapeUtils.DELIMITER); } try { out.write(ByteArrayEscapeUtils.escape(entry.getKey().getBytes(CommonConstants.UTF_8))); } catch (final IOException e) { throw new SerialisationException("Failed to serialise a key from a FreqMap: " + entry.getKey(), e); } out.write(ByteArrayEscapeUtils.DELIMITER); try { out.write(ByteArrayEscapeUtils.escape(longSerialiser.serialise(entry.getValue()))); } catch (final IOException e) { throw new SerialisationException("Failed to serialise a value from a FreqMap: " + entry.getValue(), e); } } } return out.toByteArray(); }
@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 testCanSerialiseDeserialiseCorrectlyAndBeEscaped() throws SerialisationException { TypeValue typeValue = new TypeValue("testType", "testValue"); byte[] bytes = ByteArrayEscapeUtils.escape(serialiser.serialise(typeValue)); String serialisedForm = new String(bytes); assertEquals("testType\1\1testValue", serialisedForm); TypeValue deSerialisedTypeValue = serialiser.deserialise(ByteArrayEscapeUtils .unEscape(bytes)); assertEquals(typeValue.getType(), deSerialisedTypeValue.getType()); assertEquals(typeValue.getValue(), deSerialisedTypeValue.getValue()); assertEquals(typeValue, deSerialisedTypeValue); }
/** * Unescapes the provided byte array - this should only be called on byte * arrays that have been through the {@code escape} method. * * @param allBytes The backing byte array which contains the subset to unEscape * @param start The position to start the unEscape, inclusive. * @param end The position to end the unEscape, exclusive * @return the unescaped byte array */ public static byte[] unEscapeByPosition(final byte[] allBytes, final int start, final int end) { return unEscapeByLength(allBytes, start, end - start); }
/** * Unescapes the provided byte array - this should only be called on byte * arrays that have been through the {@code escape} method. * * @param allBytes The backing byte array which contains the subset to unEscape * @param start The position to start the unEscape, inclusive. * @param end The position to end the unEscape, exclusive * @return the unescaped byte array */ public static byte[] unEscape(final byte[] allBytes, final int start, final int end) { return unEscapeByPosition(allBytes, start, end); }
@Test public void testCanSerialiseDeserialiseCorrectlyAndBeEscaped() throws SerialisationException { TypeSubTypeValue typeSubTypeValue = new TypeSubTypeValue("testType", "testSubType", "testValue"); byte[] bytes = ByteArrayEscapeUtils.escape(serialiser.serialise(typeSubTypeValue)); String serialisedForm = new String(bytes); assertEquals("testType\1\1testSubType\1\1testValue", serialisedForm); TypeSubTypeValue deSerialisedTypeSubTypeValue = serialiser .deserialise(ByteArrayEscapeUtils.unEscape(bytes)); assertEquals(typeSubTypeValue.getType(), deSerialisedTypeSubTypeValue.getType()); assertEquals(typeSubTypeValue.getSubType(), deSerialisedTypeSubTypeValue.getSubType()); assertEquals(typeSubTypeValue.getValue(), deSerialisedTypeSubTypeValue.getValue()); assertEquals(typeSubTypeValue, deSerialisedTypeSubTypeValue); }
/** * Unescapes the provided byte array - this should only be called on byte * arrays that have been through the {@code escape} method. * * @param bytes the byte array to unescape * @return the unescaped byte array */ public static byte[] unEscape(final byte[] bytes) { return unEscapeByLength(bytes, 0, bytes.length); }
@Override public byte[] serialise(final TypeValue typeValue) throws SerialisationException { String type = typeValue.getType(); String value = typeValue.getValue(); if ((null == type || type.isEmpty()) && (null == value || value.isEmpty())) { throw new SerialisationException("TypeValue passed to serialiser is blank"); } ByteArrayOutputStream out = new ByteArrayOutputStream(); if (null != type) { try { out.write(ByteArrayEscapeUtils.escape(type.getBytes(CommonConstants.UTF_8))); } catch (final IOException e) { throw new SerialisationException("Failed to serialise the Type from TypeValue Object", e); } } out.write(ByteArrayEscapeUtils.DELIMITER); if (null != value) { try { out.write(ByteArrayEscapeUtils.escape(value.getBytes(CommonConstants.UTF_8))); } catch (final IOException e) { throw new SerialisationException("Failed to serialise the Value from TypeValue Object", e); } } return out.toByteArray(); }
if (i > 0) { try { typeSubTypeValue.setType(new String(ByteArrayEscapeUtils.unEscape(bytes, lastDelimiter, i), CommonConstants.UTF_8)); } catch (final UnsupportedEncodingException e) { throw new SerialisationException("Failed to deserialise the Type from TypeSubTypeValue Object", e); if (i > lastDelimiter) { try { typeSubTypeValue.setSubType(new String(ByteArrayEscapeUtils.unEscape(bytes, lastDelimiter, i), CommonConstants.UTF_8)); } catch (final UnsupportedEncodingException e) { throw new SerialisationException("Failed to deserialise the SubType from TypeSubTypeValue Object", e); typeSubTypeValue.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 TypeSubTypeValue Object", e);
if (null != type) { try { out.write(ByteArrayEscapeUtils.escape(type.getBytes(CommonConstants.UTF_8))); } catch (final IOException e) { throw new SerialisationException("Failed to serialise the Type from TypeSubTypeValue Object", e); if (null != subType) { try { out.write(ByteArrayEscapeUtils.escape(subType.getBytes(CommonConstants.UTF_8))); } catch (final IOException e) { throw new SerialisationException("Failed to serialise the SubType from TypeSubTypeValue Object", e); if (null != value) { try { out.write(ByteArrayEscapeUtils.escape(value.getBytes(CommonConstants.UTF_8))); } catch (final IOException e) { throw new SerialisationException("Failed to serialise the Value from TypeSubTypeValue Object", e);
key = new String(ByteArrayEscapeUtils.unEscape(bytes, lastDelimiter, i), CommonConstants.UTF_8); } catch (final UnsupportedEncodingException e) { throw new SerialisationException("Failed to deserialise a key from a FreqMap", e); final Long value = longSerialiser.deserialise(ByteArrayEscapeUtils.unEscape(bytes, lastDelimiter, i)); freqMap.put(key, value); key = null; final Long value = longSerialiser.deserialise(ByteArrayEscapeUtils.unEscape(bytes, lastDelimiter, bytes.length)); freqMap.put(key, value);
/** * Escapes the provided byte[] so that it no longer contains the * Constants.DELIMITER character. * After escaping the byte[] the appendAfterEscaping[] is appended unescaped. * * @param bytes the byte array to escape * @param appendAfterEscaping the bytes to append to the array after being escaped. * @return the escaped byte array */ public static byte[] escape(final byte[] bytes, final byte... appendAfterEscaping) { final byte[] temp = new byte[(2 * bytes.length) + ((null == appendAfterEscaping) ? 0 : appendAfterEscaping.length)]; int currentPosition = escape(bytes, temp, 0); if (null != appendAfterEscaping) { for (final byte b : appendAfterEscaping) { temp[currentPosition++] = b; } } return Arrays.copyOfRange(temp, 0, currentPosition); }
@SuppressWarnings("unchecked") @Override protected EntityId getEntityId(final byte[] row) { try { return new EntitySeed(((ToBytesSerialiser) schema.getVertexSerialiser()) .deserialise(ByteArrayEscapeUtils.unEscape(Arrays.copyOfRange(row, 0, row.length - 2)))); } catch (final SerialisationException e) { throw new AccumuloElementConversionException("Failed to create EntityId from Accumulo row key", e); } }
@Override public byte[] serialise(final FreqMap map) throws SerialisationException { Set<Entry<String, Long>> entrySet = map.entrySet(); ByteArrayOutputStream out = new ByteArrayOutputStream(); boolean isFirst = true; for (final Entry<String, Long> entry : entrySet) { if (null != entry.getKey() && null != entry.getValue()) { if (isFirst) { isFirst = false; } else { out.write(ByteArrayEscapeUtils.DELIMITER); } try { out.write(ByteArrayEscapeUtils.escape(entry.getKey().getBytes(CommonConstants.UTF_8))); } catch (final IOException e) { throw new SerialisationException("Failed to serialise a key from a FreqMap: " + entry.getKey(), e); } out.write(ByteArrayEscapeUtils.DELIMITER); try { out.write(ByteArrayEscapeUtils.escape(longSerialiser.serialise(entry.getValue()))); } catch (final IOException e) { throw new SerialisationException("Failed to serialise a value from a FreqMap: " + entry.getValue(), e); } } } return out.toByteArray(); }
@Override public byte[] serialise(final TypeValue typeValue) throws SerialisationException { String type = typeValue.getType(); String value = typeValue.getValue(); if ((null == type || type.isEmpty()) && (null == value || value.isEmpty())) { throw new SerialisationException("TypeValue passed to serialiser is blank"); } ByteArrayOutputStream out = new ByteArrayOutputStream(); if (null != type) { try { out.write(ByteArrayEscapeUtils.escape(type.getBytes(CommonConstants.UTF_8))); } catch (final IOException e) { throw new SerialisationException("Failed to serialise the Type from TypeValue Object", e); } } out.write(ByteArrayEscapeUtils.DELIMITER); if (null != value) { try { out.write(ByteArrayEscapeUtils.escape(value.getBytes(CommonConstants.UTF_8))); } catch (final IOException e) { throw new SerialisationException("Failed to serialise the Value from TypeValue Object", e); } } return out.toByteArray(); }
@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; }
if (null != type) { try { out.write(ByteArrayEscapeUtils.escape(type.getBytes(CommonConstants.UTF_8))); } catch (final IOException e) { throw new SerialisationException("Failed to serialise the Type from TypeSubTypeValue Object", e); if (null != subType) { try { out.write(ByteArrayEscapeUtils.escape(subType.getBytes(CommonConstants.UTF_8))); } catch (final IOException e) { throw new SerialisationException("Failed to serialise the SubType from TypeSubTypeValue Object", e); if (null != value) { try { out.write(ByteArrayEscapeUtils.escape(value.getBytes(CommonConstants.UTF_8))); } catch (final IOException e) { throw new SerialisationException("Failed to serialise the Value from TypeSubTypeValue Object", e);
if (i > 0) { try { typeSubTypeValue.setType(new String(ByteArrayEscapeUtils.unEscape(bytes, lastDelimiter, i), CommonConstants.UTF_8)); } catch (final UnsupportedEncodingException e) { throw new SerialisationException("Failed to deserialise the Type from TypeSubTypeValue Object", e); if (i > lastDelimiter) { try { typeSubTypeValue.setSubType(new String(ByteArrayEscapeUtils.unEscape(bytes, lastDelimiter, i), CommonConstants.UTF_8)); } catch (final UnsupportedEncodingException e) { throw new SerialisationException("Failed to deserialise the SubType from TypeSubTypeValue Object", e); typeSubTypeValue.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 TypeSubTypeValue Object", e);
@Override public byte[] serialiseVertex(final Object vertex) { try { return ByteArrayEscapeUtils.escape(((ToBytesSerialiser) schema.getVertexSerialiser()).serialise(vertex)); } catch (final SerialisationException e) { throw new AccumuloElementConversionException( "Failed to serialise given identifier object for use in the bloom filter", e); } }