KickJava   Java API By Example, From Geeks To Geeks.

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


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 Anders Bengtsson <ndrsbngtssn@yahoo.se>
15  * Copyright (C) 2004 Thomas E Enebo <enebo@acm.org>
16  * Copyright (C) 2004 Jan Arne Petersen <jpetersen@uni-bonn.de>
17  * Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
18  * Copyright (C) 2006 Evan Buswell <evan@heron.sytes.net>
19  *
20  * Alternatively, the contents of this file may be used under the terms of
21  * either of the GNU General Public License Version 2 or later (the "GPL"),
22  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
23  * in which case the provisions of the GPL or the LGPL are applicable instead
24  * of those above. If you wish to allow use of your version of this file only
25  * under the terms of either the GPL or the LGPL, and not to allow others to
26  * use your version of this file under the terms of the CPL, indicate your
27  * decision by deleting the provisions above and replace them with the notice
28  * and other provisions required by the GPL or the LGPL. If you do not delete
29  * the provisions above, a recipient may use your version of this file under
30  * the terms of any one of the CPL, the GPL or the LGPL.
31  ***** END LICENSE BLOCK *****/

32 package org.jruby.util;
33
34 import java.io.InputStream JavaDoc;
35 import java.io.OutputStream JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.io.EOFException JavaDoc;
38
39 import org.jruby.Ruby;
40
41 /**
42  */

43 public abstract class IOHandlerJavaIO extends IOHandler {
44     // Last char to be 'ungot'. <0 indicates nothing waiting to be re-got
45
private int ungotc = -1;
46
47     protected IOHandlerJavaIO(Ruby runtime) {
48     super(runtime);
49     }
50
51     public ByteList gets(ByteList separatorString) throws IOException JavaDoc, BadDescriptorException {
52         checkReadable();
53
54         if (separatorString == null) {
55             return getsEntireStream();
56         }
57
58         final ByteList separator = (separatorString == PARAGRAPH_DELIMETER) ?
59             ByteList.create("\n\n") : separatorString;
60
61         byte c = (byte)read();
62         if (c == -1) {
63             return null;
64         }
65
66         ByteList buffer = new ByteList();
67
68         LineLoop : while (true) {
69             while (c != separator.bytes[0] && c != -1) {
70                 buffer.append(c);
71                 c = (byte)read();
72             }
73             for (int i = 0; i < separator.realSize; i++) {
74                 if (c == -1) {
75                     break LineLoop;
76                 } else if (c != separator.bytes[i]) {
77                     continue LineLoop;
78                 }
79                 buffer.append(c);
80                 if (i < separator.realSize - 1) {
81                     c = (byte)read();
82                 }
83             }
84             break;
85         }
86
87         if (separatorString == PARAGRAPH_DELIMETER) {
88             while (c == separator.bytes[0]) {
89                 c = (byte)read();
90             }
91             ungetc(c);
92         }
93
94         return buffer;
95     }
96
97     public ByteList getsEntireStream() throws IOException JavaDoc {
98         ByteList result = new ByteList();
99         int c;
100         while ((c = (byte)read()) != -1) {
101             result.append(c);
102         }
103
104         // We are already at EOF
105
if (result.realSize == 0) {
106             return null;
107         }
108
109         return result;
110     }
111
112     public int read() throws IOException JavaDoc {
113         try {
114             if (ungotc >= 0) {
115                 int c = ungotc;
116                 ungotc = -1;
117                 return c;
118             }
119
120             return sysread();
121         } catch (EOFException JavaDoc e) {
122             return -1;
123         }
124     }
125
126     public int getc() throws IOException JavaDoc, BadDescriptorException {
127         checkReadable();
128
129         int c = read();
130
131         if (c == -1) {
132             return c;
133         }
134         return c & 0xff;
135     }
136
137     public ByteList read(int number) throws IOException JavaDoc, BadDescriptorException {
138         try {
139
140             if (ungotc >= 0) {
141                 ByteList buf2 = sysread(number - 1);
142                 buf2.prepend((byte)ungotc);
143                 ungotc = -1;
144                 return buf2;
145             }
146
147             return sysread(number);
148         } catch (EOFException JavaDoc e) {
149             return null;
150         }
151     }
152
153     public void ungetc(int c) {
154         // Ruby silently ignores negative ints for some reason?
155
if (c >= 0) {
156             ungotc = c;
157         }
158     }
159
160     public void putc(int c) throws IOException JavaDoc, BadDescriptorException {
161         try {
162             syswrite(c);
163         } catch (IOException JavaDoc e) {
164         }
165     }
166
167     public int write(ByteList string) throws IOException JavaDoc, BadDescriptorException {
168         return syswrite(string);
169     }
170
171     protected int sysread(ByteList buf, int length) throws IOException JavaDoc {
172         if (buf == null) {
173             throw new IOException JavaDoc("sysread2: Buf is null");
174         }
175
176         int i = 0;
177         for (;i < length; i++) {
178             int c = sysread();
179
180             if (c == -1) {
181                 if (i <= 0) {
182                     return -1;
183                 }
184                 break;
185             }
186
187             buf.append(c);
188         }
189
190         return i;
191     }
192
193     // Question: We should read bytes or chars?
194
public ByteList sysread(int number) throws IOException JavaDoc, BadDescriptorException {
195         if (!isOpen()) {
196             throw new IOException JavaDoc("File not open");
197         }
198         checkReadable();
199
200         ByteList buf = new ByteList(number);
201         int position = 0;
202
203         while (position < number) {
204             int s = sysread(buf, number - position);
205
206             if (s == -1) {
207                 if (position <= 0) {
208                     throw new EOFException JavaDoc();
209                 }
210                 break;
211             }
212
213             position += s;
214         }
215
216         return buf;
217     }
218
219     public abstract int sysread() throws IOException JavaDoc;
220
221     public abstract InputStream JavaDoc getInputStream();
222     public abstract OutputStream JavaDoc getOutputStream();
223 }
224
Popular Tags