[jbosscache-dev] Re: Any reason not to use ExposedByteArrayOutputStream instead of ByteArrayOutputStream ?
Bela Ban
bela at jboss.com
Fri Jul 4 05:46:15 EDT 2008
Manik Surtani wrote:
> cc'ing JBC-dev.
>
> The stream looks good, except that getting the raw buffer would mean
> that the buffer returned would "almost certainly be longer than the
> data written to it" (quoted from Brian's excellent javadocs). Bela,
> how would JGroups behave with this? Would it attempt to stream the
> entire buffer, including uninitialized values?
Yes, good point, this is not good ! You would have to return an
org.jgroups.util.Buffer which has the byte[] buffer but also offset and
length:
public class Buffer {
private final byte[] buf;
private final int offset;
private final int length;
public Buffer(byte[] buf, int offset, int length) {
this.buf=buf;
this.offset=offset;
this.length=length;
}
public byte[] getBuf() {
return buf;
}
public int getOffset() {
return offset;
}
public int getLength() {
return length;
}
public Buffer copy() {
byte[] new_buf=buf != null? new byte[length] : null;
int new_length=new_buf != null? new_buf.length : 0;
if(new_buf != null)
System.arraycopy(buf, offset, new_buf, 0, length);
return new Buffer(new_buf, 0, new_length);
}
public String toString() {
StringBuilder sb=new StringBuilder();
sb.append(length).append(" bytes");
if(offset > 0)
sb.append(" (offset=").append(offset).append(")");
return sb.toString();
}
}
In addition, I would have to change Marshaller to use a Buffer :
public interface Marshaller {
*Buffer* objectToByteBuffer(Object obj) throws Exception;
Object objectFromByteBuffer(*Buffer* buf) throws Exception;
}
This should have far-reaching consequences as I'm not aware of anyone
else but JBossCache to use Marshaller. Or maybe, to be on the safe side,
I could create a Marshaller2 so existing code doesn't break (extending
Marshaller)... http://jira.jboss.com/jira/browse/JGRP-796That should
> Will need to see how this gets de-serialized as well. Also, since all
> cacheloaders use the same marshaller, will need to see how they deal
> with this as well. Any code that relies on buf.length may end up at
> best writing unnecessary data and at worst breaking when deserializing.
That shouldn't be an issue because your JBC marshalling code creates
local variables, and doesn't reuse ByteArrayOutputStream instances
--
Bela Ban
Lead JGroups / Clustering Team
JBoss - a division of Red Hat
More information about the jbosscache-dev
mailing list