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 null; // XXX - return new VMChannel(file.getNativeFD());
  84:   }
  85: 
  86:   /**
  87:    * Set the file descriptor to have the required blocking
  88:    * setting.
  89:    * 
  90:    * @param fd
  91:    * @param blocking
  92:    */
  93:   public void setBlocking(int fd, boolean blocking)
  94:   {
  95:     throw new RuntimeException("XXX - Implement me!");
  96:   }
  97:   
  98:   public void setBlocking(boolean blocking)
  99:   {
 100:     setBlocking(fd, blocking);
 101:   }
 102:   
 103: 
 104:   /**
 105:    * Reads a byte buffer directly using the supplied file descriptor.
 106:    * Assumes that the buffer is a DirectBuffer.
 107:    * 
 108:    * @param fd Native file descriptor to read from.
 109:    * @param dst Direct Byte Buffer to read to.
 110:    * @return Number of bytes read.
 111:    * @throws IOException If an error occurs or dst is not a direct buffers. 
 112:    */
 113:   int read(int fd, ByteBuffer dst)
 114:     throws IOException
 115:   {
 116:     throw new IOException("XXX - Implement me!");
 117:   }
 118:   
 119:   public int read(ByteBuffer dst)
 120:     throws IOException
 121:   {
 122:     return read(fd, dst);
 123:   }
 124:   
 125:   /**
 126:    * Reads into byte buffers directly using the supplied file descriptor.
 127:    * Assumes that the buffer list contains DirectBuffers.  Will perform a
 128:    * scattering read.
 129:    * 
 130:    * @param fd Native file descriptor to read from.
 131:    * @param dsts An array direct byte buffers.
 132:    * @param offset Index of the first buffer to read to.
 133:    * @param length The number of buffers to read to.
 134:    * @return Number of bytes read.
 135:    * @throws IOException If an error occurs or the dsts are not direct buffers. 
 136:    */
 137:   long readScattering(int fd, ByteBuffer[] dsts, int offset, int length)
 138:     throws IOException
 139:   {
 140:     throw new IOException("XXX - Implement me!");
 141:   }
 142: 
 143:   public long readScattering(ByteBuffer[] dsts, int offset, int length)
 144:     throws IOException
 145:   {
 146:     if (offset + length > dsts.length)
 147:       throw new IndexOutOfBoundsException("offset + length > dsts.length");
 148:     
 149:     return readScattering(fd, dsts, offset, length);
 150:   }
 151:   
 152:   /**
 153:    * Writes from a direct byte bufer using the supplied file descriptor.
 154:    * Assumes the buffer is a DirectBuffer.
 155:    * 
 156:    * @param fd
 157:    * @param src
 158:    * @return Number of bytes written.
 159:    * @throws IOException
 160:    */
 161:   int write(int fd, ByteBuffer src)
 162:     throws IOException
 163:   {
 164:     throw new IOException("XXX - Implement me!");
 165:   }
 166: 
 167:   public int write(ByteBuffer src)
 168:     throws IOException
 169:   {
 170:     return write(fd, src);
 171:   }
 172:   
 173:   /**
 174:    * Writes from byte buffers directly using the supplied file descriptor.
 175:    * Assumes the that buffer list constains DirectBuffers.  Will perform
 176:    * as gathering write.
 177:    * 
 178:    * @param fd
 179:    * @param srcs
 180:    * @param offset
 181:    * @param length
 182:    * @return Number of bytes written.
 183:    * @throws IOException
 184:    */
 185:   long writeGathering(int fd, ByteBuffer[] srcs, int offset, int length)
 186:     throws IOException
 187:   {
 188:     throw new IOException("XXX - Implement me!");
 189:   }
 190:  
 191:   public long writeGathering(ByteBuffer[] srcs, int offset, int length)
 192:     throws IOException
 193:   {
 194:     if (offset + length > srcs.length)
 195:       throw new IndexOutOfBoundsException("offset + length > srcs.length");
 196:     
 197:     return writeGathering(fd, srcs, offset, length);
 198:   }
 199: }