org.modeshape.jcr.value.binary.SharedLockingInputStream Maven / Gradle / Ivy
/*
* ModeShape (http://www.modeshape.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.modeshape.jcr.value.binary;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.Callable;
import java.util.concurrent.locks.Lock;
import org.modeshape.jcr.value.BinaryKey;
/**
* A {@link InputStream} implementation around a file that creates a shared lock when reading the file, ensuring the file is not
* changed by other writers in this or other JVM processes. The stream automatically closes itself and releases the lock when
* {@link #close() closed explicitly} or if there are any errors or exceptions while reading. Caution: be very careful when
* working with this class, as any open without close operations can produce "readLocks" which do not get released, blocking any
* potential subsequent writes.
*/
public final class SharedLockingInputStream extends InputStream {
protected final BinaryKey key;
protected final File file;
protected final NamedLocks lockManager;
protected InputStream stream;
protected Lock processLock;
protected FileLocks.WrappedLock fileLock;
protected boolean eofReached;
/**
* Create a self-closing, (shared) locking {@link InputStream} to read the content of the supplied {@link File file}.
*
* @param key the binary key; may not be null
* @param file the file that is to be read; may not be null
* @param lockManager the manager of the locks, from which a read lock is to be obtained; may be null if no read lock is
* needed
*/
public SharedLockingInputStream( BinaryKey key,
File file,
NamedLocks lockManager ) {
assert key != null;
assert file != null;
this.key = key;
this.file = file;
this.lockManager = lockManager;
}
protected void open() throws IOException {
doOperation(new Callable
© 2015 - 2024 Weber Informatics LLC | Privacy Policy