893N/A/*
2362N/A * Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved.
893N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
893N/A *
893N/A * This code is free software; you can redistribute it and/or modify it
893N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
893N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
893N/A *
893N/A * This code is distributed in the hope that it will be useful, but WITHOUT
893N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
893N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
893N/A * version 2 for more details (a copy is included in the LICENSE file that
893N/A * accompanied this code).
893N/A *
893N/A * You should have received a copy of the GNU General Public License version
893N/A * 2 along with this work; if not, write to the Free Software Foundation,
893N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
893N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
893N/A */
893N/A
893N/Apackage sun.nio.ch;
893N/A
1580N/Aimport java.nio.ByteBuffer;
893N/Aimport java.nio.channels.*;
893N/Aimport java.util.concurrent.ExecutorService;
1580N/Aimport java.util.concurrent.Future;
893N/Aimport java.util.concurrent.locks.*;
893N/Aimport java.io.FileDescriptor;
893N/Aimport java.io.IOException;
893N/A
893N/A/**
893N/A * Base implementation of AsynchronousFileChannel.
893N/A */
893N/A
893N/Aabstract class AsynchronousFileChannelImpl
893N/A extends AsynchronousFileChannel
893N/A{
893N/A // close support
893N/A protected final ReadWriteLock closeLock = new ReentrantReadWriteLock();
893N/A protected volatile boolean closed;
893N/A
893N/A // file descriptor
893N/A protected final FileDescriptor fdObj;
893N/A
893N/A // indicates if open for reading/writing
893N/A protected final boolean reading;
893N/A protected final boolean writing;
893N/A
893N/A // associated Executor
893N/A protected final ExecutorService executor;
893N/A
893N/A protected AsynchronousFileChannelImpl(FileDescriptor fdObj,
893N/A boolean reading,
893N/A boolean writing,
893N/A ExecutorService executor)
893N/A {
893N/A this.fdObj = fdObj;
893N/A this.reading = reading;
893N/A this.writing = writing;
893N/A this.executor = executor;
893N/A }
893N/A
893N/A final ExecutorService executor() {
893N/A return executor;
893N/A }
893N/A
893N/A @Override
893N/A public final boolean isOpen() {
893N/A return !closed;
893N/A }
893N/A
893N/A /**
893N/A * Marks the beginning of an I/O operation.
893N/A *
893N/A * @throws ClosedChannelException If channel is closed
893N/A */
893N/A protected final void begin() throws IOException {
893N/A closeLock.readLock().lock();
893N/A if (closed)
893N/A throw new ClosedChannelException();
893N/A }
893N/A
893N/A /**
893N/A * Marks the end of an I/O operation.
893N/A */
893N/A protected final void end() {
893N/A closeLock.readLock().unlock();
893N/A }
893N/A
893N/A /**
893N/A * Marks end of I/O operation
893N/A */
893N/A protected final void end(boolean completed) throws IOException {
893N/A end();
893N/A if (!completed && !isOpen())
893N/A throw new AsynchronousCloseException();
893N/A }
893N/A
893N/A // -- file locking --
893N/A
1580N/A abstract <A> Future<FileLock> implLock(long position,
1580N/A long size,
1580N/A boolean shared,
1580N/A A attachment,
1580N/A CompletionHandler<FileLock,? super A> handler);
1580N/A
1580N/A @Override
1580N/A public final Future<FileLock> lock(long position,
1580N/A long size,
1580N/A boolean shared)
1580N/A
1580N/A {
1580N/A return implLock(position, size, shared, null, null);
1580N/A }
1580N/A
1580N/A @Override
1580N/A public final <A> void lock(long position,
1580N/A long size,
1580N/A boolean shared,
1580N/A A attachment,
1580N/A CompletionHandler<FileLock,? super A> handler)
1580N/A {
1580N/A if (handler == null)
1580N/A throw new NullPointerException("'handler' is null");
1580N/A implLock(position, size, shared, attachment, handler);
1580N/A }
1580N/A
893N/A private volatile FileLockTable fileLockTable;
893N/A
893N/A final void ensureFileLockTableInitialized() throws IOException {
893N/A if (fileLockTable == null) {
893N/A synchronized (this) {
893N/A if (fileLockTable == null) {
893N/A fileLockTable = FileLockTable.newSharedFileLockTable(this, fdObj);
893N/A }
893N/A }
893N/A }
893N/A }
893N/A
1109N/A final void invalidateAllLocks() throws IOException {
893N/A if (fileLockTable != null) {
1109N/A for (FileLock fl: fileLockTable.removeAll()) {
1109N/A synchronized (fl) {
1109N/A if (fl.isValid()) {
1109N/A FileLockImpl fli = (FileLockImpl)fl;
1109N/A implRelease(fli);
1109N/A fli.invalidate();
893N/A }
1109N/A }
893N/A }
893N/A }
893N/A }
893N/A
893N/A /**
893N/A * Adds region to lock table
893N/A */
893N/A protected final FileLockImpl addToFileLockTable(long position, long size, boolean shared) {
893N/A final FileLockImpl fli;
893N/A try {
893N/A // like begin() but returns null instead of exception
893N/A closeLock.readLock().lock();
893N/A if (closed)
893N/A return null;
893N/A
893N/A try {
893N/A ensureFileLockTableInitialized();
893N/A } catch (IOException x) {
893N/A // should not happen
893N/A throw new AssertionError(x);
893N/A }
893N/A fli = new FileLockImpl(this, position, size, shared);
893N/A // may throw OverlappedFileLockException
893N/A fileLockTable.add(fli);
893N/A } finally {
893N/A end();
893N/A }
893N/A return fli;
893N/A }
893N/A
893N/A protected final void removeFromFileLockTable(FileLockImpl fli) {
893N/A fileLockTable.remove(fli);
893N/A }
893N/A
893N/A /**
1109N/A * Releases the given file lock.
1109N/A */
1109N/A protected abstract void implRelease(FileLockImpl fli) throws IOException;
1109N/A
1109N/A /**
1109N/A * Invoked by FileLockImpl to release the given file lock and remove it
1109N/A * from the lock table.
893N/A */
1109N/A final void release(FileLockImpl fli) throws IOException {
1109N/A try {
1109N/A begin();
1109N/A implRelease(fli);
1109N/A removeFromFileLockTable(fli);
1109N/A } finally {
1109N/A end();
1109N/A }
1109N/A }
1580N/A
1580N/A
1580N/A // -- reading and writing --
1580N/A
1580N/A abstract <A> Future<Integer> implRead(ByteBuffer dst,
1580N/A long position,
1580N/A A attachment,
1580N/A CompletionHandler<Integer,? super A> handler);
1580N/A
1580N/A @Override
1580N/A public final Future<Integer> read(ByteBuffer dst, long position) {
1580N/A return implRead(dst, position, null, null);
1580N/A }
1580N/A
1580N/A @Override
1580N/A public final <A> void read(ByteBuffer dst,
1580N/A long position,
1580N/A A attachment,
1580N/A CompletionHandler<Integer,? super A> handler)
1580N/A {
1580N/A if (handler == null)
1580N/A throw new NullPointerException("'handler' is null");
1580N/A implRead(dst, position, attachment, handler);
1580N/A }
1580N/A
1580N/A abstract <A> Future<Integer> implWrite(ByteBuffer src,
1580N/A long position,
1580N/A A attachment,
1580N/A CompletionHandler<Integer,? super A> handler);
1580N/A
1580N/A
1580N/A @Override
1580N/A public final Future<Integer> write(ByteBuffer src, long position) {
1580N/A return implWrite(src, position, null, null);
1580N/A }
1580N/A
1580N/A @Override
1580N/A public final <A> void write(ByteBuffer src,
1580N/A long position,
1580N/A A attachment,
1580N/A CompletionHandler<Integer,? super A> handler)
1580N/A {
1580N/A if (handler == null)
1580N/A throw new NullPointerException("'handler' is null");
1580N/A implWrite(src, position, attachment, handler);
1580N/A }
893N/A}