Skip to content

Commit

Permalink
Grow ByteArrayDataOutputStream exponentially (https://issues.jboss.or…
Browse files Browse the repository at this point in the history
  • Loading branch information
belaban committed Jan 13, 2016
1 parent d363d02 commit b92bc55
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 21 deletions.
42 changes: 29 additions & 13 deletions src/org/jgroups/util/ByteArrayDataOutputStream.java
Expand Up @@ -13,25 +13,30 @@
* @since 3.5 * @since 3.5
*/ */
public class ByteArrayDataOutputStream implements DataOutput { public class ByteArrayDataOutputStream implements DataOutput {
protected byte[] buf; protected byte[] buf;
protected int pos; protected int pos;
protected boolean grow_exponentially; // if true, the buffer will double every time


public ByteArrayDataOutputStream() { public ByteArrayDataOutputStream() {
this(32); this(32, false);
} }


public ByteArrayDataOutputStream(int capacity) { public ByteArrayDataOutputStream(int capacity) {
this.buf=new byte[capacity]; this(capacity, false);
} }


public ByteArrayDataOutputStream position(int pos) { public ByteArrayDataOutputStream(int capacity, boolean grow_exponentially) {
this.pos=checkBounds(pos); return this; this.buf=new byte[capacity];
this.grow_exponentially=grow_exponentially;
} }


public int position() {return pos;} public ByteArrayDataOutputStream position(int pos) {this.pos=checkBounds(pos); return this;}
public byte[] buffer() {return buf;} public int position() {return pos;}
public Buffer getBuffer() {return new Buffer(buf, 0, pos);} public byte[] buffer() {return buf;}
public ByteBuffer getByteBuffer() {return ByteBuffer.wrap(buf, 0, pos);} public Buffer getBuffer() {return new Buffer(buf, 0, pos);}
public ByteBuffer getByteBuffer() {return ByteBuffer.wrap(buf, 0, pos);}
public boolean growExponentially() {return grow_exponentially;}
public ByteArrayDataOutputStream growExponentially(boolean b) {grow_exponentially=b; return this;}




public void write(int b) { public void write(int b) {
Expand Down Expand Up @@ -181,10 +186,21 @@ protected int checkBounds(int pos) {
return pos; return pos;
} }


/** Grows the buffer; whether it grow linearly or exponentially depends on grow_exponentially */
protected void ensureCapacity(int bytes) { protected void ensureCapacity(int bytes) {
if(pos + bytes > buf.length) { int minCapacity=pos+bytes;
int new_size=buf.length + bytes + 32;
buf=Arrays.copyOf(buf, new_size); if(minCapacity - buf.length > 0) {
int newCapacity=this.grow_exponentially? buf.length << 1 : buf.length + bytes + 32;
if(newCapacity - minCapacity < 0)
newCapacity=minCapacity;
if(newCapacity < 0) {
if(minCapacity < 0) // overflow
throw new OutOfMemoryError();
newCapacity=Integer.MAX_VALUE;
}
// System.out.printf("growing buffer from %d -> %d (pos=%d, bytes=%d)\n", buf.length, newCapacity, pos, bytes);
buf=Arrays.copyOf(buf, newCapacity);
} }
} }
} }
16 changes: 8 additions & 8 deletions src/org/jgroups/util/Util.java
Expand Up @@ -521,20 +521,20 @@ public static byte[] objectToByteBuffer(Object obj) throws Exception {
return TYPE_NULL_ARRAY; return TYPE_NULL_ARRAY;


if(obj instanceof Streamable) { if(obj instanceof Streamable) {
final ByteArrayDataOutputStream out=new ByteArrayDataOutputStream(512); final ByteArrayDataOutputStream out=new ByteArrayDataOutputStream(512, true);
out.write(TYPE_STREAMABLE); out.write(TYPE_STREAMABLE);
writeGenericStreamable((Streamable)obj,out); writeGenericStreamable((Streamable)obj,out);
return Arrays.copyOf(out.buf,out.position()); return Arrays.copyOf(out.buf,out.position());
} }


Byte type=PRIMITIVE_TYPES.get(obj.getClass()); Byte type=PRIMITIVE_TYPES.get(obj.getClass());
if(type == null) { // will throw an exception if object is not serializable if(type == null) { // will throw an exception if object is not serializable
final ByteArrayOutputStream out_stream=new ByteArrayOutputStream(512); final ByteArrayDataOutputStream out_stream=new ByteArrayDataOutputStream(512, true);
out_stream.write(TYPE_SERIALIZABLE); out_stream.write(TYPE_SERIALIZABLE);
try(ObjectOutputStream out=new ObjectOutputStream(out_stream)) { try(ObjectOutputStream out=new ObjectOutputStream(new OutputStreamAdapter(out_stream))) {
out.writeObject(obj); out.writeObject(obj);
out.flush(); out.flush();
return out_stream.toByteArray(); return Arrays.copyOf(out_stream.buffer(), out_stream.position());
} }
} }


Expand Down Expand Up @@ -597,20 +597,20 @@ public static Buffer objectToBuffer(Object obj) throws Exception {
return new Buffer(TYPE_NULL_ARRAY); return new Buffer(TYPE_NULL_ARRAY);


if(obj instanceof Streamable) { if(obj instanceof Streamable) {
final ByteArrayDataOutputStream out=new ByteArrayDataOutputStream(512); final ByteArrayDataOutputStream out=new ByteArrayDataOutputStream(512, true);
out.write(TYPE_STREAMABLE); out.write(TYPE_STREAMABLE);
writeGenericStreamable((Streamable)obj,out); writeGenericStreamable((Streamable)obj,out);
return out.getBuffer(); return out.getBuffer();
} }


Byte type=PRIMITIVE_TYPES.get(obj.getClass()); Byte type=PRIMITIVE_TYPES.get(obj.getClass());
if(type == null) { // will throw an exception if object is not serializable if(type == null) { // will throw an exception if object is not serializable
final ByteArrayOutputStream out_stream=new ByteArrayOutputStream(512); final ByteArrayDataOutputStream out_stream=new ByteArrayDataOutputStream(512, true);
out_stream.write(TYPE_SERIALIZABLE); out_stream.write(TYPE_SERIALIZABLE);
try(ObjectOutputStream out=new ObjectOutputStream(out_stream)) { try(ObjectOutputStream out=new ObjectOutputStream(new OutputStreamAdapter(out_stream))) {
out.writeObject(obj); out.writeObject(obj);
out.flush(); out.flush();
return new Buffer(out_stream.toByteArray()); return out_stream.getBuffer();
} }
} }


Expand Down

0 comments on commit b92bc55

Please sign in to comment.