1 /* PipeImpl.java -- 2 Copyright (C) 2002, 2003, 2004, 2005 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 package gnu.java.nio; 39 40 41 import java.io.IOException; 42 import java.nio.ByteBuffer; 43 import java.nio.channels.Pipe; 44 import java.nio.channels.spi.SelectorProvider; 45 46 class PipeImpl extends Pipe 47 { 48 public static final class SourceChannelImpl extends Pipe.SourceChannel 49 { 50 private int native_fd; 51 private VMChannel vmch; 52 SourceChannelImpl(SelectorProvider selectorProvider, int native_fd)53 public SourceChannelImpl (SelectorProvider selectorProvider, 54 int native_fd) 55 { 56 super (selectorProvider); 57 this.native_fd = native_fd; 58 vmch = VMChannel.getVMChannel(this); 59 } 60 implCloseSelectableChannel()61 protected final void implCloseSelectableChannel() 62 throws IOException 63 { 64 throw new Error ("Not implemented"); 65 } 66 implConfigureBlocking(boolean blocking)67 protected void implConfigureBlocking (boolean blocking) 68 throws IOException 69 { 70 vmch.setBlocking(blocking); 71 } 72 read(ByteBuffer src)73 public final int read (ByteBuffer src) 74 throws IOException 75 { 76 return vmch.read(src); 77 } 78 read(ByteBuffer[] srcs)79 public final long read (ByteBuffer[] srcs) 80 throws IOException 81 { 82 return vmch.readScattering(srcs, 0, srcs.length); 83 } 84 read(ByteBuffer[] srcs, int offset, int len)85 public final synchronized long read (ByteBuffer[] srcs, int offset, 86 int len) 87 throws IOException 88 { 89 if (offset < 0 90 || offset > srcs.length 91 || len < 0 92 || len > srcs.length - offset) 93 throw new IndexOutOfBoundsException(); 94 95 return vmch.readScattering(srcs, offset, len); 96 } 97 getNativeFD()98 public final int getNativeFD() 99 { 100 return native_fd; 101 } 102 } 103 104 public static final class SinkChannelImpl extends Pipe.SinkChannel 105 { 106 private int native_fd; 107 private VMChannel vmch; 108 SinkChannelImpl(SelectorProvider selectorProvider, int native_fd)109 public SinkChannelImpl (SelectorProvider selectorProvider, 110 int native_fd) 111 { 112 super (selectorProvider); 113 this.native_fd = native_fd; 114 vmch = VMChannel.getVMChannel(this); 115 } 116 implCloseSelectableChannel()117 protected final void implCloseSelectableChannel() 118 throws IOException 119 { 120 throw new Error ("Not implemented"); 121 } 122 implConfigureBlocking(boolean blocking)123 protected final void implConfigureBlocking (boolean blocking) 124 throws IOException 125 { 126 vmch.setBlocking(blocking); 127 } 128 write(ByteBuffer dst)129 public final int write (ByteBuffer dst) 130 throws IOException 131 { 132 return vmch.write(dst); 133 } 134 write(ByteBuffer[] srcs)135 public final long write (ByteBuffer[] srcs) 136 throws IOException 137 { 138 return vmch.writeGathering(srcs, 0, srcs.length); 139 } 140 write(ByteBuffer[] srcs, int offset, int len)141 public final synchronized long write (ByteBuffer[] srcs, int offset, int len) 142 throws IOException 143 { 144 if (offset < 0 145 || offset > srcs.length 146 || len < 0 147 || len > srcs.length - offset) 148 throw new IndexOutOfBoundsException(); 149 150 return vmch.writeGathering(srcs, offset, len); 151 } 152 getNativeFD()153 public final int getNativeFD() 154 { 155 return native_fd; 156 } 157 } 158 159 private SinkChannelImpl sink; 160 private SourceChannelImpl source; 161 PipeImpl(SelectorProvider provider)162 public PipeImpl (SelectorProvider provider) 163 throws IOException 164 { 165 super(); 166 VMPipe.init (this, provider); 167 } 168 sink()169 public Pipe.SinkChannel sink() 170 { 171 return sink; 172 } 173 source()174 public Pipe.SourceChannel source() 175 { 176 return source; 177 } 178 } 179