Author: zothar
Date: 2006-07-23 15:50:20 +0000 (Sun, 23 Jul 2006)
New Revision: 9733
Modified:
trunk/freenet/src/freenet/support/io/FileBucket.java
trunk/freenet/src/freenet/support/io/SpyInputStream.java
trunk/freenet/src/freenet/support/io/SpyOutputStream.java
trunk/freenet/src/freenet/support/io/TempFileBucket.java
Log:
Locking consistency in support/io/
Modified: trunk/freenet/src/freenet/support/io/FileBucket.java
===================================================================
--- trunk/freenet/src/freenet/support/io/FileBucket.java 2006-07-23
15:20:47 UTC (rev 9732)
+++ trunk/freenet/src/freenet/support/io/FileBucket.java 2006-07-23
15:50:20 UTC (rev 9733)
@@ -67,6 +67,7 @@
* directory.
*/
public FileBucket(RandomSource random) {
+ // **FIXME**/TODO: locking on tempDir needs to be checked by a
Java guru for consistency
file =
new File(
tempDir,
@@ -117,7 +118,7 @@
return new FileBucketOutputStream(s, streamNumber);
}
- protected void resetLength() {
+ protected synchronized void resetLength() {
length = 0;
}
@@ -175,7 +176,7 @@
}
}
- public InputStream getInputStream() throws IOException {
+ public synchronized InputStream getInputStream() throws IOException {
return file.exists()
? (InputStream) new FileBucketInputStream(file)
: (InputStream) new NullInputStream();
@@ -184,18 +185,18 @@
/**
* @return the name of the file.
*/
- public String getName() {
+ public synchronized String getName() {
return file.getName();
}
- public long size() {
+ public synchronized long size() {
return length;
}
/**
* Returns the file object this buckets data is kept in.
*/
- public File getFile() {
+ public synchronized File getFile() {
return file;
}
@@ -203,7 +204,7 @@
* Actually delete the underlying file. Called by finalizer, will not be
* called twice. But length must still be valid when calling it.
*/
- protected void deleteFile() {
+ protected synchronized void deleteFile() {
file.delete();
}
@@ -216,7 +217,7 @@
* Return directory used for temp files.
*/
public final synchronized static String getTempDir() {
- return tempDir;
+ return tempDir; // **FIXME**/TODO: locking on tempDir needs to
be checked by a Java guru for consistency
}
/**
@@ -230,7 +231,7 @@
throw new IllegalArgumentException(
"Bad Temp Directory: " + dir.getAbsolutePath());
}
- tempDir = dirName;
+ tempDir = dirName; // **FIXME**/TODO: locking on tempDir needs
to be checked by a Java guru for consistency
}
// determine the temp directory in one of several ways
@@ -292,11 +293,11 @@
}
}
- public boolean isReadOnly() {
+ public synchronized boolean isReadOnly() {
return readOnly;
}
- public void setReadOnly() {
+ public synchronized void setReadOnly() {
readOnly = true;
}
@@ -305,11 +306,11 @@
* Note that if you have already set delete file on exit, there is
little that you
* can do to recover it! Delete file on finalize, on the other hand, is
reversible.
*/
- public void dontDeleteOnFinalize() {
+ public synchronized void dontDeleteOnFinalize() {
deleteOnFinalize = false;
}
- public Bucket[] split(int splitSize) {
+ public synchronized Bucket[] split(int splitSize) {
if(length > ((long)Integer.MAX_VALUE) * splitSize)
throw new IllegalArgumentException("Way too big!:
"+length+" for "+splitSize);
int bucketCount = (int) (length / splitSize);
@@ -328,7 +329,7 @@
free(false);
}
- public void free(boolean forceFree) {
+ public synchronized void free(boolean forceFree) {
if ((deleteOnFree || forceFree) && file.exists()) {
Logger.debug(this,
"Deleting bucket " + file.getName());
@@ -339,11 +340,11 @@
}
}
- public String toString() {
+ public synchronized String toString() {
return super.toString()+":"+file.getPath();
}
- public SimpleFieldSet toFieldSet() {
+ public synchronized SimpleFieldSet toFieldSet() {
if(deleteOnFinalize) return null;
SimpleFieldSet fs = new SimpleFieldSet(true);
fs.put("Type", "FileBucket");
Modified: trunk/freenet/src/freenet/support/io/SpyInputStream.java
===================================================================
--- trunk/freenet/src/freenet/support/io/SpyInputStream.java 2006-07-23
15:20:47 UTC (rev 9732)
+++ trunk/freenet/src/freenet/support/io/SpyInputStream.java 2006-07-23
15:50:20 UTC (rev 9733)
@@ -31,9 +31,11 @@
}
private final void checkValid() throws IOException {
- if (tfb.isReleased()) {
- throw new IOException(
- "Attempt to use a released TempFileBucket: " +
prefix);
+ synchronized (tfb) {
+ if (tfb.isReleased()) {
+ throw new IOException(
+ "Attempt to use a released
TempFileBucket: " + prefix);
+ }
}
}
Modified: trunk/freenet/src/freenet/support/io/SpyOutputStream.java
===================================================================
--- trunk/freenet/src/freenet/support/io/SpyOutputStream.java 2006-07-23
15:20:47 UTC (rev 9732)
+++ trunk/freenet/src/freenet/support/io/SpyOutputStream.java 2006-07-23
15:50:20 UTC (rev 9733)
@@ -27,9 +27,11 @@
}
private final void checkValid() throws IOException {
- if (tfb.isReleased()) {
- throw new IOException(
- "Attempt to use a released TempFileBucket: " +
prefix);
+ synchronized (tfb) {
+ if (tfb.isReleased()) {
+ throw new IOException(
+ "Attempt to use a released
TempFileBucket: " + prefix);
+ }
}
}
Modified: trunk/freenet/src/freenet/support/io/TempFileBucket.java
===================================================================
--- trunk/freenet/src/freenet/support/io/TempFileBucket.java 2006-07-23
15:20:47 UTC (rev 9732)
+++ trunk/freenet/src/freenet/support/io/TempFileBucket.java 2006-07-23
15:50:20 UTC (rev 9733)
@@ -39,7 +39,9 @@
float factor)
throws IOException {
super(f, false, true, true, true);
- logDebug = Logger.shouldLog(Logger.DEBUG, this);
+ synchronized(this) {
+ logDebug = Logger.shouldLog(Logger.DEBUG, this);
+ }
if (minAlloc > 0)
this.minAlloc = minAlloc;
else
@@ -58,10 +60,12 @@
long x = startLength <= 0 ? minAlloc : startLength;
hook.createFile(x);
this.fakeLength = x;
- if (logDebug)
- Logger.debug(
- this,
- "Initializing TempFileBucket(" + f + "," + hook
+ ")");
+ synchronized(this) {
+ if (logDebug)
+ Logger.debug(
+ this,
+ "Initializing TempFileBucket(" + f +
"," + hook + ")");
+ }
}
/**
@@ -70,7 +74,7 @@
* @return The realInputStream value
* @exception IOException Description of the Exception
*/
- InputStream getRealInputStream() throws IOException {
+ synchronized InputStream getRealInputStream() throws IOException {
if (released)
throw new IllegalStateException(
"Trying to getInputStream on " + "released
TempFileBucket!");
@@ -92,11 +96,13 @@
* @exception IOException Description of the Exception
*/
OutputStream getRealOutputStream() throws IOException {
- if (logDebug)
- Logger.debug(
- this,
- "getRealOutputStream() for " + file,
- new Exception("debug"));
+ synchronized(this) {
+ if (logDebug)
+ Logger.debug(
+ this,
+ "getRealOutputStream() for " + file,
+ new Exception("debug"));
+ }
return super.getOutputStream();
}
@@ -256,7 +262,7 @@
protected Vector streams = new Vector();
private boolean released;
- protected FileBucketOutputStream newFileBucketOutputStream(
+ protected synchronized FileBucketOutputStream newFileBucketOutputStream(
String s,
boolean append,
long restartCount)
@@ -270,7 +276,7 @@
return super.newFileBucketOutputStream(s, restartCount);
}
- protected void deleteFile() {
+ protected synchronized void deleteFile() {
if (logDebug)
Logger.debug(this, "Deleting " + file);
file.delete();
@@ -290,7 +296,7 @@
}
}
- protected final void getLengthSynchronized(long len) throws IOException
{
+ protected final synchronized void getLengthSynchronized(long len)
throws IOException {
// Core.logger.log(this, "getLengthSynchronized("+len+
// "); fakeLength = "+fakeLength,
Logger.DEBUG);
long l = fakeLength;
@@ -324,7 +330,7 @@
fakeLength = l;
}
- public String toString(){
+ public synchronized String toString(){
return "TempFileBucket (File:
'"+getFile().getAbsolutePath()+"', streams: "+streams.size()+", hook:
"+hook+")";
}