--- /dev/null 2006-09-14 17:47:38.000000000 -0700 +++ gnu/java/nio/FileChannelImpl.java 2006-09-13 18:02:02.000000000 -0700 @@ -0,0 +1,572 @@ +/* FileChannelImpl.java -- + Copyright (C) 2002, 2004, 2005, 2006 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath 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 for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio; + +import gnu.classpath.Configuration; +import gnu.java.nio.FileLockImpl; +import gnu.java.nio.VMChannel; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.MappedByteBuffer; +import java.nio.channels.ClosedChannelException; +import java.nio.channels.FileChannel; +import java.nio.channels.FileLock; +import java.nio.channels.NonReadableChannelException; +import java.nio.channels.NonWritableChannelException; +import java.nio.channels.ReadableByteChannel; +import java.nio.channels.WritableByteChannel; + +/** + * This file is not user visible ! + * But alas, Java does not have a concept of friendly packages + * so this class is public. + * Instances of this class are created by invoking getChannel + * Upon a Input/Output/RandomAccessFile object. + */ +public final class FileChannelImpl extends FileChannel +{ + // These are mode values for open(). + public static final int READ = 1; + public static final int WRITE = 2; + public static final int APPEND = 4; + + // EXCL is used only when making a temp file. + public static final int EXCL = 8; + public static final int SYNC = 16; + public static final int DSYNC = 32; + + public static final FileChannelImpl in; + public static final FileChannelImpl out; + public static final FileChannelImpl err; + + //private static native void init(); + + static + { + if (Configuration.INIT_LOAD_LIBRARY) + { + System.loadLibrary("javanio"); + } + + //init(); + + FileChannelImpl ch = null; + try + { + ch = new FileChannelImpl(VMChannel.getStdin(), READ); + } + catch (IOException ioe) + { + throw new Error(ioe); + } + in = ch; + + ch = null; + try + { + ch = new FileChannelImpl(VMChannel.getStdout(), WRITE); + } + catch (IOException ioe) + { + throw new Error(ioe); + } + out = ch; + + ch = null; + try + { + ch = new FileChannelImpl(VMChannel.getStderr(), WRITE); + } + catch (IOException ioe) + { + throw new Error(ioe); + } + err = ch; + } + + /** + * This is the actual native file descriptor value + */ + private VMChannel ch; + + private int mode; + + final String description; + + /* Open a file. MODE is a combination of the above mode flags. */ + /* This is a static factory method, so that VM implementors can decide + * substitute subclasses of FileChannelImpl. */ + public static FileChannelImpl create(File file, int mode) + throws IOException + { + return new FileChannelImpl(file, mode); + } + + private FileChannelImpl(File file, int mode) + throws IOException + { + String path = file.getPath(); + description = path; + this.mode = mode; + this.ch = new VMChannel(); + ch.openFile(path, mode); + + // First open the file and then check if it is a a directory + // to avoid race condition. + if (file.isDirectory()) + { + try + { + close(); + } + catch (IOException e) + { + /* ignore it */ + } + + throw new FileNotFoundException(description + " is a directory"); + } + } + + /** + * Constructor for default channels in, out and err. + * + * Used by init() (native code). + * + * @param fd the file descriptor (0, 1, 2 for stdin, stdout, stderr). + * + * @param mode READ or WRITE + */ + FileChannelImpl (VMChannel ch, int mode) + { + this.mode = mode; + this.description = "descriptor(" + ch.getState() + ")"; + this.ch = ch; + } + + public int available() throws IOException + { + return ch.available(); + } + + private long implPosition() throws IOException + { + return ch.position(); + } + + private void seek(long newPosition) throws IOException + { + ch.seek(newPosition); + } + + private void implTruncate(long size) throws IOException + { + ch.truncate(size); + } + + public void unlock(long pos, long len) throws IOException + { + ch.unlock(pos, len); + } + + public long size () throws IOException + { + return ch.size(); + } + + protected void implCloseChannel() throws IOException + { + ch.close(); + } + + /** + * Makes sure the Channel is properly closed. + */ + protected void finalize() throws IOException + { + if (ch.getState().isValid()) + close(); + } + + public int read (ByteBuffer dst) throws IOException + { + return ch.read(dst); + } + + public int read (ByteBuffer dst, long position) + throws IOException + { + if (position < 0) + throw new IllegalArgumentException ("position: " + position); + long oldPosition = implPosition (); + position (position); + int result = read(dst); + position (oldPosition); + + return result; + } + + public int read() throws IOException + { + return ch.read(); + } + + public long read (ByteBuffer[] dsts, int offset, int length) + throws IOException + { + return ch.readScattering(dsts, offset, length); + } + + public int write (ByteBuffer src) throws IOException + { + return ch.write(src); + } + + public int write (ByteBuffer src, long position) + throws IOException + { + if (position < 0) + throw new IllegalArgumentException ("position: " + position); + + if (!isOpen ()) + throw new ClosedChannelException (); + + if ((mode & WRITE) == 0) + throw new NonWritableChannelException (); + + int result; + long oldPosition; + + oldPosition = implPosition (); + seek (position); + result = write(src); + seek (oldPosition); + + return result; + } + + public void write (int b) throws IOException + { + ch.write(b); + } + + public long write(ByteBuffer[] srcs, int offset, int length) + throws IOException + { + return ch.writeGathering(srcs, offset, length); + } + + public MappedByteBuffer map (FileChannel.MapMode mode, + long position, long size) + throws IOException + { + char nmode = 0; + if (mode == MapMode.READ_ONLY) + { + nmode = 'r'; + if ((this.mode & READ) == 0) + throw new NonReadableChannelException(); + } + else if (mode == MapMode.READ_WRITE || mode == MapMode.PRIVATE) + { + nmode = mode == MapMode.READ_WRITE ? '+' : 'c'; + if ((this.mode & WRITE) != WRITE) + throw new NonWritableChannelException(); + if ((this.mode & READ) != READ) + throw new NonReadableChannelException(); + } + else + throw new IllegalArgumentException ("mode: " + mode); + + if (position < 0 || size < 0 || size > Integer.MAX_VALUE) + throw new IllegalArgumentException ("position: " + position + + ", size: " + size); + return ch.map(nmode, position, (int) size); + } + + /** + * msync with the disk + */ + public void force (boolean metaData) throws IOException + { + if (!isOpen ()) + throw new ClosedChannelException (); + + ch.flush(metaData); + } + + // like transferTo, but with a count of less than 2Gbytes + private int smallTransferTo (long position, int count, + WritableByteChannel target) + throws IOException + { + ByteBuffer buffer; + try + { + // Try to use a mapped buffer if we can. If this fails for + // any reason we'll fall back to using a ByteBuffer. + buffer = map (MapMode.READ_ONLY, position, count); + } + catch (IOException e) + { + buffer = ByteBuffer.allocate (count); + read (buffer, position); + buffer.flip(); + } + + return target.write (buffer); + } + + public long transferTo (long position, long count, + WritableByteChannel target) + throws IOException + { + if (position < 0 + || count < 0) + throw new IllegalArgumentException ("position: " + position + + ", count: " + count); + + if (!isOpen ()) + throw new ClosedChannelException (); + + if ((mode & READ) == 0) + throw new NonReadableChannelException (); + + final int pageSize = 65536; + long total = 0; + + while (count > 0) + { + int transferred + = smallTransferTo (position, (int)Math.min (count, pageSize), + target); + if (transferred < 0) + break; + total += transferred; + position += transferred; + count -= transferred; + } + + return total; + } + + // like transferFrom, but with a count of less than 2Gbytes + private int smallTransferFrom (ReadableByteChannel src, long position, + int count) + throws IOException + { + ByteBuffer buffer = null; + + if (src instanceof FileChannel) + { + try + { + // Try to use a mapped buffer if we can. If this fails + // for any reason we'll fall back to using a ByteBuffer. + buffer = ((FileChannel)src).map (MapMode.READ_ONLY, position, + count); + } + catch (IOException e) + { + } + } + + if (buffer == null) + { + buffer = ByteBuffer.allocate ((int) count); + src.read (buffer); + buffer.flip(); + } + + return write (buffer, position); + } + + public long transferFrom (ReadableByteChannel src, long position, + long count) + throws IOException + { + if (position < 0 + || count < 0) + throw new IllegalArgumentException ("position: " + position + + ", count: " + count); + + if (!isOpen ()) + throw new ClosedChannelException (); + + if ((mode & WRITE) == 0) + throw new NonWritableChannelException (); + + final int pageSize = 65536; + long total = 0; + + while (count > 0) + { + int transferred = smallTransferFrom (src, position, + (int)Math.min (count, pageSize)); + if (transferred < 0) + break; + total += transferred; + position += transferred; + count -= transferred; + } + + return total; + } + + // Shared sanity checks between lock and tryLock methods. + private void lockCheck(long position, long size, boolean shared) + throws IOException + { + if (position < 0 + || size < 0) + throw new IllegalArgumentException ("position: " + position + + ", size: " + size); + + if (!isOpen ()) + throw new ClosedChannelException(); + + if (shared && ((mode & READ) == 0)) + throw new NonReadableChannelException(); + + if (!shared && ((mode & WRITE) == 0)) + throw new NonWritableChannelException(); + } + + public FileLock tryLock (long position, long size, boolean shared) + throws IOException + { + lockCheck(position, size, shared); + + boolean completed = false; + try + { + begin(); + boolean lockable = ch.lock(position, size, shared, false); + completed = true; + return (lockable + ? new FileLockImpl(this, position, size, shared) + : null); + } + finally + { + end(completed); + } + } + + public FileLock lock (long position, long size, boolean shared) + throws IOException + { + lockCheck(position, size, shared); + + boolean completed = false; + try + { + boolean lockable = ch.lock(position, size, shared, true); + completed = true; + return (lockable + ? new FileLockImpl(this, position, size, shared) + : null); + } + finally + { + end(completed); + } + } + + public long position () + throws IOException + { + if (!isOpen ()) + throw new ClosedChannelException (); + + return implPosition (); + } + + public FileChannel position (long newPosition) + throws IOException + { + if (newPosition < 0) + throw new IllegalArgumentException ("newPosition: " + newPosition); + + if (!isOpen ()) + throw new ClosedChannelException (); + + // FIXME note semantics if seeking beyond eof. + // We should seek lazily - only on a write. + seek (newPosition); + return this; + } + + public FileChannel truncate (long size) + throws IOException + { + if (size < 0) + throw new IllegalArgumentException ("size: " + size); + + if (!isOpen ()) + throw new ClosedChannelException (); + + if ((mode & WRITE) == 0) + throw new NonWritableChannelException (); + + if (size < size ()) + implTruncate (size); + + return this; + } + + public String toString() + { + return (super.toString() + + "[ fd: " + ch.getState() + + "; mode: " + Integer.toOctalString(mode) + + "; " + description + " ]"); + } + + /** + * @return The native file descriptor. + * / + public int getNativeFD() + { + return fd; + }*/ +} --- /dev/null 2006-09-14 17:47:38.000000000 -0700 +++ gnu/java/nio/KqueueSelectionKeyImpl.java 2006-08-20 14:07:37.000000000 -0700 @@ -0,0 +1,175 @@ +/* KqueueSelectionKeyImpl.java -- selection key for kqueue/kevent. + Copyright (C) 2006 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath 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 for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio; + + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.channels.SelectableChannel; +import java.nio.channels.SelectionKey; +import java.nio.channels.Selector; + +/** + * @author Casey Marshall (csm@gnu.org) + */ +public class KqueueSelectionKeyImpl extends SelectionKey +{ + int interestOps; + int readyOps; + ByteBuffer nstate; + boolean valid; + int key; + boolean readEverEnabled = false; + boolean writeEverEnabled = false; + + /** The selector we were created for. */ + private final KqueueSelectorImpl selector; + + /** The channel we are attached to. */ + private final SelectableChannel channel; + + private final VMChannelOwner natChannel; + + public KqueueSelectionKeyImpl(KqueueSelectorImpl selector, + SelectableChannel channel) + { + this.selector = selector; + this.channel = channel; + natChannel = (VMChannelOwner) channel; + interestOps = 0; + readyOps = 0; + valid = true; + } + + /* (non-Javadoc) + * @see java.nio.channels.SelectionKey#cancel() + */ + //@Override + public void cancel() + { + selector.doCancel(this); + valid = false; + } + + /* (non-Javadoc) + * @see java.nio.channels.SelectionKey#channel() + */ + //@Override + public SelectableChannel channel() + { + return channel; + } + + /* (non-Javadoc) + * @see java.nio.channels.SelectionKey#interestOps() + */ + //@Override + public int interestOps() + { + return interestOps; + } + + /* (non-Javadoc) + * @see java.nio.channels.SelectionKey#interestOps(int) + */ + //@Override + public SelectionKey interestOps(int ops) + { + if (!isValid()) + throw new IllegalStateException(); + if ((ops & ~channel.validOps()) != 0) + throw new IllegalArgumentException(); + this.interestOps = ops; + try + { + selector.updateOps(this, + natChannel.getVMChannel().getState().getNativeFD(), + false); + } + catch (IOException ioe) + { + throw new IllegalStateException("channel is invalid"); + } + return this; + } + + /* (non-Javadoc) + * @see java.nio.channels.SelectionKey#isValid() + */ + //@Override + public boolean isValid() + { + return valid && selector.isOpen(); + } + + /* (non-Javadoc) + * @see java.nio.channels.SelectionKey#readyOps() + */ + //@Override + public int readyOps() + { + return readyOps; + } + + /* (non-Javadoc) + * @see java.nio.channels.SelectionKey#selector() + */ + //@Override + public Selector selector() + { + return selector; + } + + public String toString() + { + if (!isValid()) + return super.toString() + " [ <> ]"; + return super.toString() + " [ interest ops: {" + + ((interestOps & OP_ACCEPT) != 0 ? " OP_ACCEPT" : "") + + ((interestOps & OP_CONNECT) != 0 ? " OP_CONNECT" : "") + + ((interestOps & OP_READ) != 0 ? " OP_READ" : "") + + ((interestOps & OP_WRITE) != 0 ? " OP_WRITE" : "") + + " }; ready ops: {" + + ((readyOps & OP_ACCEPT) != 0 ? " OP_ACCEPT" : "") + + ((readyOps & OP_CONNECT) != 0 ? " OP_CONNECT" : "") + + ((readyOps & OP_READ) != 0 ? " OP_READ" : "") + + ((readyOps & OP_WRITE) != 0 ? " OP_WRITE" : "") + + " } ]"; + } +} --- /dev/null 2006-09-14 17:47:38.000000000 -0700 +++ gnu/java/nio/KqueueSelectorImpl.java 2006-09-13 16:07:33.000000000 -0700 @@ -0,0 +1,430 @@ +/* KqueueSelectorImpl.java -- Selector for systems with kqueue event notification. + Copyright (C) 2006 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath 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 for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio; + + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.nio.channels.ClosedSelectorException; +import java.nio.channels.SelectionKey; +import java.nio.channels.Selector; +import java.nio.channels.spi.AbstractSelectableChannel; +import java.nio.channels.spi.AbstractSelector; +import java.nio.channels.spi.SelectorProvider; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + +/** + * A {@link Selector} implementation that uses the kqueue + * event notification facility. + * + * @author Casey Marshall (csm@gnu.org) + */ +public class KqueueSelectorImpl extends AbstractSelector +{ + private static final int sizeof_struct_kevent; + + static + { + try + { + System.loadLibrary("javanio"); + } + catch (Exception x) + { + x.printStackTrace(); + } + sizeof_struct_kevent = sizeof_struct_kevent(); + } + + /** + * Tell if kqueue-based selectors are supported on this system. + * + * @return True if this system has kqueue support, and support for it was + * compiled in to Classpath. + */ + public static native boolean kqueue_supported(); + + /* Our native file descriptor. */ + private int kq; + + private HashMap/**/ keys; + private HashSet/**/ selected; + private HashSet/**/ cancelled; + private Thread blockedThread; + + public KqueueSelectorImpl(SelectorProvider provider) throws IOException + { + super(provider); + kq = implOpen(); + keys = new HashMap/**/(); + cancelled = new HashSet(); + } + + protected void implCloseSelector() throws IOException + { + implClose(kq); + kq = -1; + } + + /* (non-Javadoc) + * @see java.nio.channels.Selector#keys() + */ + public Set keys() + { + if (!isOpen()) + throw new ClosedSelectorException(); + + return new HashSet(keys.values()); + } + + /* (non-Javadoc) + * @see java.nio.channels.Selector#select() + */ + public int select() throws IOException + { + return doSelect(-1); + } + + /* (non-Javadoc) + * @see java.nio.channels.Selector#select(long) + */ + public int select(long timeout) throws IOException + { + if (timeout == 0) + timeout = -1; + return doSelect(timeout); + } + + /* (non-Javadoc) + * @see java.nio.channels.Selector#selectedKeys() + */ + public Set selectedKeys() + { + if (!isOpen()) + throw new ClosedSelectorException(); + + return selected; + } + + /* (non-Javadoc) + * @see java.nio.channels.Selector#selectNow() + */ + public int selectNow() throws IOException + { + return doSelect(0); + } + + /* (non-Javadoc) + * @see java.nio.channels.Selector#wakeup() + */ + public Selector wakeup() + { + if (blockedThread != null) + blockedThread.interrupt(); + return this; + } + + synchronized int doSelect(long timeout) throws IOException + { + // FIXME -- I'm unclear on how we should synchronize this; and how to + // handle cancelled keys. + for (Iterator it = cancelled.iterator(); it.hasNext(); ) + { + KqueueSelectionKeyImpl key = (KqueueSelectionKeyImpl) it.next(); + updateOps(key, 0, true); + } + int events_size = 0; + for (Iterator it = keys.values().iterator(); it.hasNext(); ) + { + KqueueSelectionKeyImpl key = (KqueueSelectionKeyImpl) it.next(); + if ((key.interestOps & SelectionKey.OP_ACCEPT) != 0 + || (key.interestOps & SelectionKey.OP_READ) != 0) + key.readEverEnabled = true; + if ((key.interestOps & SelectionKey.OP_CONNECT) != 0 + || (key.interestOps & SelectionKey.OP_WRITE) != 0) + key.writeEverEnabled = true; + + if (key.readEverEnabled) + events_size += sizeof_struct_kevent; + if (key.writeEverEnabled) + events_size += sizeof_struct_kevent; + } + + // We handle native events a little strangely here; per selection key, + // we allocate enough space for two struct kevents, the first in the + // list will be our EVFILT_READ filter, the second our EVFILT_WRITE + // one. If only one of the two needs enabling, though, we don't want + // to pass the other to kevent, because that would result in spurious + // events. We can break down our handling as follows: + // + // - READ enabled, WRITE never enabled. We pass only the first structure + // to kevent. + // - WRITE enabled, READ never enabled. Likewise, but only pass the + // second structure. + // - READ and WRITE enabled. Pass both. + // - READ enabled, WRITE enabled in the past. Pass both, with the + // first structure's flag set to EV_ADD or EV_ENABLE, and the second + // with EV_DISABLE. It seems OK to keep sending events with the + // EV_DISABLE flag. + // - WRITE enabled, READ enabled in the past. Likewise, but flipped. + // + // We handle these states with the readEverEnabled and writeEverEnabled + // flags of selection keys; they start off as false, and become true + // the first time we select() with READ or WRITE enabled. They never + // become false. + ByteBuffer events = ByteBuffer.allocateDirect(events_size); + + for (Iterator it = keys.entrySet().iterator(); it.hasNext(); ) + { + Map.Entry e = (Map.Entry) it.next(); + KqueueSelectionKeyImpl key = (KqueueSelectionKeyImpl) e.getValue(); + + if (key.readEverEnabled) + events.put((ByteBuffer) key.nstate.duplicate().limit + (sizeof_struct_kevent)); + if (key.writeEverEnabled) + events.put((ByteBuffer) key.nstate.duplicate().position + (sizeof_struct_kevent).limit(2 * sizeof_struct_kevent)); + } + events.rewind(); + + //System.out.println("dump of keys to select:"); + //dump_selection_keys(events.duplicate()); + + blockedThread = Thread.currentThread(); + if (blockedThread.isInterrupted()) + timeout = 0; + final int n = kevent(kq, events, events_size / sizeof_struct_kevent, + timeout); + Thread.interrupted(); + + //System.out.println("dump of keys selected:"); + //dump_selection_keys((ByteBuffer) events.duplicate().limit(n * sizeof_struct_kevent)); + + selected = new HashSet/**/(n); + int x = 0; + for (int i = 0; i < n; i++) + { + events.position(x).limit(x + sizeof_struct_kevent); + x += sizeof_struct_kevent; + int y = fetch_key(events.slice()); + KqueueSelectionKeyImpl key = + (KqueueSelectionKeyImpl) keys.get(new Integer(y)); + key.readyOps = ready_ops(events.slice(), key.interestOps); + selected.add(key); + } + for (Iterator it = cancelled.iterator(); it.hasNext(); ) + { + KqueueSelectionKeyImpl key = (KqueueSelectionKeyImpl) it.next(); + keys.remove(new Integer(key.key)); + it.remove(); + } + + return selected.size(); + } + + protected SelectionKey register(AbstractSelectableChannel channel, + int interestOps, + Object attachment) + { + int native_fd = -1; + try + { + if (channel instanceof VMChannelOwner) + native_fd = ((VMChannelOwner) channel).getVMChannel() + .getState().getNativeFD(); + else + throw new IllegalArgumentException("cannot handle channel type " + + channel.getClass().getName()); + } + catch (IOException ioe) + { + throw new IllegalArgumentException("channel is closed or invalid"); + } + + KqueueSelectionKeyImpl result = new KqueueSelectionKeyImpl(this, channel); + result.interestOps = interestOps; + result.attach(attachment); + int k = System.identityHashCode(result); + while (keys.containsKey(new Integer(k))) + k++; + result.key = k; + keys.put(new Integer(k), result); + result.nstate = ByteBuffer.allocateDirect(2 * sizeof_struct_kevent); + updateOps(result, native_fd, false); + return result; + } + + synchronized void updateOps(KqueueSelectionKeyImpl key) + { + updateOps(key, 0, false); + } + + synchronized void updateOps(KqueueSelectionKeyImpl key, int fd, boolean delete) + { + //System.out.println(">> updating kqueue selection key:"); + //dump_selection_keys(key.nstate.duplicate()); + //System.out.println("<<"); + kevent_set(key.nstate, fd, key.interestOps, key.key, delete); + //System.out.println(">> updated kqueue selection key:"); + //dump_selection_keys(key.nstate.duplicate()); + //System.out.println("<<"); + } + + synchronized void doCancel(KqueueSelectionKeyImpl key) + { + cancelled.add(key); + } + + private void dump_selection_keys(ByteBuffer keys) + { + // WARNING! This method is not guaranteed to be portable! This works + // on darwin/x86, but the sizeof and offsetof these fields may be + // different on other platforms! + int i = 0; + keys.order(ByteOrder.nativeOrder()); + while (keys.hasRemaining()) + { + System.out.println("struct kevent { ident: " + + Integer.toString(keys.getInt()) + + " filter: " + + Integer.toHexString(keys.getShort() & 0xFFFF) + + " flags: " + + Integer.toHexString(keys.getShort() & 0xFFFF) + + " fflags: " + + Integer.toHexString(keys.getInt()) + + " data: " + + Integer.toHexString(keys.getInt()) + + " udata: " + + Integer.toHexString(keys.getInt()) + + " }"); + } + } + + /** + * Return the size of a struct kevent on this system. + * + * @return The size of struct kevent. + */ + private static native int sizeof_struct_kevent(); + + /** + * Opens a kqueue descriptor. + * + * @return The new kqueue descriptor. + * @throws IOException If opening fails. + */ + private static native int implOpen() throws IOException; + + /** + * Closes the kqueue file descriptor. + * + * @param kq The kqueue file descriptor. + * @throws IOException + */ + private static native void implClose(int kq) throws IOException; + + /** + * Initialize the specified native state for the given interest ops. + * + * @param nstate The native state structures; in this buffer should be + * the struct kevents created for a key. + * @param fd The file descriptor. If 0, the native FD is unmodified. + * @param interestOps The operations to enable. + * @param key A unique key that will reference the associated key later. + * @param delete Set to true if this event should be deleted from the + * kqueue (if false, this event is added/updated). + */ + private static native void kevent_set(ByteBuffer nstate, int fd, int interestOps, + int key, boolean delete); + + /** + * Poll for events. The source events are stored in events, + * which is also where polled events will be placed. + * + * @param events The events to poll. This buffer is also the destination + * for events read from the queue. + * @param nevents The number of events to poll (that is, the number of + * events in the events buffer). + * @param timeout The timeout. A timeout of -1 returns immediately; a timeout + * of 0 waits indefinitely. + * @return The number of events read. + */ + private static native int kevent(int kq, ByteBuffer events, int nevents, + long timeout); + + /** + * Fetch a polled key from a native state buffer. For each kevent key we + * create, we put the native state info (one or more struct + * kevents) in that key's {@link KqueueSelectionKeyImpl#nstate} + * buffer, and place the pointer of the key in the udata field + * of that structure. This method fetches that pointer from the given + * buffer (assumed to be a struct kqueue) and returns it. + * + * @param nstate The buffer containing the struct kqueue to read. + * @return The key object. + */ + private static native int fetch_key(ByteBuffer nstate); + + /** + * Fetch the ready ops of the associated native state. That is, this + * inspects the first argument as a struct kevent, looking + * at its operation (the input is assumed to have been returned via a + * previous call to kevent), and translating that to the + * appropriate Java bit set, based on the second argument. + * + * @param nstate The native state. + * @param interestOps The enabled operations for the key. + * @return The bit set representing the ready operations. + */ + private static native int ready_ops(ByteBuffer nstate, int interestOps); + + /** + * Check if kevent returned EV_EOF for a selection key. + * + * @param nstate The native state. + * @return True if the kevent call returned EOF. + */ + private static native boolean check_eof(ByteBuffer nstate); +} --- /dev/null 2006-09-14 17:47:38.000000000 -0700 +++ gnu/java/nio/NIOSocketImpl.java 2006-08-20 14:20:45.000000000 -0700 @@ -0,0 +1,110 @@ +/* NIOSocketImpl.java -- subclass of PlainSocketImpl for NIO. + Copyright (C) 2006 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath 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 for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio; + +import gnu.java.net.PlainSocketImpl; + +import java.io.IOException; +import java.net.InetAddress; + +/** + * @author Casey Marshall (csm@gnu.org) + */ +public class NIOSocketImpl extends PlainSocketImpl +{ + + private final SocketChannelImpl channel; + + NIOSocketImpl(SocketChannelImpl channel) throws IOException + { + this.channel = channel; + impl.getState().setChannelFD(channel.getVMChannel().getState()); + } + + /* (non-Javadoc) + * @see java.net.SocketImpl#getInetAddress() + */ + //@Override + protected InetAddress getInetAddress() + { + try + { + return channel.getVMChannel().getPeerAddress().getAddress(); + } + catch (IOException ioe) + { + return null; + } + catch (NullPointerException npe) + { + // Socket is not connected yet. + return null; + } + } + + /* (non-Javadoc) + * @see java.net.SocketImpl#getPort() + */ + //@Override + protected int getPort() + { + try + { + return channel.getVMChannel().getPeerAddress().getPort(); + } + catch (IOException ioe) + { + return -1; + } + catch (NullPointerException npe) + { + // Socket is not connected yet. + return -1; + } + } + + /* (non-Javadoc) + * @see gnu.java.net.PlainSocketImpl#create(boolean) + */ + //@Override + protected synchronized void create(boolean stream) + { + // Ignored; the socket has already been created. + } +} --- /dev/null 2006-09-14 17:47:38.000000000 -0700 +++ gnu/java/nio/VMChannelOwner.java 2006-08-20 13:51:03.000000000 -0700 @@ -0,0 +1,60 @@ +/* NativeFD.java -- interface for Channels that have an underlying file descriptor. + Copyright (C) 2006 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath 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 for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio; + +import java.nio.channels.Channel; +import java.nio.channels.Selector; + +/** + * This interface is meant to be implemented by any {@link Channel} + * implementation we support that uses a platform-specific {@link VMChannel} + * at their core. This is primarily used by {@link Selector} implementations, + * for easier access to the native state. + * + * @author Casey Marshall (csm@gnu.org) + */ +interface VMChannelOwner +{ + /** + * Return the underlying platform-specific Channel instance. + * + * @return The platform channel object. + */ + VMChannel getVMChannel(); +} --- /dev/null 2006-09-14 17:47:38.000000000 -0700 +++ native/jni/java-nio/gnu_java_nio_KqueueSelectorImpl.c 2006-09-14 17:33:04.000000000 -0700 @@ -0,0 +1,361 @@ +/* gnu_java_nio_channel_KqueueSelectorImpl.c -- + Copyright (C) 2006 Free Software Foundation, Inc. + +This file is a part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or (at +your option) any later version. + +GNU Classpath 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 for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 +USA + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +#if HAVE_CONFIG_H +#include +#endif /* HAVE_CONFIG_H */ + +#include +#if HAVE_SYS_EVENT_H +#include +#endif /* HAVE_SYS_EVENT_H */ +#include +#include +#include +#include + +#include +#include + +#include + +#define KEY_OP_ACCEPT 16 +#define KEY_OP_CONNECT 8 +#define KEY_OP_READ 1 +#define KEY_OP_WRITE 4 + +/* XXX this requires -std=gnu99 or c99 */ +/* #ifdef TRACE_KQUEUE */ +/* #define TRACE(fmt, ...) fprintf (stderr, "%s: " fmt "\n", __FUNCTION__, __VA_ARGS__); */ +/* #else */ +/* #define TRACE(fmt, ...) */ +/* #endif */ + + +#define throw_not_supported(env) JCL_ThrowException (env, "java/lang/UnsupportedOperationException", "kqueue/kevent support not available") + + +/* + * Class: gnu_java_nio_KqueueSelectorImpl + * Method: kqueue_supported + * Signature: ()Z + */ +JNIEXPORT jboolean JNICALL +Java_gnu_java_nio_KqueueSelectorImpl_kqueue_1supported (JNIEnv *env __attribute__((unused)), + jclass clazz __attribute__((unused))) +{ +#if HAVE_KQUEUE && HAVE_KEVENT + return JNI_TRUE; +#else + return JNI_FALSE; +#endif /* HAVE_KQUEUE && HAVE_KEVENT */ +} + + +/* + * Class: gnu_java_nio_KqueueSelectorImpl + * Method: sizeof_struct_kevent + * Signature: ()I + */ +JNIEXPORT jint JNICALL +Java_gnu_java_nio_KqueueSelectorImpl_sizeof_1struct_1kevent +(JNIEnv *env __attribute__((unused)), jclass clazz __attribute__((unused))) +{ +#if HAVE_KQUEUE && HAVE_KEVENT +/* TRACE("return sizeof %lu", sizeof (struct kevent)); */ + return sizeof (struct kevent); +#else + throw_not_supported (env); + return -1; +#endif /* HAVE_KQUEUE && HAVE_KEVENT */ +} + + +/* + * Class: gnu_java_nio_KqueueSelectorImpl + * Method: implOpen + * Signature: ()I + */ +JNIEXPORT jint JNICALL +Java_gnu_java_nio_KqueueSelectorImpl_implOpen +(JNIEnv *env, jclass clazz __attribute__((unused))) +{ +#if HAVE_KQUEUE && HAVE_KEVENT + int kq = kqueue (); +/* TRACE("kqueue returns %d", kq); */ + if (kq == -1) + JCL_ThrowException (env, "java/io/IOException", strerror (errno)); + return kq; +#else + throw_not_supported (env); + return -1; +#endif +} + + +/* + * Class: gnu_java_nio_KqueueSelectorImpl + * Method: implClose + * Signature: (I)V + */ +JNIEXPORT void JNICALL +Java_gnu_java_nio_KqueueSelectorImpl_implClose (JNIEnv *env, + jclass clazz __attribute__((unused)), + jint kq) +{ +#if HAVE_KQUEUE && HAVE_KEVENT +/* TRACE("closing %d", kq); */ + if (close (kq) != 0) + JCL_ThrowException (env, "java/io/IOException", strerror (errno)); +#else + (void) kq; + throw_not_supported (env); +#endif /* HAVE_KQUEUE && HAVE_KEVENT */ +} + + +/* + * Class: gnu_java_nio_KqueueSelectorImpl + * Method: kevent_set + * Signature: (Ljava/nio/ByteBuffer;IIIZ)V + */ +JNIEXPORT void JNICALL +Java_gnu_java_nio_KqueueSelectorImpl_kevent_1set (JNIEnv *env, + jclass clazz __attribute__((unused)), + jobject nstate, jint fd, + jint ops, jint key, jboolean delete) +{ +#if HAVE_KQUEUE && HAVE_KEVENT + struct kevent *kev = (struct kevent *) (*env)->GetDirectBufferAddress (env, nstate); + short ident = kev->ident; + + if (fd != 0) + ident = fd; + + if ((ops & KEY_OP_READ) || (ops & KEY_OP_ACCEPT)) + { + /* Add the event if we never added it before. */ + if (kev[0].flags == 0 && JNI_FALSE == delete) + EV_SET(&kev[0], ident, EVFILT_READ, EV_ADD, 0, 0, (void *) key); + /* Otherwise, delete or reenable it. */ + else if (kev[0].flags != 0) + EV_SET(&kev[0], ident, EVFILT_READ, (JNI_TRUE == delete) ? EV_DELETE : EV_ENABLE, + 0, 0, (void *) key); + } + else + { + /* Means we've added this event before, and need to disable it. */ + if (kev[0].flags != 0) + EV_SET(&kev[0], ident, EVFILT_READ, EV_DISABLE, 0, 0, (void *) key); + } + + /* Do the same thing for the write filter. */ + if ((ops & KEY_OP_WRITE) || (ops & KEY_OP_CONNECT)) + { + if (kev[1].flags == 0 && JNI_FALSE == delete) + EV_SET(&kev[1], ident, EVFILT_WRITE, EV_ADD, 0, 0, (void *) key); + else if (kev[1].flags != 0) + EV_SET(&kev[1], ident, EVFILT_WRITE, (JNI_TRUE == delete) ? EV_DELETE : EV_ENABLE, + 0, 0, (void *) key); + } + else + { + if (kev[1].flags != 0) + EV_SET(&kev[1], ident, EVFILT_WRITE, EV_DISABLE, 0, 0, (void *) key); + } +#else + (void) nstate; + (void) fd; + (void) ops; + (void) key; + (void) delete; + throw_not_supported (env); +#endif /* HAVE_KQUEUE && HAVE_KEVENT */ +} + + +/* + * Class: gnu_java_nio_KqueueSelectorImpl + * Method: kevent + * Signature: (ILjava/nio/ByteBuffer;IJ)I + */ +JNIEXPORT jint JNICALL +Java_gnu_java_nio_KqueueSelectorImpl_kevent (JNIEnv *env, + jobject this __attribute__((unused)), + jint kq, jobject nstate, jint nevents, + jlong timeout) +{ +#if HAVE_KQUEUE && HAVE_KEVENT + struct timespec tv; + struct timespec *t = NULL; + struct kevent *kev = (struct kevent *) (*env)->GetDirectBufferAddress (env, nstate); + int ret; + +#ifdef TRACE_KQUEUE + int i; + for (i = 0; i < nevents; i++) + { + printf ("kevent [%d]: ident:%u filter:%x flags:%o fflags:%o data:%p udata:%p\n", + i, (unsigned) kev[i].ident, kev[i].filter, kev[i].flags, kev[i].fflags, + (void *) kev[i].data, kev[i].udata); + } +#endif + +/* TRACE("events: %p; nevents: %d; timeout: %lld", (void *) kev, nevents, timeout); */ + + if (timeout != -1) + { + tv.tv_sec = timeout / 1000; + tv.tv_nsec = (timeout % 1000) * 1000; + t = &tv; + } + + ret = kevent (kq, (const struct kevent *) kev, nevents, kev, nevents, t); + + if (ret == -1) + { + if (errno == EINTR) + ret = 0; + else + JCL_ThrowException (env, "java/io/IOException", strerror (errno)); + } + +#ifdef TRACE_KQUEUE + for (i = 0; i < ret; i++) + { + printf ("kevent [%d]: ident:%u filter:%x flags:%o fflags:%o data:%p udata:%p\n", + i, (unsigned) kev[i].ident, kev[i].filter, kev[i].flags, kev[i].fflags, + (void *) kev[i].data, kev[i].udata); + } +#endif + + return ret; +#else + (void) kq; + (void) nstate; + (void) nevents; + (void) timeout; + throw_not_supported (env); + return -1; +#endif /* HAVE_KQUEUE && HAVE_KEVENT */ +} + + +/* + * Class: gnu_java_nio_KqueueSelectorImpl + * Method: fetch_key + * Signature: (Ljava/nio/ByteBuffer;)I; + */ +JNIEXPORT jint JNICALL +Java_gnu_java_nio_KqueueSelectorImpl_fetch_1key (JNIEnv *env, + jclass clazz __attribute__((unused)), + jobject nstate) +{ +#if HAVE_KQUEUE && HAVE_KEVENT + struct kevent *kev = (struct kevent *) (*env)->GetDirectBufferAddress (env, nstate); +/* TRACE("return key %p\n", kev->udata); */ + return (jint) kev->udata; +#else + (void) nstate; + throw_not_supported (env); + return -1; +#endif /* HAVE_KQUEUE && HAVE_KEVENT */ +} + + +/* + * Class: gnu_java_nio_KqueueSelectorImpl + * Method: ready_ops + * Signature: (Ljava/nio/ByteBuffer;I)I + */ +JNIEXPORT jint JNICALL +Java_gnu_java_nio_KqueueSelectorImpl_ready_1ops (JNIEnv *env, + jclass clazz __attribute__((unused)), + jobject nstate, jint interest) +{ +#if HAVE_KQUEUE && HAVE_KEVENT + struct kevent *kev = (struct kevent *) (*env)->GetDirectBufferAddress (env, nstate); + jint ready = 0; + + /* We poll for READ for OP_READ and OP_ACCEPT. */ + if (kev->filter == EVFILT_READ) + { + ready = (interest & KEY_OP_READ) | (interest & KEY_OP_ACCEPT); +/* TRACE("filter EVFILT_READ. Ready ops set to %x", ready); */ + } + + /* Poll for WRITE for OP_WRITE and OP_CONNECT; I guess we *should* + get a WRITE event if we are connected, but I don't know if we do + for real. FIXME */ + if (kev->filter == EVFILT_WRITE) + { + ready = (interest & KEY_OP_WRITE) | (interest & KEY_OP_CONNECT); +/* TRACE("filter EVFILT_WRITE. Ready ops set to %x", ready); */ + } + + return ready; +#else + (void) nstate; + (void) interest; + throw_not_supported (env); + return -1; +#endif /* HAVE_KQUEUE && HAVE_KEVENT */ +} + + +/* + * Class: gnu_java_nio_KqueueSelectorImpl + * Method: check_eof + * Signature: (Ljava/nio/ByteBuffer;)Z + */ +JNIEXPORT jboolean JNICALL +Java_gnu_java_nio_KqueueSelectorImpl_check_1eof (JNIEnv *env, + jclass clazz __attribute__((unused)), + jobject nstate) +{ +#if HAVE_KQUEUE && HAVE_KEVENT + struct kevent *kev = (struct kevent *) (*env)->GetDirectBufferAddress (env, nstate); + if ((kev->flags & EV_EOF) == EV_EOF) + return JNI_TRUE; + return JNI_FALSE; +#else + (void) nstate; + throw_not_supported (env); + return JNI_FALSE; +#endif +} --- /dev/null 2006-09-14 17:47:38.000000000 -0700 +++ native/jni/java-nio/javanio.c 2006-08-20 17:37:49.000000000 -0700 @@ -0,0 +1,122 @@ +/* javanio.c -- implementations of functions in javanio.h. + Copyright (C) 2006 Free Software Foundation, Inc. + +This file is a part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or (at +your option) any later version. + +GNU Classpath 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 for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 +USA + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +/* + * Note, because these functions are trivial, and should be inlined, + * we include this file in the header, and do not compile it. + */ + +#include +#include +#include +#include +#include + +extern inline ssize_t +cpnio_read (int fd, void *buf, size_t nbytes) +{ + return read (fd, buf, nbytes); +} + +extern inline ssize_t +cpnio_readv (int fd, const struct iovec *iov, int iovcnt) +{ + return readv (fd, iov, iovcnt); +} + +extern inline ssize_t +cpnio_write (int fd, const void *buf, size_t nbytes) +{ + return write (fd, buf, nbytes); +} + +extern inline ssize_t +cpnio_writev (int fd, const struct iovec *iov, size_t iovcnt) +{ + return writev (fd, iov, iovcnt); +} + +extern inline int +cpnio_socket (int domain, int type, int protocol) +{ + return socket (domain, type, protocol); +} + +extern inline int +cpnio_connect (int fd, const struct sockaddr *addr, socklen_t addrlen) +{ + return connect (fd, addr, addrlen); +} + +extern inline int +cpnio_accept (int fd, struct sockaddr *addr, socklen_t *addrlen) +{ + return accept (fd, addr, addrlen); +} + +extern inline ssize_t +cpnio_sendto (int fd, const void *msg, size_t len, int flags, + const struct sockaddr *to, socklen_t tolen) +{ + return sendto (fd, msg, len, flags, to, tolen); +} + +extern inline ssize_t +cpnio_recvfrom (int fd, void *buf, size_t len, int flags, + struct sockaddr *from, socklen_t *fromlen) +{ + return recvfrom (fd, buf, len, flags, from, fromlen); +} + +extern inline int +cpnio_fcntl (int fd, int cmd, int arg) +{ +#ifdef HAVE_FCNTL + return fcntl (fd, cmd, arg); +#else + errno = ENOSUP; + return -1; +#endif /* HAVE_FCNTL */ +} + +extern inline int +cpnio_select (int nfds, fd_set *readfds, fd_set *writefds, + fd_set *excepfds, struct timeval *timeo) +{ + return select (nfds, readfds, writefds, excepfds, timeo); +} --- /dev/null 2006-09-14 17:47:38.000000000 -0700 +++ native/jni/java-nio/javanio.h 2006-08-20 17:35:59.000000000 -0700 @@ -0,0 +1,332 @@ +/* javanio.h -- reference implementation of native functions. + Copyright (C) 2006 Free Software Foundation, Inc. + +This file is a part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or (at +your option) any later version. + +GNU Classpath 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 for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 +USA + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +#ifndef __JAVANIO_H__ +#define __JAVANIO_H__ + +/** + * This header defines functions that are called by our JNI reference + * implementation of java.nio.*. In our reference implementation, these + * functions map exactly to their counterparts in POSIX; in implementations + * that can't use these functions directly (such as systems that use user-land + * threads, and thus can't call blocking system calls directly) can provide + * their own implementations suitable for their system. + */ + +/** + * This macro is used in all function prototypes below; if any additional + * keywords need to be added to a prototype, declare them in this macro. + */ +#define CPNIO_EXPORT extern inline + +/** + * Read bytes from the given file descriptor into the given memory address, which + * has sufficient space for NBYTES bytes. + * + * \param fd The file descriptor to read from. + * \param buf The memory address to read bytes into. + * \param nbytes The number of bytes available to store in BUF. + * \return The number of bytes read, possibly zero, on success; return -1 on failure, + * and set ERRNO to an appropriate value. + * \see read(2) + * + * Allowed errno values: + * [EBADF] If FD is not a valid file descriptor, or is not open for reading. + * [EFAULT] If BUF points outside the process's address space. + * [EIO] An I/O error occurrs. + * [EINTR] If the read is interrupted by a signal. + * [EINVAL] If FD is negative. + * [EAGAIN] If FD was marked for non-blocking I/O, and no data were ready to + * be read. + */ +CPNIO_EXPORT ssize_t cpnio_read (int fd, void *buf, size_t nbytes); + +/* + * Read bytes from a file descriptor into a sequence of IO buffers. + * + * The iovec structure is defined as: + * + * struct iovec { + * char *iov_base; + * size_t iov_len; + * }; + * + * The call to _cp_readv should do a scattering read, where for each struct iovec + * in the supplied list, up to IOV_LEN bytes are read into IOV_BASE. The function + * returns the total number of bytes read into all supplied buffers. + * + * \param fd The file descriptor. + * \param iov A pointer to the head of a list of iovec structures. + * \param iovcnt The number of iovec structures pointed to by IOV. + * \return The total number of bytes read accross all buffers, possibly zero. On + * error, -1 is returned and ERRNO is set. + * \see readv(2) + * + * Allowed ERRNO values include all of those listed for _cp_read, as well as the + * following: + * [EINVAL] If IOVCNT overflows the maximum number of iovec structures + * this platform supports (usually 16), if any IOV_LEN value + * is negative, or if the sum of all IOV_LEN values is too + * large to be stored in a ssize_t (usually a 32-bit integer). + * [EFAULT] If part of IOV points outside the process's address space. + */ +CPNIO_EXPORT ssize_t cpnio_readv (int fd, const struct iovec *iov, int iovcnt); + +/* + * Write NBYTES bytes from BUF to the file descriptor FD, returning the number + * of bytes successfully written. + * + * \param fd The file descriptor. + * \param buf A pointer to the bytes to write. + * \param nbytes The maximum number of bytes to write. + * \return The number of bytes written to the file descriptor, possibly zero. -1 + * is returned if an error is encountered, and ERRNO will be set. + * \see write(2) + * + * Allowed ERRNO values: + * [EBADF] If FD is not a valid file descriptor or is not open for writing. + * [EPIPE] If FD is a pipe, when the other side is disconnected; if FD is a + * socket, when the peer is not connected. + * [EFBIG] When FD is a file, and writing to it overflows the process's + * or the system's maximim file size. + * [EFAULT] If the buffer to write points outside the process's address + * space. + * [EINVAL] If the descriptor FD is negative. + * [ENOSPC] If FD is a file, and there is insufficient space on the + * filesystem. + * [EDQUOT] If FD is a file, and the user's disk quota has been exceeded. + * [EIO] If an I/O error occurs. + * [EINTR] If the call is interrupted by a signal. + * [EAGAIN] If FD is in non-blocking mode, and no bytes could be immediately + * written. + */ +CPNIO_EXPORT ssize_t cpnio_write (int fd, const void *buf, size_t nbytes); + +/* + * Write data from a sequence of IOVCNT buffers IOV to a file descriptor FD. + * + * \param fd The file descriptor. + * \param iov The list of buffers to write. + * \param iovcnt The number of iovec structures pointed to by IOV. + * \return The total number of bytes written from the given buffers, possibly + * zero. -1 if an error occurs, and ERRNO will be set. + * \see writev(2) + * + * Allowed ERRNO values include those mentioned in _cp_write, as well as: + * [EDESTADDRREQ] If the descriptor is a datagram socket, and the peer is + * no longer available. + * [EINVAL] If IOVCNT is out of range, if any IOV_LEN value is + * negative, or if the sum of all IOVCNT IOV_LEN values + * will overflow a ssize_t. + * [ENOBUFS] If the mbuf pool is exhausted (???). + */ +CPNIO_EXPORT ssize_t cpnio_writev (int fd, const struct iovec *iov, size_t iovcnt); + +/** + * Open a new, unbound and unconnected socket. + * + * \param domain The socket domain. Implementations need only handle AF_INET. + * \param type The socket type; implementations need only handle types + * SOCK_STREAM (for streaming sockets) and SOCK_DGRAM (for datagram sockets). + * \param protocol This should always be 0. It can be ignored. + * \return A new file descriptor pointing to a newly created socket, or -1 on + * error, and ERRNO set. + * + * Allowed ERRNO values: + * [EPROTONOSUPPORT] If TYPE is unrecognized. + * [EMFILE] If a new file descriptor cannot be allocated, because + * the process's descriptor table is full. + * [ENFILE] Likewise, but when the system table is full. + * [EACCES] If this operation is not allowed. + * [ENOBUFS] If there is not enough buffer space available for the + * new socket. + */ +CPNIO_EXPORT int cpnio_socket (int domain, int type, int protocol); + +/** + * Connect a socket to a remote address. + * + * \param fd The file descriptor of the socket to connect. + * \param addr The address to connect to. In practice, this should be + * either a `struct sockaddr_in' or a `struct sockaddr_in6'. + * \param addrlen The size of the address structure passed by ADDR. + * \return Zero if the connect succeeds. -1 on error, and ERRNO should be set. + * + * Allowed ERRNO values: + * [EBADF] If FD is not a valid file descriptor. + * [ENOTSOCK] If FD is not a socket descriptor. + * [EADDRNOTAVAIL] If ADDR is not available for use to this process. + * [EAFNOSUPPORT] If the address family of ADDR is not supported. + * [EISCONN] If the socket is already connected. + * [ETIMEDOUT] If the connection could not be made in a reasonable + * amount of time. + * [ECONNREFUSED] If the connection attempt was rejected. + * [ENETUNREACH] If the network ADDR is on is unreachable. + * [EADDRINUSE] If the address is already in use. + * [EFAULT] If ADDR points outside the addressable space. + * [EINPROGRESS] If FD is in non-blocking mode, and the connection could + * not be completed immediately. + * [EALREADY] If FD is in non-blocking mode, and a connection attempt + * is still pending. + * [EACCESS] If ADDR is the broadcast address, and the socket option + * SO_BROADCAST is not set. + */ +CPNIO_EXPORT int cpnio_connect (int fd, const struct sockaddr *addr, socklen_t addrlen); + +/** + * Accept an incoming connection on a socket, returning a new socket for + * the connection, and storing the peer address in ADDR. + * + * \param fd The socket file descriptor. + * \param addr The structure to store the peer address in. + * \param addrlen The size of the data available in ADDR; upon return, the + * number of bytes stored in ADDR will be placed here. + * \return The new socket file descriptor, or -1 on error, and ERRNO set. + * + * Allowed ERRNO values: + * [EBADF] If FD is not a valid file descriptor. + * [ENOTSOCK] If FD in not a socket descriptor. + * [EOPNOTSUPP] If the socket is not a SOCK_STREAM socket. + * [EFAULT] If ADDR points outside the process's addressable space. + * [EWOULDBLOCK] If the socket is in non-blocking mode, and no connection + * attempt is currently ready. + * [EMFILE] If the process's descriptor table is full. + * [ENFILE] If the system's descriptor table is full. + */ +CPNIO_EXPORT int cpnio_accept (int fd, struct sockaddr *addr, socklen_t *addrlen); + +/** + * Send a datagram to the given address. + * + * \param fd The socket file descriptor. + * \param msg A pointer to the message to send. + * \param len The size of the message to send. + * \param flags Flags for sending. + * \param to The remote address to send the message to. + * \param tolen The size of the TO address structure. + * \return The number of bytes written, possibly zero, on success. Returns + * -1 on failure, and sets ERRNO. + * \see sendto(2) + * + * Allowed ERRNO values: + * [EBADF] + * [ENOTSOCK] + * [EFAULT] + * [EMSGSIZE] + * [EAGAIN] + * [ENOBUFS] + * [EACCES] + * [EHOSTUNREACH] + */ +CPNIO_EXPORT ssize_t cpnio_sendto (int fd, const void *msg, size_t len, int flags, + const struct sockaddr *to, socklen_t tolen); + +/** + * Receive a message on a socket, storing the remote host's address in + * FROM. + * + * \param fd The socket file descriptor. + * \param buf The buffer to store received bytes in. + * \param flags Flags to control the receive. + * \param from Where to store the remote address. + * \param fromlen Pointer to the size of FROM; on return, it will contain the + * size of the structure placed in FROM. + * \return The number of bytes received on success. -1 on error, and ERRNO will + * be set. + * \see recvfrom(2) + * + * Allewed ERRNO values: + * [EBADF] FD is not a valid file descriptor. + * [ENOTCONN] If the socket is stream-oriented, and no prior call to + * connect(2) was made. + * [ENOTSOCK] FD is not a socket. + * [EAGAIN] FD is in non-blocking mode, and no message was + * immediately available. + * [EINTR] The system call was interrupted by a signal. + * [EFAULT] BUF, FROM, or FROMLEN lie outside the process's address + * space. + */ +CPNIO_EXPORT ssize_t cpnio_recvfrom (int fd, void *buf, size_t len, int flags, + struct sockaddr *from, socklen_t *fromlen); + + +/** + * Control file descriptor properties. + * + * \param fd The file descriptor to control. + * \param cmd The command to execute. + * \param arg The command argument. + * \return A value other than -1, specific to CMD. On error, -1 is + * returned, and ERRNO is set. + * + * Allowed ERRNO values: + * FIXME + */ +CPNIO_EXPORT int cpnio_fcntl (int fd, int cmd, int arg); + + +/** + * Select from one of the given file descriptor sets a descriptor that + * is ready for the given operation (read, write, etc.). + * + * \param nfds A value one larger than the largest file + * descriptor. + * \param readfds A set of file descriptors to select for + * readability. + * \param writefds A set of file descriptors to select for + * writability. + * \param exceptfds A set of file descriptors to select for + * exceptional conditions. + * \param tm The selection timeout. + * \return The number of file descriptors selected, possibly zero, or + * -1 on error (and with ERRNO set). + */ +CPNIO_EXPORT int cpnio_select (int nfds, fd_set *readfds, fd_set *writefds, + fd_set *exceptfds, struct timeval *tm); + +/* + * We include the implementation file here, because our reference + * implementation is trivial, and the functions are declared extern + * inline. + * + * Implementations that need different implementations of these functions + * SHOULD remove this line, and compile javanio.c as a separate unit. + */ +#include "javanio.c" + +#endif /* __JAVANIO_H__ */ --- /dev/null 2006-09-14 17:47:38.000000000 -0700 +++ include/gnu_java_nio_FileChannelImpl.h 2006-08-18 14:56:11.000000000 -0700 @@ -0,0 +1,25 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include +/* Header for class gnu_java_nio_FileChannelImpl */ + +#ifndef _Included_gnu_java_nio_FileChannelImpl +#define _Included_gnu_java_nio_FileChannelImpl +#ifdef __cplusplus +extern "C" { +#endif +#undef gnu_java_nio_FileChannelImpl_READ +#define gnu_java_nio_FileChannelImpl_READ 1L +#undef gnu_java_nio_FileChannelImpl_WRITE +#define gnu_java_nio_FileChannelImpl_WRITE 2L +#undef gnu_java_nio_FileChannelImpl_APPEND +#define gnu_java_nio_FileChannelImpl_APPEND 4L +#undef gnu_java_nio_FileChannelImpl_EXCL +#define gnu_java_nio_FileChannelImpl_EXCL 8L +#undef gnu_java_nio_FileChannelImpl_SYNC +#define gnu_java_nio_FileChannelImpl_SYNC 16L +#undef gnu_java_nio_FileChannelImpl_DSYNC +#define gnu_java_nio_FileChannelImpl_DSYNC 32L +#ifdef __cplusplus +} +#endif +#endif --- /dev/null 2006-09-14 17:47:38.000000000 -0700 +++ include/gnu_java_nio_KqueueSelectorImpl.h 2006-08-05 20:02:51.000000000 -0700 @@ -0,0 +1,85 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include +/* Header for class gnu_java_nio_KqueueSelectorImpl */ + +#ifndef _Included_gnu_java_nio_KqueueSelectorImpl +#define _Included_gnu_java_nio_KqueueSelectorImpl +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: gnu_java_nio_KqueueSelectorImpl + * Method: kqueue_supported + * Signature: ()Z + */ +JNIEXPORT jboolean JNICALL Java_gnu_java_nio_KqueueSelectorImpl_kqueue_1supported + (JNIEnv *, jclass); + +/* + * Class: gnu_java_nio_KqueueSelectorImpl + * Method: sizeof_struct_kevent + * Signature: ()I + */ +JNIEXPORT jint JNICALL Java_gnu_java_nio_KqueueSelectorImpl_sizeof_1struct_1kevent + (JNIEnv *, jclass); + +/* + * Class: gnu_java_nio_KqueueSelectorImpl + * Method: implOpen + * Signature: ()I + */ +JNIEXPORT jint JNICALL Java_gnu_java_nio_KqueueSelectorImpl_implOpen + (JNIEnv *, jclass); + +/* + * Class: gnu_java_nio_KqueueSelectorImpl + * Method: implClose + * Signature: (I)V + */ +JNIEXPORT void JNICALL Java_gnu_java_nio_KqueueSelectorImpl_implClose + (JNIEnv *, jclass, jint); + +/* + * Class: gnu_java_nio_KqueueSelectorImpl + * Method: kevent_set + * Signature: (Ljava/nio/ByteBuffer;IIIZ)V + */ +JNIEXPORT void JNICALL Java_gnu_java_nio_KqueueSelectorImpl_kevent_1set + (JNIEnv *, jclass, jobject, jint, jint, jint, jboolean); + +/* + * Class: gnu_java_nio_KqueueSelectorImpl + * Method: kevent + * Signature: (ILjava/nio/ByteBuffer;IJ)I + */ +JNIEXPORT jint JNICALL Java_gnu_java_nio_KqueueSelectorImpl_kevent + (JNIEnv *, jclass, jint, jobject, jint, jlong); + +/* + * Class: gnu_java_nio_KqueueSelectorImpl + * Method: fetch_key + * Signature: (Ljava/nio/ByteBuffer;)I + */ +JNIEXPORT jint JNICALL Java_gnu_java_nio_KqueueSelectorImpl_fetch_1key + (JNIEnv *, jclass, jobject); + +/* + * Class: gnu_java_nio_KqueueSelectorImpl + * Method: ready_ops + * Signature: (Ljava/nio/ByteBuffer;I)I + */ +JNIEXPORT jint JNICALL Java_gnu_java_nio_KqueueSelectorImpl_ready_1ops + (JNIEnv *, jclass, jobject, jint); + +/* + * Class: gnu_java_nio_KqueueSelectorImpl + * Method: check_eof + * Signature: (Ljava/nio/ByteBuffer;)Z + */ +JNIEXPORT jboolean JNICALL Java_gnu_java_nio_KqueueSelectorImpl_check_1eof + (JNIEnv *, jclass, jobject); + +#ifdef __cplusplus +} +#endif +#endif