Skip to content

Instantly share code, notes, and snippets.

@jmesnil
Created July 27, 2012 09:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jmesnil/3187135 to your computer and use it in GitHub Desktop.
Save jmesnil/3187135 to your computer and use it in GitHub Desktop.
Compare Java and data serialization
package org.jboss.as.test.smoke.jms.objectmessage;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.UUID;
public class Main {
static class Payload implements Serializable {
private static final long serialVersionUID = -6537086985098258447L;
private final int field;
private final String text;
Payload(final int field, String text) {
this.field = field;
this.text = text;
}
public int getField() {
return field;
}
public String getText() {
return text;
}
}
public static void main(String[] args) throws Exception {
int field = Integer.MAX_VALUE;
String text = UUID.randomUUID().toString();
Payload payload = new Payload(field, text);
ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
DataOutputStream dos = new DataOutputStream(baos);
dos.writeInt(field);
dos.writeUTF(text);
baos.flush();
int dataSize = baos.toByteArray().length;
baos.reset();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(payload);
baos.flush();
int objectSize = baos.toByteArray().length;
System.out.println("data to bytes => " + dataSize); // => 42
System.out.println("object to bytes => " + objectSize); // => 154
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment