public void serialize(final File outputFile) throws MemInspectorException {
final NodeForObjectTransferObjectFactory factory = NodeForObjectTransferObjectFactory
.getInstance();
try {
final OutputStream out = new FileOutputStreamNIO(outputFile);
final long rootNodeId = currentMemoryImage.getRootNode().getId().getValue();
out.write((byte)(rootNodeId & 0xff));
out.write((byte)((rootNodeId >> 8) & 0xff));
out.write((byte)((rootNodeId >> 16) & 0xff));
out.write((byte)((rootNodeId >> 24) & 0xff));
out.write((byte)((rootNodeId >> 32) & 0xff));
out.write((byte)((rootNodeId >> 40) & 0xff));
out.write((byte)((rootNodeId >> 48) & 0xff));
out.write((byte)((rootNodeId >> 56) & 0xff));
final ITreeNodeForObjectVisitor visitor = new ITreeNodeForObjectVisitor() {
public void beginVisit(final NodeForObject object,
final boolean firstVisit) throws MemInspectorException {
if (firstVisit) {
try {
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
final ObjectOutputStream objectOutputStream = new ObjectOutputStream(
byteArrayOutputStream);
final NodeForObjectTO nodeForObjectTO = factory
.createNodeForObjectTO(object);
objectOutputStream.writeObject(nodeForObjectTO);
objectOutputStream.close();
final byte[] byteArray = byteArrayOutputStream
.toByteArray();
final int length = byteArray.length;
out.write(length & 0xff);
out.write((length >> 8) & 0xff);
out.write((length >> 16) & 0xff);
out.write((length >> 24) & 0xff);
out.write(byteArray);
} catch (IOException exception) {
throw new MemInspectorException(exception);
}
}
}
public void endVisit(final NodeForObject object) {
// no implementation
}
};
visitCurrent(visitor);
out.flush();
out.close();
} catch (FileNotFoundException exception) {
throw new MemInspectorException(exception);
} catch (IOException exception) {
throw new MemInspectorException(exception);
}