public class MultiReferenceStorage extends java.lang.Object implements Storage
A wrapper around another Storage that also maintains a reference
counter. The inner storage gets deleted only if the reference counter reaches
zero.
Reference counting is used to delete the storage when it is no longer needed. So, any users of this class should note:
addReference() should be called when the storage is shared.addReference() should ensure that
delete() is called once and only once.Storage instance passed into
MultiReferenceStorage(Storage) may lead to miscounting and premature
deletion| Modifier and Type | Field and Description |
|---|---|
private int |
referenceCounter |
private Storage |
storage |
| Constructor and Description |
|---|
MultiReferenceStorage(Storage storage)
Creates a new
MultiReferenceStorage instance for the given
back-end. |
| Modifier and Type | Method and Description |
|---|---|
void |
addReference()
Increments the reference counter.
|
private boolean |
decrementCounter()
Synchronized decrement of reference count.
|
void |
delete()
Decrements the reference counter and deletes the inner
Storage object if the reference counter reaches zero. |
java.io.InputStream |
getInputStream()
Returns the input stream of the inner
Storage object. |
private void |
incrementCounter()
Synchronized increment of reference count.
|
private final Storage storage
private int referenceCounter
public MultiReferenceStorage(Storage storage)
MultiReferenceStorage instance for the given
back-end. The reference counter is initially set to one so the caller
does not have to call addReference() after this constructor.storage - storage back-end that should be reference counted.java.lang.IllegalArgumentException - when storage is nullpublic void addReference()
java.lang.IllegalStateException - if the reference counter is zero which implies that the
backing storage has already been deleted.public void delete()
Storage object if the reference counter reaches zero.
A client that holds a reference to this object must make sure not to invoke this method a second time.
public java.io.InputStream getInputStream()
throws java.io.IOException
Storage object.getInputStream in interface Storagejava.io.IOException - if an I/O error occurs.private void incrementCounter()
java.lang.IllegalStateException - when counter is already zeroprivate boolean decrementCounter()
java.lang.IllegalStateException - when counter is already zero