org.modeshape.jcr.value.binary.SharedLockingInputStream Maven / Gradle / Ivy
/*
* ModeShape (http://www.modeshape.org)
* See the COPYRIGHT.txt file distributed with this work for information
* regarding copyright ownership. Some portions may be licensed
* to Red Hat, Inc. under one or more contributor license agreements.
* See the AUTHORS.txt file in the distribution for a full listing of
* individual contributors.
*
* ModeShape is free software. Unless otherwise indicated, all code in ModeShape
* is licensed to you under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* ModeShape is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
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 - 2025 Weber Informatics LLC | Privacy Policy