1 31 package org.jruby.util; 32 33 import java.io.IOException ; 34 import java.io.InputStream ; 35 import java.io.OutputStream ; 36 import java.nio.channels.FileChannel ; 37 38 import org.jruby.Ruby; 39 import org.jruby.RubyIO; 40 41 public class IOHandlerProcess extends IOHandlerJavaIO { 42 protected InputStream input = null; 43 protected OutputStream output = null; 44 protected Process process = null; 45 46 public IOHandlerProcess(Ruby runtime, Process process, IOModes modes) throws IOException { 47 super(runtime); 48 49 if (process == null) { 50 throw new IOException ("Null process"); 51 } 52 53 this.process = process; 54 this.input = process.getInputStream(); 55 this.output = process.getOutputStream(); 56 57 isOpen = true; 58 59 this.modes = modes; 60 this.isSync = true; 61 fileno = RubyIO.getNewFileno(); 62 } 63 64 public IOHandler cloneIOHandler() throws IOException { 65 return new IOHandlerProcess(getRuntime(), process, modes); 67 } 68 69 76 public void close() throws IOException , BadDescriptorException { 77 if (!isOpen()) { 78 throw new BadDescriptorException(); 79 } 80 81 isOpen = false; 82 83 input.close(); 84 output.close(); 85 86 process.destroy(); 88 process = null; 89 } 90 91 96 public void flush() throws IOException , BadDescriptorException { 97 checkWriteable(); 98 99 output.flush(); 100 } 101 102 105 public InputStream getInputStream() { 106 return input; 107 } 108 109 112 public OutputStream getOutputStream() { 113 return output; 114 } 115 116 121 public boolean isEOF() throws IOException , BadDescriptorException { 122 checkReadable(); 123 124 int c = input.read(); 125 if (c == -1) { 126 return true; 127 } 128 ungetc(c); 129 return false; 130 } 131 132 135 public int pid() { 136 return process.hashCode(); 138 } 139 140 144 public long pos() throws PipeException { 145 throw new IOHandler.PipeException(); 146 } 147 148 public void resetByModes(IOModes newModes) { 149 } 150 151 155 public void rewind() throws PipeException { 156 throw new IOHandler.PipeException(); 157 } 158 159 163 public void seek(long offset, int type) throws PipeException { 164 throw new IOHandler.PipeException(); 165 } 166 167 170 public void sync() throws IOException { 171 output.flush(); 172 } 173 174 177 public int sysread() throws IOException { 178 return input.read(); 179 } 180 181 public ByteList sysread(int number) throws IOException , BadDescriptorException { 182 checkReadable(); 183 byte[] buf = new byte[number]; 184 int read = 0; 185 int n; 186 while(read < number) { 187 n = input.read(buf,read,number-read); 188 if(n == -1) { 189 if(read == 0) { 190 throw new java.io.EOFException (); 191 } else { 192 break; 193 } 194 } 195 read += n; 196 } 197 198 return new ByteList(buf, 0, read, false); 199 } 200 201 206 public int syswrite(ByteList buf) throws IOException , BadDescriptorException { 207 getRuntime().secure(4); 208 checkWriteable(); 209 210 if (buf == null || buf.realSize == 0) { 211 return 0; 212 } 213 214 output.write(buf.bytes,0,buf.realSize); 215 216 if (isSync) { 218 sync(); 219 } 220 221 return buf.realSize; 222 } 223 224 229 public int syswrite(int c) throws IOException , BadDescriptorException { 230 getRuntime().secure(4); 231 checkWriteable(); 232 233 output.write(c); 234 235 if (isSync) { 237 sync(); 238 } 239 240 return 1; 241 } 242 243 public void truncate(long newLength) throws IOException , PipeException { 244 throw new IOHandler.PipeException(); 245 } 246 247 public FileChannel getFileChannel() { 248 assert false : "No file channel for process streams"; 249 return null; 250 } 251 } 252 | Popular Tags |