DataOutputStream out = new DataOutputStream(new BufferedOutputStream(stream));
try {
for (Map.Entry<String, StateTransferProvider> entry: stateProviders.entrySet()) {
String serviceName = entry.getKey();
out.writeUTF(serviceName);
StateTransferProvider provider = entry.getValue();
Marshaller marshaller = marshallerFactory.createMarshaller(new MarshallingConfiguration());
ByteArrayOutputStream output = new ByteArrayOutputStream();
marshaller.start(Marshalling.createByteOutput(output));
marshaller.writeObject(provider.getCurrentState());
marshaller.close();
byte[] bytes = output.toByteArray();
out.writeInt(bytes.length);
out.write(bytes);
}