[jboss-jira] [JBoss JIRA] (JGRP-2122) Streamable class might implement SizeStreamable
Bela Ban (JIRA)
issues at jboss.org
Thu Nov 3 04:08:00 EDT 2016
Bela Ban created JGRP-2122:
------------------------------
Summary: Streamable class might implement SizeStreamable
Key: JGRP-2122
URL: https://issues.jboss.org/browse/JGRP-2122
Project: JGroups
Issue Type: Enhancement
Reporter: Bela Ban
Assignee: Bela Ban
Fix For: 4.0
Many classes implement Streamable, but also provide a serializedSize() method which returns the number of bytes needed for the serialized form.
Methods like Util.streamableToBuffer() could benefit from this: instead of allocating a 512 byte buffer, which is either too big or needs to be resized later, we can allocate a buffer of the exact size.
We should therefore make all classes implementing Streamable instead implement SizeStreamable.
Code that makes use of this:
{code:java|title=Util.streamableToBuffer()|borderStyle=solid}
public static Buffer streamableToBuffer(Streamable obj) {
int expected_size=obj instanceof SizeStreamable?
((SizeStreamable)obj).serializedSize() +1 : 512;
final ByteArrayDataOutputStream out=new ByteArrayDataOutputStream(expected_size);
try {
Util.writeStreamable(obj,out);
return out.getBuffer();
}
catch(Exception ex) {
return null;
}
}
{code}
--
This message was sent by Atlassian JIRA
(v7.2.3#72005)
More information about the jboss-jira
mailing list