Author: nextgens
Date: 2008-08-26 13:30:17 +0000 (Tue, 26 Aug 2008)
New Revision: 22173
Modified:
trunk/freenet/src/freenet/support/io/ArrayBucket.java
Log:
ArrayBucket: switch back to a sane synchronization scheme
Modified: trunk/freenet/src/freenet/support/io/ArrayBucket.java
===================================================================
--- trunk/freenet/src/freenet/support/io/ArrayBucket.java 2008-08-26
13:14:49 UTC (rev 22172)
+++ trunk/freenet/src/freenet/support/io/ArrayBucket.java 2008-08-26
13:30:17 UTC (rev 22173)
@@ -18,9 +18,8 @@
private final ArrayList<byte[]> data;
private final String name;
private volatile boolean readOnly;
-
private long size;
-
+
/** Create a new immutable ArrayBucket with the data provided and a
name*/
public ArrayBucket(String name, byte[] initdata) {
this(name);
@@ -28,8 +27,8 @@
this.size = initdata.length;
setReadOnly();
}
-
- /** Create a new immutable ArrayBucket with the data provided */
+
+ /** Create a new immutable ArrayBucket with the data provided */
public ArrayBucket(byte[] initdata) {
this();
data.add(initdata);
@@ -49,7 +48,8 @@
}
public synchronized OutputStream getOutputStream() throws IOException {
- if(readOnly) throw new IOException("Read only");
+ if(readOnly)
+ throw new IOException("Read only");
return new ArrayBucketOutputStream();
}
@@ -60,7 +60,7 @@
@Override
public synchronized String toString() {
StringBuffer s = new StringBuffer(250);
- for (Iterator i = data.iterator(); i.hasNext();) {
+ for(Iterator i = data.iterator(); i.hasNext();) {
byte[] b = (byte[]) i.next();
s.append(new String(b));
}
@@ -71,7 +71,7 @@
OutputStream out = new ArrayBucketOutputStream();
int i;
byte[] b = new byte[8 * 1024];
- while ((i = in.read(b)) != -1) {
+ while((i = in.read(b)) != -1) {
out.write(b, 0, i);
}
out.close();
@@ -81,44 +81,57 @@
return size;
}
- public String getName() {
+ public synchronized String getName() {
return name;
}
private class ArrayBucketOutputStream extends ByteArrayOutputStream {
+
private boolean hasBeenClosed = false;
-
+
public ArrayBucketOutputStream() {
super();
}
-
+
@Override
- public synchronized void write(byte b[], int off, int len) {
- if(readOnly) throw new IllegalStateException("Read
only");
- if(hasBeenClosed) throw new IllegalStateException("Has
been closed!");
- super.write(b, off, len);
- size +=len;
+ public void write(byte b[], int off, int len) {
+ synchronized(ArrayBucket.this) {
+ if(readOnly)
+ throw new IllegalStateException("Read
only");
+ if(hasBeenClosed)
+ throw new IllegalStateException("Has
been closed!");
+ super.write(b, off, len);
+ size += len;
+ }
}
-
+
@Override
- public synchronized void write(int b) {
- if(readOnly) throw new IllegalStateException("Read
only");
- if(hasBeenClosed) throw new IllegalStateException("Has
been closed!");
- super.write(b);
- size++;
+ public void write(int b) {
+ synchronized(ArrayBucket.this) {
+ if(readOnly)
+ throw new IllegalStateException("Read
only");
+ if(hasBeenClosed)
+ throw new IllegalStateException("Has
been closed!");
+ super.write(b);
+ size++;
+ }
}
@Override
- public synchronized void close() throws IOException {
- if(hasBeenClosed) return;
- hasBeenClosed = true;
- data.add(super.toByteArray());
- if(readOnly) throw new IOException("Read only");
+ public void close() throws IOException {
+ synchronized(ArrayBucket.this) {
+ if(hasBeenClosed)
+ return;
+ hasBeenClosed = true;
+ data.add(super.toByteArray());
+ if(readOnly)
+ throw new IOException("Read only");
+ }
}
}
private class ArrayBucketInputStream extends InputStream {
-
+
private final Iterator<byte[]> i;
private ByteArrayInputStream in;
private boolean hasBeenClosed = false;
@@ -127,74 +140,80 @@
i = data.iterator();
}
- public synchronized int read() throws IOException {
- return priv_read();
+ public int read() throws IOException {
+ synchronized(ArrayBucket.this) {
+ return priv_read();
+ }
}
- private synchronized int priv_read() throws IOException {
- if(hasBeenClosed) throw new IOException("Has been
closed!");
- if (in == null) {
- if (i.hasNext()) {
+ private int priv_read() throws IOException {
+ if(hasBeenClosed)
+ throw new IOException("Has been closed!");
+ if(in == null)
+ if(i.hasNext())
in = new ByteArrayInputStream(i.next());
- } else {
+ else
return -1;
- }
- }
int x = in.read();
- if (x == -1) {
+ if(x == -1) {
in = null;
return priv_read();
- } else {
+ } else
return x;
- }
}
@Override
- public synchronized int read(byte[] b) throws IOException {
- return priv_read(b, 0, b.length);
+ public int read(byte[] b) throws IOException {
+ synchronized(ArrayBucket.this) {
+ return priv_read(b, 0, b.length);
+ }
}
@Override
- public synchronized int read(byte[] b, int off, int len) throws
IOException {
- return priv_read(b, off, len);
+ public int read(byte[] b, int off, int len) throws IOException {
+ synchronized(ArrayBucket.this) {
+ return priv_read(b, off, len);
+ }
}
- private synchronized int priv_read(byte[] b, int off, int len)
throws IOException {
- if(hasBeenClosed) throw new IOException("Has been
closed!");
- if (in == null) {
- if (i.hasNext()) {
+ private int priv_read(byte[] b, int off, int len) throws
IOException {
+ if(hasBeenClosed)
+ throw new IOException("Has been closed!");
+ if(in == null)
+ if(i.hasNext())
in = new ByteArrayInputStream(i.next());
- } else {
+ else
return -1;
- }
- }
int x = in.read(b, off, len);
- if (x == -1) {
+ if(x == -1) {
in = null;
return priv_read(b, off, len);
- } else {
+ } else
return x;
- }
}
@Override
- public synchronized int available() throws IOException {
- if(hasBeenClosed) throw new IOException("Has been
closed!");
- if (in == null) {
- if (i.hasNext()) {
- in = new ByteArrayInputStream(i.next());
- } else {
- return 0;
- }
+ public int available() throws IOException {
+ synchronized(ArrayBucket.this) {
+ if(hasBeenClosed)
+ throw new IOException("Has been
closed!");
+ if(in == null)
+ if(i.hasNext())
+ in = new
ByteArrayInputStream(i.next());
+ else
+ return 0;
+ return in.available();
}
- return in.available();
}
@Override
- public synchronized void close() throws IOException {
- if(hasBeenClosed) return;
- hasBeenClosed = true;
- Closer.close(in);
+ public void close() throws IOException {
+ synchronized(ArrayBucket.this) {
+ if(hasBeenClosed)
+ return;
+ hasBeenClosed = true;
+ Closer.close(in);
+ }
}
}
@@ -209,12 +228,12 @@
public synchronized void free() {
readOnly = true;
data.clear();
- // Not much else we can do.
+ // Not much else we can do.
}
public synchronized byte[] toByteArray() {
long sz = size();
- int bufSize = (int)sz;
+ int bufSize = (int) sz;
byte[] buf = new byte[bufSize];
int index = 0;
for(byte[] obuf : data) {