Compare Java and data serialization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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