JGroupsAddress address = new JGroupsAddress(new IpAddress(12345));
PutKeyValueCommand cmd = new PutKeyValueCommand(
"k", "v", false, null, new EmbeddedMetadata.Builder().build(), Collections.<Flag>emptySet(), AnyEquivalence.getInstance());
try {
// Write
StreamingMarshaller globalMarshal = extractGlobalMarshaller(cm);
ExposedByteArrayOutputStream baos = new ExposedByteArrayOutputStream(1024);
ObjectOutput globalOO = globalMarshal.startObjectOutput(baos, false, 1024);
try {
globalOO.writeObject(address);
/** BEGIN: Special treatment **/
globalOO.flush(); // IMPORTANT: Flush needed to make sure the address gets written!!
globalOO.writeInt(baos.size()); // Note amount of bytes that have been read so far
globalOO.flush(); // IMPORTANT: Flush again!
/** END: Special treatment **/
// Now try cache marshaller to 'borrow' the output stream
StreamingMarshaller cacheMarshaller = extractCacheMarshaller(cm.getCache());
ObjectOutput cacheOO = cacheMarshaller.startObjectOutput(baos, true, 1024);
try {
cacheOO.writeObject(cmd);
} finally {
cacheMarshaller.finishObjectOutput(cacheOO);
}
} finally {
globalMarshal.finishObjectOutput(globalOO);
}
byte[] bytes = new byte[baos.size()];
System.arraycopy(baos.getRawBuffer(), 0, bytes, 0, bytes.length);
// Read
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
ObjectInput globalOI = globalMarshal.startObjectInput(bais, false);
try {
assertEquals(address, globalOI.readObject());
/** BEGIN: Special treatment **/
int offset = globalOI.readInt();
// Now try the cache marshaller and borrow the input stream to read
StreamingMarshaller cacheMarshaller = extractCacheMarshaller(cm.getCache());
// Advance 4 bytes to go over the number of bytes written
bais = new ByteArrayInputStream(bytes, offset + 4, bytes.length);
ObjectInput cacheOI = cacheMarshaller.startObjectInput(bais, true);
/** END: Special treatment **/
try {
assertEquals(cmd, cacheOI.readObject());
} finally {
cacheMarshaller.finishObjectInput(cacheOI);
}
} finally {
globalMarshal.finishObjectInput(globalOI);
}
} finally {