Index: jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/AbstractQValue.java
===================================================================
--- jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/AbstractQValue.java (revision 1482639)
+++ jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/AbstractQValue.java (working copy)
@@ -402,6 +402,7 @@
}
public void dispose() {
+ AbstractQValue.this.discard();
}
};
}
Index: jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/QValueFactoryImpl.java
===================================================================
--- jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/QValueFactoryImpl.java (revision 1482639)
+++ jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/QValueFactoryImpl.java (working copy)
@@ -22,6 +22,7 @@
import org.apache.jackrabbit.spi.QValueFactory;
import org.apache.jackrabbit.util.TransientFileFactory;
+import javax.jcr.Binary;
import javax.jcr.PropertyType;
import javax.jcr.RepositoryException;
import java.io.ByteArrayInputStream;
@@ -114,11 +115,11 @@
private transient File file;
/**
- * flag indicating if this instance represents a temporary value
+ * reference counter indicating if this instance represents a temporary value
* whose dynamically allocated resources can be explicitly freed on
* {@link #discard()}.
*/
- private transient boolean temp;
+ private transient int temp;
/**
* Buffer for small-sized data
@@ -201,7 +202,7 @@
// init vars
file = spoolFile;
- this.temp = temp;
+ this.temp = temp ? 1 : 0;
// buffer is EMPTY_BYTE_ARRAY (default value)
}
@@ -217,7 +218,7 @@
buffer = bytes;
file = null;
// this instance is not backed by a temporarily allocated buffer
- temp = false;
+ temp = 0;
}
/**
@@ -238,7 +239,7 @@
// this instance is backed by a 'real' file
this.file = file;
// this instance is not backed by temporarily allocated resource/buffer
- temp = false;
+ temp = 0;
// buffer is EMPTY_BYTE_ARRAY (default value)
}
@@ -300,6 +301,14 @@
throw new UnsupportedOperationException();
}
+ @Override
+ public Binary getBinary() throws RepositoryException {
+ if (temp > 0) {
+ temp++;
+ }
+ return super.getBinary();
+ }
+
/**
* Frees temporarily allocated resources such as temporary file, buffer, etc.
* If this BinaryQValue is backed by a persistent resource
@@ -308,7 +317,7 @@
*/
@Override
public void discard() {
- if (!temp) {
+ if (temp == 0 || --temp > 0) {
// do nothing if this instance is not backed by temporarily
// allocated resource/buffer
return;
@@ -316,6 +325,7 @@
if (file != null) {
// this instance is backed by a temp file
file.delete();
+ file = null;
} else if (buffer != null) {
// this instance is backed by an in-memory buffer
buffer = EMPTY_BYTE_ARRAY;
@@ -416,7 +426,7 @@
out.close();
}
// deserialized value is always temp
- temp = true;
+ temp = 1;
}
}
}
Index: jackrabbit-spi2dav/src/main/java/org/apache/jackrabbit/spi2davex/QValueFactoryImpl.java
===================================================================
--- jackrabbit-spi2dav/src/main/java/org/apache/jackrabbit/spi2davex/QValueFactoryImpl.java (revision 1482639)
+++ jackrabbit-spi2dav/src/main/java/org/apache/jackrabbit/spi2davex/QValueFactoryImpl.java (working copy)
@@ -36,6 +36,7 @@
import org.w3c.dom.Element;
import org.xml.sax.SAXException;
+import javax.jcr.Binary;
import javax.jcr.PropertyType;
import javax.jcr.RepositoryException;
import javax.jcr.Value;
@@ -137,11 +138,11 @@
private transient File file;
/**
- * flag indicating if this instance represents a temporary value
+ * reference counter indicating if this instance represents a temporary value
* whose dynamically allocated resources can be explicitly freed on
* {@link #discard()}.
*/
- private transient boolean temp;
+ private transient int temp;
/**
* Buffer for small-sized data
@@ -227,12 +228,12 @@
if (spoolFile == null && buffer == null) {
// input stream was empty -> initialize an empty binary value
- this.temp = false;
+ this.temp = 0;
buffer = EMPTY_BYTE_ARRAY;
} else {
// init vars
file = spoolFile;
- this.temp = temp;
+ this.temp = temp ? 1 : 0;
}
initialized = true;
}
@@ -308,6 +309,14 @@
throw new UnsupportedOperationException();
}
+ @Override
+ public Binary getBinary() throws RepositoryException {
+ if (temp > 0) {
+ temp++;
+ }
+ return super.getBinary();
+ }
+
/**
* Frees temporarily allocated resources such as temporary file, buffer, etc.
* If this BinaryQValue is backed by a persistent resource
@@ -316,18 +325,7 @@
*/
@Override
public void discard() {
- if (!temp) {
- // do nothing if this instance is not backed by temporarily
- // allocated resource/buffer
- return;
- }
- if (file != null) {
- // this instance is backed by a temp file
- file.delete();
- } else if (buffer != null) {
- // this instance is backed by an in-memory buffer
- buffer = EMPTY_BYTE_ARRAY;
- }
+ reset();
}
/**
@@ -339,7 +337,7 @@
* @see QValue#discard()
*/
public void reset() {
- if (!temp) {
+ if (temp == 0 || --temp > 0) {
// do nothing if this instance is not backed by temporarily
// allocated resource/buffer
return;
@@ -540,7 +538,7 @@
out.close();
}
// deserialized value is always temp
- temp = true;
+ temp = 1;
}
//---------------------------------------------------------< Target >---