Path: blob/master/src/java.base/share/classes/sun/nio/ch/AsynchronousFileChannelImpl.java
41159 views
/*1* Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.nio.ch;2627import java.nio.ByteBuffer;28import java.nio.channels.*;29import java.util.concurrent.ExecutorService;30import java.util.concurrent.Future;31import java.util.concurrent.locks.*;32import java.io.FileDescriptor;33import java.io.IOException;3435/**36* Base implementation of AsynchronousFileChannel.37*/3839abstract class AsynchronousFileChannelImpl40extends AsynchronousFileChannel41{42// close support43protected final ReadWriteLock closeLock = new ReentrantReadWriteLock();44protected volatile boolean closed;4546// file descriptor47protected final FileDescriptor fdObj;4849// indicates if open for reading/writing50protected final boolean reading;51protected final boolean writing;5253// associated Executor54protected final ExecutorService executor;5556protected AsynchronousFileChannelImpl(FileDescriptor fdObj,57boolean reading,58boolean writing,59ExecutorService executor)60{61this.fdObj = fdObj;62this.reading = reading;63this.writing = writing;64this.executor = executor;65}6667final ExecutorService executor() {68return executor;69}7071@Override72public final boolean isOpen() {73return !closed;74}7576/**77* Marks the beginning of an I/O operation.78*79* @throws ClosedChannelException If channel is closed80*/81protected final void begin() throws IOException {82closeLock.readLock().lock();83if (closed)84throw new ClosedChannelException();85}8687/**88* Marks the end of an I/O operation.89*/90protected final void end() {91closeLock.readLock().unlock();92}9394/**95* Marks end of I/O operation96*/97protected final void end(boolean completed) throws IOException {98end();99if (!completed && !isOpen())100throw new AsynchronousCloseException();101}102103// -- file locking --104105abstract <A> Future<FileLock> implLock(long position,106long size,107boolean shared,108A attachment,109CompletionHandler<FileLock,? super A> handler);110111@Override112public final Future<FileLock> lock(long position,113long size,114boolean shared)115116{117return implLock(position, size, shared, null, null);118}119120@Override121public final <A> void lock(long position,122long size,123boolean shared,124A attachment,125CompletionHandler<FileLock,? super A> handler)126{127if (handler == null)128throw new NullPointerException("'handler' is null");129implLock(position, size, shared, attachment, handler);130}131132private volatile FileLockTable fileLockTable;133134final void ensureFileLockTableInitialized() throws IOException {135if (fileLockTable == null) {136synchronized (this) {137if (fileLockTable == null) {138fileLockTable = new FileLockTable(this, fdObj);139}140}141}142}143144final void invalidateAllLocks() throws IOException {145if (fileLockTable != null) {146for (FileLock fl: fileLockTable.removeAll()) {147synchronized (fl) {148if (fl.isValid()) {149FileLockImpl fli = (FileLockImpl)fl;150implRelease(fli);151fli.invalidate();152}153}154}155}156}157158/**159* Adds region to lock table160*/161protected final FileLockImpl addToFileLockTable(long position, long size, boolean shared) {162final FileLockImpl fli;163try {164// like begin() but returns null instead of exception165closeLock.readLock().lock();166if (closed)167return null;168169try {170ensureFileLockTableInitialized();171} catch (IOException x) {172// should not happen173throw new AssertionError(x);174}175fli = new FileLockImpl(this, position, size, shared);176// may throw OverlappedFileLockException177fileLockTable.add(fli);178} finally {179end();180}181return fli;182}183184protected final void removeFromFileLockTable(FileLockImpl fli) {185fileLockTable.remove(fli);186}187188/**189* Releases the given file lock.190*/191protected abstract void implRelease(FileLockImpl fli) throws IOException;192193/**194* Invoked by FileLockImpl to release the given file lock and remove it195* from the lock table.196*/197final void release(FileLockImpl fli) throws IOException {198try {199begin();200implRelease(fli);201removeFromFileLockTable(fli);202} finally {203end();204}205}206207208// -- reading and writing --209210abstract <A> Future<Integer> implRead(ByteBuffer dst,211long position,212A attachment,213CompletionHandler<Integer,? super A> handler);214215@Override216public final Future<Integer> read(ByteBuffer dst, long position) {217return implRead(dst, position, null, null);218}219220@Override221public final <A> void read(ByteBuffer dst,222long position,223A attachment,224CompletionHandler<Integer,? super A> handler)225{226if (handler == null)227throw new NullPointerException("'handler' is null");228implRead(dst, position, attachment, handler);229}230231abstract <A> Future<Integer> implWrite(ByteBuffer src,232long position,233A attachment,234CompletionHandler<Integer,? super A> handler);235236237@Override238public final Future<Integer> write(ByteBuffer src, long position) {239return implWrite(src, position, null, null);240}241242@Override243public final <A> void write(ByteBuffer src,244long position,245A attachment,246CompletionHandler<Integer,? super A> handler)247{248if (handler == null)249throw new NullPointerException("'handler' is null");250implWrite(src, position, attachment, handler);251}252}253254255