throw new SerializationException( "Unable to de-serialze. Expected version " + VERSION + " but was version " + version ); throw new SerializationException( "The total size we expected was different. Stored total size is " + expectedTotalSize + " but actual buffer size is " + totalSize );
/** {@inheritDoc} */ @Override public Object fromByteBuffer(ByteBuffer bytes) { if (bytes == null || !bytes.hasRemaining()) { return null; } ByteBuffer dup = bytes.duplicate(); ByteArrayInputStream bais = new ByteArrayInputStream(dup.array()); try { XMLStreamReader reader = createStreamReader(bais); Object ret = unmarshaller.get().unmarshal(reader); reader.close(); return ret; } catch (JAXBException e) { throw new SerializationException("Jaxb exception occurred during deserialization.", e); } catch (XMLStreamException e) { throw new SerializationException("Exception reading XML stream.", e); } }
/** {@inheritDoc} */ @Override public ByteBuffer toByteBuffer(Object obj) { if (obj == null) { return null; } ByteArrayOutputStream buffer = new ByteArrayOutputStream(); try { XMLStreamWriter writer = createStreamWriter(buffer); marshaller.get().marshal(obj, writer); writer.flush(); writer.close(); } catch (JAXBException e) { throw new SerializationException("Object to serialize " + obj + " does not seem compatible with the configured JaxbContext;" + " note this Serializer works only with JAXBable objects.", e); } catch (XMLStreamException e) { throw new SerializationException("Exception occurred writing XML stream.", e); } return ByteBuffer.wrap(buffer.toByteArray()); }
@Override public Object fromByteBuffer(ByteBuffer bytes) { if ((bytes == null) || !bytes.hasRemaining()) { return null; } try { ByteBuffer dup = bytes.duplicate(); int l = dup.remaining(); ByteArrayInputStream bais = new ByteArrayInputStream(dup.array(), dup.arrayOffset() + dup.position(), l); ObjectInputStream ois = new ObjectInputStream(bais); Object obj = ois.readObject(); dup.position(dup.position() + (l - ois.available())); ois.close(); return obj; } catch (Exception ex) { throw new SerializationException(ex); } }
@Override public S fromByteBuffer(ByteBuffer bytes) throws SerializationException { if ((bytes == null) || !bytes.hasArray()) { return null; } ByteBuffer dup = bytes.duplicate(); if (compareByteArrays(prefixBytes.array(), prefixBytes.arrayOffset() + prefixBytes.position(), prefixBytes.remaining(), dup.array(), dup.arrayOffset() + dup.position(), prefixBytes.remaining()) != 0) { log.error("Unprefixed value received, throwing exception..."); throw new SerializationException("Unexpected prefix value"); } dup.position(prefixBytes.remaining()); S s = suffixSerializer.fromByteBuffer(dup); return s; }