Source for gnu.java.nio.VMChannel

   1: /* VMChannel.java -- Native interface suppling channel operations.
   2:    Copyright (C) 2006 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package gnu.java.nio;
  40: 
  41: import gnu.classpath.Configuration;
  42: import gnu.java.net.PlainSocketImpl;
  43: import gnu.java.nio.PipeImpl.SinkChannelImpl;
  44: import gnu.java.nio.PipeImpl.SourceChannelImpl;
  45: import gnu.java.nio.channels.FileChannelImpl;
  46: 
  47: import java.io.IOException;
  48: import java.nio.ByteBuffer;
  49: 
  50: /**
  51:  * Native interface to support configuring of channel to run in a non-blocking
  52:  * manner and support scatter/gather io operations.
  53:  * 
  54:  * @author Michael Barker <mike@middlesoft.co.uk>
  55:  *
  56:  */
  57: public class VMChannel
  58: {
  59:   private final int fd;
  60:   
  61:   private VMChannel(int fd)
  62:   {
  63:     this.fd = fd;
  64:   }
  65:   
  66:   public static VMChannel getVMChannel(PlainSocketImpl socket)
  67:   {
  68:     return new VMChannel(socket.getNativeFD());
  69:   }
  70:   
  71:   public static VMChannel getVMChannel(SourceChannelImpl source)
  72:   {
  73:     return new VMChannel(source.getNativeFD());
  74:   }
  75:   
  76:   public static VMChannel getVMChannel(SinkChannelImpl sink)
  77:   {
  78:     return new VMChannel(sink.getNativeFD());
  79:   }
  80:   
  81:   public static VMChannel getVMChannel(FileChannelImpl file)
  82:   {
  83:     return new VMChannel(file.getNativeFD());
  84:   }
  85: 
  86:   static
  87:   {
  88:     // load the shared library needed for native methods.
  89:     if (Configuration.INIT_LOAD_LIBRARY)
  90:       {
  91:         System.loadLibrary ("javanio");
  92:       }
  93:     initIDs();
  94:   }
  95:   
  96:   /**
  97:    * Set the file descriptor to have the required blocking
  98:    * setting.
  99:    * 
 100:    * @param fd
 101:    * @param blocking
 102:    */
 103:   public native void setBlocking(int fd, boolean blocking);
 104:   
 105:   public void setBlocking(boolean blocking)
 106:   {
 107:     setBlocking(fd, blocking);
 108:   }
 109:   
 110: 
 111:   /**
 112:    * Reads a byte buffer directly using the supplied file descriptor.
 113:    * Assumes that the buffer is a DirectBuffer.
 114:    * 
 115:    * @param fd Native file descriptor to read from.
 116:    * @param dst Direct Byte Buffer to read to.
 117:    * @return Number of bytes read.
 118:    * @throws IOException If an error occurs or dst is not a direct buffers. 
 119:    */
 120:   native int read(int fd, ByteBuffer dst)
 121:     throws IOException;
 122:   
 123:   public int read(ByteBuffer dst)
 124:     throws IOException
 125:   {
 126:     return read(fd, dst);
 127:   }
 128:   
 129:   /**
 130:    * Reads into byte buffers directly using the supplied file descriptor.
 131:    * Assumes that the buffer list contains DirectBuffers.  Will perform a
 132:    * scattering read.
 133:    * 
 134:    * @param fd Native file descriptor to read from.
 135:    * @param dsts An array direct byte buffers.
 136:    * @param offset Index of the first buffer to read to.
 137:    * @param length The number of buffers to read to.
 138:    * @return Number of bytes read.
 139:    * @throws IOException If an error occurs or the dsts are not direct buffers. 
 140:    */
 141:   native long readScattering(int fd, ByteBuffer[] dsts, int offset, int length)
 142:     throws IOException;
 143: 
 144:   public long readScattering(ByteBuffer[] dsts, int offset, int length)
 145:     throws IOException
 146:   {
 147:     if (offset + length > dsts.length)
 148:       throw new IndexOutOfBoundsException("offset + length > dsts.length");
 149:     
 150:     return readScattering(fd, dsts, offset, length);
 151:   }
 152:   
 153:   /**
 154:    * Writes from a direct byte bufer using the supplied file descriptor.
 155:    * Assumes the buffer is a DirectBuffer.
 156:    * 
 157:    * @param fd
 158:    * @param src
 159:    * @return Number of bytes written.
 160:    * @throws IOException
 161:    */
 162:   native int write(int fd, ByteBuffer src)
 163:     throws IOException;
 164: 
 165:   public int write(ByteBuffer src)
 166:     throws IOException
 167:   {
 168:     return write(fd, src);
 169:   }
 170:   
 171:   /**
 172:    * Writes from byte buffers directly using the supplied file descriptor.
 173:    * Assumes the that buffer list constains DirectBuffers.  Will perform
 174:    * as gathering write.
 175:    * 
 176:    * @param fd
 177:    * @param srcs
 178:    * @param offset
 179:    * @param length
 180:    * @return Number of bytes written.
 181:    * @throws IOException
 182:    */
 183:   native long writeGathering(int fd, ByteBuffer[] srcs, int offset, int length)
 184:     throws IOException;
 185:   
 186:   public long writeGathering(ByteBuffer[] srcs, int offset, int length)
 187:     throws IOException
 188:   {
 189:     if (offset + length > srcs.length)
 190:       throw new IndexOutOfBoundsException("offset + length > srcs.length");
 191:     
 192:     return writeGathering(fd, srcs, offset, length);
 193:   }
 194:   
 195:   private native static void initIDs();
 196: 
 197: }