KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > util > IOHandlerProcess


1 /***** BEGIN LICENSE BLOCK *****
2  * Version: CPL 1.0/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Common Public
5  * License Version 1.0 (the "License"); you may not use this file
6  * except in compliance with the License. You may obtain a copy of
7  * the License at http://www.eclipse.org/legal/cpl-v10.html
8  *
9  * Software distributed under the License is distributed on an "AS
10  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * rights and limitations under the License.
13  *
14  * Copyright (C) 2004 Thomas E Enebo <enebo@acm.org>
15  * Copyright (C) 2004 Jan Arne Petersen <jpetersen@uni-bonn.de>
16  * Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
17  * Copyright (C) 2005 Charles O Nutter <headius@headius.com>
18  *
19  * Alternatively, the contents of this file may be used under the terms of
20  * either of the GNU General Public License Version 2 or later (the "GPL"),
21  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
22  * in which case the provisions of the GPL or the LGPL are applicable instead
23  * of those above. If you wish to allow use of your version of this file only
24  * under the terms of either the GPL or the LGPL, and not to allow others to
25  * use your version of this file under the terms of the CPL, indicate your
26  * decision by deleting the provisions above and replace them with the notice
27  * and other provisions required by the GPL or the LGPL. If you do not delete
28  * the provisions above, a recipient may use your version of this file under
29  * the terms of any one of the CPL, the GPL or the LGPL.
30  ***** END LICENSE BLOCK *****/

31 package org.jruby.util;
32
33 import java.io.IOException JavaDoc;
34 import java.io.InputStream JavaDoc;
35 import java.io.OutputStream JavaDoc;
36 import java.nio.channels.FileChannel JavaDoc;
37
38 import org.jruby.Ruby;
39 import org.jruby.RubyIO;
40
41 public class IOHandlerProcess extends IOHandlerJavaIO {
42     protected InputStream JavaDoc input = null;
43     protected OutputStream JavaDoc output = null;
44     protected Process JavaDoc process = null;
45
46     public IOHandlerProcess(Ruby runtime, Process JavaDoc process, IOModes modes) throws IOException JavaDoc {
47         super(runtime);
48         
49         if (process == null) {
50             throw new IOException JavaDoc("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 JavaDoc {
65         // may need to pass streams instead?
66
return new IOHandlerProcess(getRuntime(), process, modes);
67     }
68
69     /**
70      * <p>Close IO handler resources.</p>
71      * @throws IOException
72      * @throws BadDescriptorException
73      *
74      * @see org.jruby.util.IOHandler#close()
75      */

76     public void close() throws IOException JavaDoc, BadDescriptorException {
77         if (!isOpen()) {
78             throw new BadDescriptorException();
79         }
80         
81         isOpen = false;
82
83         input.close();
84         output.close();
85         
86         // TODO: to destroy or not destroy the process?
87
process.destroy();
88         process = null;
89     }
90
91     /**
92      * @throws IOException
93      * @throws BadDescriptorException
94      * @see org.jruby.util.IOHandler#flush()
95      */

96     public void flush() throws IOException JavaDoc, BadDescriptorException {
97         checkWriteable();
98
99         output.flush();
100     }
101     
102     /**
103      * @see org.jruby.util.IOHandler#getInputStream()
104      */

105     public InputStream JavaDoc getInputStream() {
106         return input;
107     }
108
109     /**
110      * @see org.jruby.util.IOHandler#getOutputStream()
111      */

112     public OutputStream JavaDoc getOutputStream() {
113         return output;
114     }
115
116     /**
117      * @throws IOException
118      * @throws BadDescriptorException
119      * @see org.jruby.util.IOHandler#isEOF()
120      */

121     public boolean isEOF() throws IOException JavaDoc, 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     /**
133      * @see org.jruby.util.IOHandler#pid()
134      */

135     public int pid() {
136         // no way to get pid, so faking it
137
return process.hashCode();
138     }
139     
140     /**
141      * @throws PipeException
142      * @see org.jruby.util.IOHandler#pos()
143      */

144     public long pos() throws PipeException {
145         throw new IOHandler.PipeException();
146     }
147     
148     public void resetByModes(IOModes newModes) {
149     }
150     
151     /**
152      * @throws PipeException
153      * @see org.jruby.util.IOHandler#rewind()
154      */

155     public void rewind() throws PipeException {
156         throw new IOHandler.PipeException();
157     }
158     
159     /**
160      * @throws PipeException
161      * @see org.jruby.util.IOHandler#seek(long, int)
162      */

163     public void seek(long offset, int type) throws PipeException {
164         throw new IOHandler.PipeException();
165     }
166     
167     /**
168      * @see org.jruby.util.IOHandler#sync()
169      */

170     public void sync() throws IOException JavaDoc {
171         output.flush();
172     }
173     
174     /**
175      * @see org.jruby.util.IOHandler#sysread()
176      */

177     public int sysread() throws IOException JavaDoc {
178         return input.read();
179     }
180
181     public ByteList sysread(int number) throws IOException JavaDoc, 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 JavaDoc();
191                 } else {
192                     break;
193                 }
194             }
195             read += n;
196         }
197         
198         return new ByteList(buf, 0, read, false);
199     }
200
201     /**
202      * @throws IOException
203      * @throws BadDescriptorException
204      * @see org.jruby.util.IOHandler#syswrite(String buf)
205      */

206     public int syswrite(ByteList buf) throws IOException JavaDoc, 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         // Should syswrite sync?
217
if (isSync) {
218             sync();
219         }
220             
221         return buf.realSize;
222     }
223
224     /**
225      * @throws IOException
226      * @throws BadDescriptorException
227      * @see org.jruby.util.IOHandler#syswrite(String buf)
228      */

229     public int syswrite(int c) throws IOException JavaDoc, BadDescriptorException {
230         getRuntime().secure(4);
231         checkWriteable();
232         
233         output.write(c);
234
235         // Should syswrite sync?
236
if (isSync) {
237             sync();
238         }
239             
240         return 1;
241     }
242     
243     public void truncate(long newLength) throws IOException JavaDoc, PipeException {
244         throw new IOHandler.PipeException();
245     }
246
247     public FileChannel JavaDoc getFileChannel() {
248         assert false : "No file channel for process streams";
249         return null;
250     }
251 }
252
Popular Tags