Author: nextgens
Date: 2008-08-17 10:12:46 +0000 (Sun, 17 Aug 2008)
New Revision: 21948
Modified:
trunk/freenet/src/freenet/support/io/ArrayBucket.java
Log:
ArrayBucket: account the size appended to the bucket so that size() can be fast.
Introduce a maxLength parameter and enforce it
Modified: trunk/freenet/src/freenet/support/io/ArrayBucket.java
===================================================================
--- trunk/freenet/src/freenet/support/io/ArrayBucket.java 2008-08-17
09:35:35 UTC (rev 21947)
+++ trunk/freenet/src/freenet/support/io/ArrayBucket.java 2008-08-17
10:12:46 UTC (rev 21948)
@@ -11,28 +11,36 @@
import freenet.support.api.Bucket;
/**
- * A bucket that stores data in the memory.
- *
- * @author oskar
+ * A bucket that stores data in RAM
*/
public class ArrayBucket implements Bucket {
private final ArrayList<byte[]> data;
private final String name;
private volatile boolean readOnly;
-
- public ArrayBucket() {
- this("ArrayBucket");
+
+ /** The maximum size of the bucket; -1 means no maxSize */
+ private final long maxSize;
+ private long size;
+
+ public ArrayBucket(long maxSize) {
+ this("ArrayBucket", maxSize);
}
+
+ public ArrayBucket(byte[] finalData) {
+ this(finalData, finalData.length);
+ setReadOnly();
+ }
- public ArrayBucket(byte[] initdata) {
- this("ArrayBucket");
+ public ArrayBucket(byte[] initdata, long maxSize) {
+ this("ArrayBucket", -1);
data.add(initdata);
}
- ArrayBucket(String name) {
+ ArrayBucket(String name, long maxSize) {
data = new ArrayList<byte[]>();
this.name = name;
+ this.maxSize = maxSize;
}
public synchronized OutputStream getOutputStream() throws IOException {
@@ -65,12 +73,7 @@
}
public synchronized long size() {
- long currentSize = 0;
-
- for(byte[] buf : data)
- currentSize += buf.length;
-
- return currentSize;
+ return size;
}
public String getName() {
@@ -87,13 +90,23 @@
@Override
public synchronized void write(byte b[], int off, int len) {
if(readOnly) throw new IllegalStateException("Read
only");
+ long sizeIfWritten = size + len;
+ if(maxSize > -1 && maxSize < sizeIfWritten)
+ throw new IllegalArgumentException("The maxSize
of the bucket is "+maxSize+
+ " and writing "+len+ " bytes to it
would make it oversize!");
super.write(b, off, len);
+ size = sizeIfWritten;
}
@Override
public synchronized void write(int b) {
if(readOnly) throw new IllegalStateException("Read
only");
+ long sizeIfWritten = size + 1;
+ if(maxSize > -1 && maxSize < sizeIfWritten)
+ throw new IllegalArgumentException("The maxSize
of the bucket is "+maxSize+
+ " and writing 1 byte to it would make
it oversize!");
super.write(b);
+ size = sizeIfWritten;
}
@Override
@@ -126,12 +139,12 @@
return -1;
}
}
- int i = in.read();
- if (i == -1) {
+ int x = in.read();
+ if (x == -1) {
in = null;
return priv_read();
} else {
- return i;
+ return x;
}
}
@@ -153,12 +166,12 @@
return -1;
}
}
- int i = in.read(b, off, len);
- if (i == -1) {
+ int x = in.read(b, off, len);
+ if (x == -1) {
in = null;
return priv_read(b, off, len);
} else {
- return i;
+ return x;
}
}
@@ -185,14 +198,16 @@
}
public synchronized void free() {
+ readOnly = true;
data.clear();
+ size = 0;
// Not much else we can do.
}
public synchronized byte[] toByteArray() {
long sz = size();
- int size = (int)sz;
- byte[] buf = new byte[size];
+ int bufSize = (int)sz;
+ byte[] buf = new byte[bufSize];
int index = 0;
for(Iterator i=data.iterator();i.hasNext();) {
byte[] obuf = (byte[]) i.next();