Permalink
Browse files

Remove raw buffer size from serialization process to avoid allocating…

… more memory than is needed when deserializing. add method to calculate the # of bytes required to serialize the bitmap
  • Loading branch information...
davidtime committed Feb 23, 2012
1 parent f348e81 commit 9c0fd8e1e0e4c982e097d47bc04f8a6e1d8fdfd7
Showing with 12 additions and 4 deletions.
  1. +12 −4 src/main/java/javaewah/EWAHCompressedBitmap.java
@@ -1293,9 +1293,8 @@ public void readExternal(ObjectInput in) throws IOException {
public void deserialize(DataInput in) throws IOException {
this.sizeinbits = in.readInt();
this.actualsizeinwords = in.readInt();
- int bufferSize = in.readInt();
- if (this.buffer.length < bufferSize) {
- this.buffer = new long[bufferSize];
+ if (this.buffer.length < actualsizeinwords) {
+ this.buffer = new long[actualsizeinwords];
}
for (int k = 0; k < this.actualsizeinwords; ++k)
this.buffer[k] = in.readLong();
@@ -1318,12 +1317,21 @@ public void writeExternal(ObjectOutput out) throws IOException {
public void serialize(DataOutput out) throws IOException {
out.writeInt(this.sizeinbits);
out.writeInt(this.actualsizeinwords);
- out.writeInt(this.buffer.length);
for (int k = 0; k < this.actualsizeinwords; ++k)
out.writeLong(this.buffer[k]);
out.writeInt(this.rlw.position);
}
+ /**
+ * Report the size required to serialize this bitmap
+ *
+ * @return the size in bytes
+ */
+ public int serializedSizeInBytes() {
+ return this.sizeInBytes() + 3 * 4;
+ }
+
+
/**
* Clear any set bits and set size in bits back to 0
*/

0 comments on commit 9c0fd8e

Please sign in to comment.