Josh
Josh

Reputation: 137

Efficient Java Serialization: Using ByteOutputArrayStream?

I have been working to find an efficient way to serialize a specific class to pass between my server and client and have been using ByteOutputArrayStream to pass around a byte array. However, this article makes me wonder if I should be using ByteOutputArrayStream at all. Here is the codec I use to serialize and deserialize a class called PackagedData:

public final PackagedData decode(final byte[] data) {
 final ByteArrayInputStream bstream = new ByteArrayInputStream(data);
 final PackagedData result;

 try (final ObjectInputStream ois = new ObjectInputStream(bstream)) {
     result = (PackagedData)ois.readObject();
 }catch (IOException | ClassNotFoundException e) {
     throw new RuntimeException(e.getCause());
 }
 return result;
}

public final byte[] encode(final PackagedData packagedData) {
  final ByteArrayOutputStream bstream = new ByteArrayOutputStream();

  try (final ObjectOutputStream oos = new ObjectOutputStream(bstream)) {
    oos.writeObject(packagedData);
  }
  catch (IOException e) {
    throw new RuntimeException(e.getCause());
  }
  return bstream.toByteArray();
}

The class PackagedData looks like:

public final class PackagedData implements Serializable {
  private final String dataType;
  private final String key;
  private final Integer value;

  public PackagedData(final String dataType, final String key, final Integer value) {
    this.dataType = dataType;
    this.key = key;
    this.value = value;
  }
  public final String getType(){
    return dataType;
  }
  public final String getKey() {
    return key;
  }
  public final Integer getValue() {
    return value;
  }
}

My two questions are: Should I use a ByteArrayOutputStream? If so, what should I set the buffer size to in the parameter? I understand ByteArrayOutputStream increases buffer size as needed but I figure that will just take more resources and time than initializing it at a proper buffer size in the first place.

Thank you in advance.

Upvotes: 0

Views: 185

Answers (1)

Elliott Frisch
Elliott Frisch

Reputation: 201447

If you want to make your object Serialization more efficient, I strongly suggest you implement Externalizable with something like

public final class PackagedData implements Externalizable {
    private String dataType;
    private String key;
    private Integer value;

    public PackagedData(final String dataType, final String key,
            final Integer value) {
        this.dataType = dataType;
        this.key = key;
        this.value = value;
    }

    public String getType() {
        return dataType;
    }

    public String getKey() {
        return key;
    }

    public Integer getValue() {
        return value;
    }

    @Override
    public void writeExternal(ObjectOutput out) throws IOException {
        out.writeUTF(this.dataType);
        out.writeUTF(this.key);
        out.writeInt(this.value);
    }

    @Override
    public void readExternal(ObjectInput in) throws IOException,
            ClassNotFoundException {
        this.dataType = in.readUTF();
        this.key = in.readUTF();
        this.value = in.readInt();
    }
}

If serializing PackageData is truly critical code you should see significant performance improvements.

Upvotes: 1

Related Questions