1 33 package org.jruby.util; 34 35 import java.io.BufferedInputStream ; 36 import java.io.FileOutputStream ; 37 import java.io.IOException ; 38 import java.io.InputStream ; 39 import java.io.OutputStream ; 40 import java.nio.channels.FileChannel ; 41 42 import org.jruby.Ruby; 43 import org.jruby.RubyIO; 44 45 49 public class IOHandlerUnseekable extends IOHandlerJavaIO { 50 protected InputStream input = null; 51 protected OutputStream output = null; 52 53 58 public IOHandlerUnseekable(Ruby runtime, InputStream inStream, 59 OutputStream outStream) throws IOException { 60 super(runtime); 61 String mode = ""; 62 63 if (inStream != null) { 64 if (inStream instanceof BufferedInputStream ) { 66 input = inStream; 67 } else { 68 input = new BufferedInputStream (inStream); 69 } 70 mode += "r"; 71 isOpen = true; 72 } 73 74 if (outStream != null) { 75 output = outStream; 76 mode += "w"; 77 isOpen = true; 78 } 79 if ("rw".equals(mode)) { 80 modes = new IOModes(runtime, IOModes.RDWR); 81 isOpen = true; 82 } else { 83 84 if (!isOpen) { 86 throw new IOException ("Opening nothing?"); 87 } 88 89 modes = new IOModes(runtime, mode); 90 } 91 fileno = RubyIO.getNewFileno(); 92 } 93 94 public IOHandlerUnseekable(Ruby runtime, int fileno) throws IOException { 95 super(runtime); 96 97 switch (fileno) { 98 case 0: 99 input = new RubyInputStream(runtime.getIn()); 100 modes = new IOModes(runtime, "r"); 101 isOpen = true; 102 break; 103 case 1: 104 output = runtime.getOut(); 105 modes = new IOModes(runtime, "w"); 106 isOpen = true; 107 break; 108 case 2: 109 output = runtime.getErr(); 110 modes = new IOModes(runtime, "w"); 111 isOpen = true; 112 break; 113 default: 114 throw new IOException ("Bad file descriptor"); 115 } 116 117 this.fileno = fileno; 118 } 119 120 public IOHandlerUnseekable(Ruby runtime, int fileno, String mode) throws IOException { 121 super(runtime); 122 123 modes = new IOModes(runtime, mode); 124 125 if (fileno < 0 || fileno > 2) { 126 throw new IOException ("Bad file descriptor"); 127 } 128 129 if (modes.isReadable()) { 130 input = new RubyInputStream(System.in); 131 isOpen = true; 132 if (modes.isWriteable()) { 133 output = System.out; 134 } 135 } else if (isWriteable()) { 136 output = System.out; 137 isOpen = true; 138 } 139 140 this.fileno = fileno; 141 } 142 143 public IOHandler cloneIOHandler() throws IOException { 144 return new IOHandlerUnseekable(getRuntime(), input, output); 145 } 146 147 153 160 public void close() throws IOException , BadDescriptorException { 161 if (!isOpen()) { 162 throw new BadDescriptorException(); 163 } 164 165 isOpen = false; 166 167 if (modes.isReadable() && input != System.in) { 169 input.close(); 170 } 171 172 if (modes.isWriteable() && output != System.out && output != System.err) { 174 output.close(); 175 } 176 } 177 178 183 public void flush() throws IOException , BadDescriptorException { 184 checkWriteable(); 185 186 output.flush(); 187 } 188 189 192 public InputStream getInputStream() { 193 return input; 194 } 195 196 199 public OutputStream getOutputStream() { 200 return output; 201 } 202 203 208 public boolean isEOF() throws IOException , BadDescriptorException { 209 checkReadable(); 210 211 int c = input.read(); 212 if (c == -1) { 213 return true; 214 } 215 ungetc(c); 216 return false; 217 } 218 219 222 public int pid() { 223 return -1; 225 } 226 227 231 public long pos() throws PipeException { 232 throw new IOHandler.PipeException(); 233 } 234 235 public void resetByModes(IOModes newModes) { 236 } 237 238 242 public void rewind() throws PipeException { 243 throw new IOHandler.PipeException(); 244 } 245 246 250 public void seek(long offset, int type) throws PipeException { 251 throw new IOHandler.PipeException(); 252 } 253 254 257 public void sync() throws IOException { 258 output.flush(); 259 260 if (output instanceof FileOutputStream ) { 261 ((FileOutputStream ) output).getFD().sync(); 262 } 263 } 264 265 protected void sysread(ByteList buf, int off, int length) throws IOException { 266 int read = 0; 267 int n; 268 while(read < length) { 269 n = input.read(buf.bytes,off+read,length-read); 270 if(n == -1) { 271 if(read == 0) { 272 throw new java.io.EOFException (); 273 } else { 274 break; 275 } 276 } 277 read += n; 278 } 279 buf.realSize += read; 280 } 281 282 public ByteList sysread(int number) throws IOException , BadDescriptorException { 283 checkReadable(); 284 byte[] buf = new byte[number]; 285 int read = 0; 286 int n; 287 while(read < number) { 288 n = input.read(buf,read,number-read); 289 if(n == -1) { 290 if(read == 0) { 291 throw new java.io.EOFException (); 292 } else { 293 break; 294 } 295 } 296 read += n; 297 } 298 299 return new ByteList(buf, 0, read, false); 300 } 301 302 305 public int sysread() throws IOException { 306 return input.read(); 307 } 308 309 314 public int syswrite(ByteList buf) throws IOException , BadDescriptorException { 315 getRuntime().secure(4); 316 checkWriteable(); 317 318 if (buf == null || buf.realSize == 0) { 319 return 0; 320 } 321 322 output.write(buf.bytes,0,buf.realSize); 323 324 if (isSync) { 326 sync(); 327 } 328 329 return buf.realSize; 330 } 331 332 337 public int syswrite(int c) throws IOException , BadDescriptorException { 338 getRuntime().secure(4); 339 checkWriteable(); 340 341 output.write(c); 342 343 if (isSync) { 345 sync(); 346 } 347 348 return c; 349 } 350 351 public void truncate(long newLength) throws IOException , PipeException { 352 throw new IOHandler.PipeException(); 353 } 354 355 public FileChannel getFileChannel() { 356 assert false : "No file channel for unseekable IO"; 357 return null; 358 } 359 } 360 | Popular Tags |