0N/A/*
4963N/A * Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/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
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/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.
0N/A */
0N/A
0N/Apackage sun.nio.ch;
0N/A
0N/Aimport java.io.*;
0N/Aimport java.nio.ByteBuffer;
0N/Aimport java.nio.channels.*;
0N/Aimport java.nio.channels.spi.*;
0N/A
0N/A
0N/Aclass SourceChannelImpl
0N/A extends Pipe.SourceChannel
0N/A implements SelChImpl
0N/A{
0N/A
0N/A // Used to make native read and write calls
0N/A private static NativeDispatcher nd;
0N/A
0N/A // The file descriptor associated with this channel
0N/A FileDescriptor fd;
0N/A
0N/A // fd value needed for dev/poll. This value will remain valid
0N/A // even after the value in the file descriptor object has been set to -1
0N/A int fdVal;
0N/A
0N/A // ID of native thread doing read, for signalling
0N/A private volatile long thread = 0;
0N/A
0N/A // Lock held by current reading thread
0N/A private final Object lock = new Object();
0N/A
0N/A // Lock held by any thread that modifies the state fields declared below
0N/A // DO NOT invoke a blocking I/O operation while holding this lock!
0N/A private final Object stateLock = new Object();
0N/A
0N/A // -- The following fields are protected by stateLock
0N/A
0N/A // Channel state
0N/A private static final int ST_UNINITIALIZED = -1;
0N/A private static final int ST_INUSE = 0;
0N/A private static final int ST_KILLED = 1;
0N/A private volatile int state = ST_UNINITIALIZED;
0N/A
0N/A // -- End of fields protected by stateLock
0N/A
0N/A
0N/A public FileDescriptor getFD() {
0N/A return fd;
0N/A }
0N/A
0N/A public int getFDVal() {
0N/A return fdVal;
0N/A }
0N/A
0N/A SourceChannelImpl(SelectorProvider sp, FileDescriptor fd) {
0N/A super(sp);
0N/A this.fd = fd;
0N/A this.fdVal = IOUtil.fdVal(fd);
0N/A this.state = ST_INUSE;
0N/A }
0N/A
0N/A protected void implCloseSelectableChannel() throws IOException {
0N/A synchronized (stateLock) {
4963N/A if (state != ST_KILLED)
4963N/A nd.preClose(fd);
0N/A long th = thread;
0N/A if (th != 0)
0N/A NativeThread.signal(th);
0N/A if (!isRegistered())
0N/A kill();
0N/A }
0N/A }
0N/A
0N/A public void kill() throws IOException {
0N/A synchronized (stateLock) {
0N/A if (state == ST_KILLED)
0N/A return;
0N/A if (state == ST_UNINITIALIZED) {
0N/A state = ST_KILLED;
0N/A return;
0N/A }
0N/A assert !isOpen() && !isRegistered();
0N/A nd.close(fd);
0N/A state = ST_KILLED;
0N/A }
0N/A }
0N/A
0N/A protected void implConfigureBlocking(boolean block) throws IOException {
0N/A IOUtil.configureBlocking(fd, block);
0N/A }
0N/A
0N/A public boolean translateReadyOps(int ops, int initialOps,
0N/A SelectionKeyImpl sk) {
0N/A int intOps = sk.nioInterestOps(); // Do this just once, it synchronizes
0N/A int oldOps = sk.nioReadyOps();
0N/A int newOps = initialOps;
0N/A
0N/A if ((ops & PollArrayWrapper.POLLNVAL) != 0)
0N/A throw new Error("POLLNVAL detected");
0N/A
0N/A if ((ops & (PollArrayWrapper.POLLERR
0N/A | PollArrayWrapper.POLLHUP)) != 0) {
0N/A newOps = intOps;
0N/A sk.nioReadyOps(newOps);
0N/A return (newOps & ~oldOps) != 0;
0N/A }
0N/A
0N/A if (((ops & PollArrayWrapper.POLLIN) != 0) &&
0N/A ((intOps & SelectionKey.OP_READ) != 0))
0N/A newOps |= SelectionKey.OP_READ;
0N/A
0N/A sk.nioReadyOps(newOps);
0N/A return (newOps & ~oldOps) != 0;
0N/A }
0N/A
0N/A public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl sk) {
0N/A return translateReadyOps(ops, sk.nioReadyOps(), sk);
0N/A }
0N/A
0N/A public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl sk) {
0N/A return translateReadyOps(ops, 0, sk);
0N/A }
0N/A
0N/A public void translateAndSetInterestOps(int ops, SelectionKeyImpl sk) {
0N/A if (ops == SelectionKey.OP_READ)
0N/A ops = PollArrayWrapper.POLLIN;
0N/A sk.selector.putEventOps(sk, ops);
0N/A }
0N/A
0N/A private void ensureOpen() throws IOException {
0N/A if (!isOpen())
0N/A throw new ClosedChannelException();
0N/A }
0N/A
0N/A public int read(ByteBuffer dst) throws IOException {
0N/A ensureOpen();
0N/A synchronized (lock) {
0N/A int n = 0;
0N/A try {
0N/A begin();
0N/A if (!isOpen())
0N/A return 0;
0N/A thread = NativeThread.current();
0N/A do {
6050N/A n = IOUtil.read(fd, dst, -1, nd);
0N/A } while ((n == IOStatus.INTERRUPTED) && isOpen());
0N/A return IOStatus.normalize(n);
0N/A } finally {
0N/A thread = 0;
0N/A end((n > 0) || (n == IOStatus.UNAVAILABLE));
0N/A assert IOStatus.check(n);
0N/A }
0N/A }
0N/A }
0N/A
0N/A public long read(ByteBuffer[] dsts, int offset, int length)
0N/A throws IOException
0N/A {
0N/A if ((offset < 0) || (length < 0) || (offset > dsts.length - length))
0N/A throw new IndexOutOfBoundsException();
0N/A return read(Util.subsequence(dsts, offset, length));
0N/A }
0N/A
0N/A public long read(ByteBuffer[] dsts) throws IOException {
0N/A if (dsts == null)
0N/A throw new NullPointerException();
0N/A ensureOpen();
0N/A synchronized (lock) {
0N/A long n = 0;
0N/A try {
0N/A begin();
0N/A if (!isOpen())
0N/A return 0;
0N/A thread = NativeThread.current();
0N/A do {
0N/A n = IOUtil.read(fd, dsts, nd);
0N/A } while ((n == IOStatus.INTERRUPTED) && isOpen());
0N/A return IOStatus.normalize(n);
0N/A } finally {
0N/A thread = 0;
0N/A end((n > 0) || (n == IOStatus.UNAVAILABLE));
0N/A assert IOStatus.check(n);
0N/A }
0N/A }
0N/A }
0N/A
0N/A static {
0N/A Util.load();
893N/A nd = new FileDispatcherImpl();
0N/A }
0N/A
0N/A}