@Override public Object deserialise(final byte[] bytes) throws SerialisationException { return deserialise(bytes, 0, bytes.length); }
@Override public Serialiser<Object, byte[]> getSerialisation() { return new JavaSerialiser(); }
@Override public byte[] serialise(final Object object) throws SerialisationException { ObjectOutputStream out = null; ByteArrayOutputStream byteOut = null; try { byteOut = new ByteArrayOutputStream(); out = new ObjectOutputStream(byteOut); out.writeObject(object); return byteOut.toByteArray(); } catch (final IOException e) { throw new SerialisationException("Unable to serialise given object of class: " + object.getClass().getName() + ", does it implement the serializable interface?", e); } finally { close(out); close(byteOut); } }
@Override public void put(final K key, final V value) { if (useJavaSerialisation) { try { cache.put(key, JAVA_SERIALISER.serialise(value)); } catch (final SerialisationException e) { throw new RuntimeException(e); } } else { cache.<K, V>put(key, value); } }
@Override public V get(final K key) { try { return (V) (useJavaSerialisation ? JAVA_SERIALISER.deserialise((byte[]) cache.get(key)) : cache.get(key)); } catch (final SerialisationException e) { throw new RuntimeException(e); } }
@Override public byte[] serialise(final Object object) throws SerialisationException { ObjectOutputStream out = null; ByteArrayOutputStream byteOut = null; try { byteOut = new ByteArrayOutputStream(); out = new ObjectOutputStream(byteOut); out.writeObject(object); return byteOut.toByteArray(); } catch (final IOException e) { throw new SerialisationException("Unable to serialise given object of class: " + object.getClass().getName() + ", does it implement the serializable interface?", e); } finally { close(out); close(byteOut); } }
@Override public Collection<V> getAllValues() { ArrayList<V> rtn = Lists.newArrayList(); if (useJavaSerialisation) { cache.values() .forEach((Object o) -> { try { rtn.add((V) JAVA_SERIALISER.deserialise((byte[]) o)); } catch (final SerialisationException e) { throw new RuntimeException(e); } }); } else { rtn.addAll((Collection<V>) cache.values()); } return rtn; }
@Override public Object deserialise(final byte[] bytes) throws SerialisationException { return deserialise(bytes, 0, bytes.length); }