/* * Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code 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 General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
/** * Defines static methods to create {@link java.nio.channels.Channel channels}. * * <p> Unless otherwise specified, passing a {@code null} argument to any of the * methods defined here will cause a {@code NullPointerException} to be thrown. * * @since 11
*/
/** * An object used to coordinate the closing of a selectable channel created * by {@link Channels#readWriteSelectableChannel readWriteSelectableChannel}. * * @since 11
*/ publicinterface SelectableChannelCloser {
/** * Closes a selectable channel. * * <p> This method is invoked by the channel's close method in order to * perform the actual work of closing the channel. This method is only * invoked if the channel has not yet been closed, and it is never * invoked more than once by the channel's close implementation. * * <p> An implementation of this method must arrange for any other * thread that is blocked in an I/O operation upon the channel to return * immediately, either by throwing an exception or by returning normally. * If the channel is {@link SelectableChannel#isRegistered registered} * with one or more {@link java.nio.channels.Selector Selector}s then * the file descriptor should not be released until the {@link * #implReleaseChannel implReleaseChannel} method is invoked. </p> * * @param sc * The selectable channel * * @throws IOException * If an I/O error occurs while closing the file descriptor * * @see java.nio.channels.spi.AbstractInterruptibleChannel#implCloseChannel
*/ void implCloseChannel(SelectableChannel sc) throws IOException;
/** * Release the file descriptor and any resources for a selectable * channel that closed while registered with one or more {@link * java.nio.channels.Selector Selector}s. * * <p> This method is for cases where a channel is closed when * {@link java.nio.channels.SelectableChannel#isRegistered registered} * with one or more {@code Selector}s. A channel may remain registered * for some time after it is closed. This method is invoked when the * channel is eventually deregistered from the last {@code Selector} * that it was registered with. It is invoked at most once. * * @apiNote This method is invoked while synchronized on the selector * and its selected-key set. Great care must be taken to avoid deadlocks * with other threads that also synchronize on these objects. * * @param sc * The closed selectable channel * * @throws IOException * If an I/O error occurs * * @see java.nio.channels.spi.AbstractSelector#deregister
*/ void implReleaseChannel(SelectableChannel sc) throws IOException;
}
/** * Creates a selectable channel to a file descriptor that supports an * {@link SelectableChannel#validOps() operation-set} of * {@link SelectionKey#OP_READ OP_READ} and * {@link SelectionKey#OP_WRITE OP_WRITE}. The selectable channel will be * created by the default {@link SelectorProvider}. * * <p> The given file descriptor is a socket or resource that can be * multiplexed by a {@link java.nio.channels.Selector} for read and write * readiness. Great care is required to coordinate direct use of the file * descriptor with the use of the selectable channel. In particular, * changing the blocking mode or closing the file descriptor without careful * coordination will result in unspecified and unsafe side effects. The * given {@link SelectableChannelCloser SelectableChannelCloser} is invoked to * close the file descriptor and to coordinate the closing when the channel * is registered with a {@code Selector}. </p> * * <p> If there is a security manager set then its * {@link SecurityManager#checkRead(FileDescriptor) checkRead} and * {@link SecurityManager#checkWrite(FileDescriptor) checkWrite} methods * are invoked to check that the caller has permission to both read from and * write to the file descriptor. </p> * * @implNote This method throws {@code UnsupportedOperationException} if * the default {@code SelectorProvider} is not the JDK built-in implementation. * * @param fd * The file descriptor * @param closer * The object to close the channel * * @return The selectable channel * * @throws IllegalArgumentException * If the file descriptor is not {@link FileDescriptor#valid() valid} * @throws SecurityException * If denied by the security manager
*/ publicstatic SelectableChannel readWriteSelectableChannel(FileDescriptor fd,
SelectableChannelCloser closer) {
Objects.requireNonNull(closer); if (!fd.valid()) thrownew IllegalArgumentException("file descriptor is not valid");
@SuppressWarnings("removal")
SecurityManager sm = System.getSecurityManager(); if (sm != null) {
sm.checkRead(fd);
sm.checkWrite(fd);
}
privateboolean translateReadyOps(int ops, int initialOps,
SelectionKeyImpl ski) { int intOps = ski.nioInterestOps(); int oldOps = ski.nioReadyOps(); int newOps = initialOps;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.