KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > lib > file > ReadStreamInput


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.quercus.lib.file;
31
32 import com.caucho.quercus.QuercusModuleException;
33 import com.caucho.quercus.env.BinaryBuilderValue;
34 import com.caucho.quercus.env.BinaryValue;
35 import com.caucho.quercus.env.BooleanValue;
36 import com.caucho.quercus.env.StringBuilderValue;
37 import com.caucho.quercus.env.StringValue;
38 import com.caucho.quercus.env.Value;
39 import com.caucho.vfs.ReadStream;
40
41 import java.io.IOException JavaDoc;
42 import java.io.InputStream JavaDoc;
43 import java.io.OutputStream JavaDoc;
44 import java.io.UnsupportedEncodingException JavaDoc;
45 import java.util.logging.Level JavaDoc;
46 import java.util.logging.Logger JavaDoc;
47
48 /**
49  * Represents a Quercus file open for reading
50  */

51 public class ReadStreamInput extends InputStream JavaDoc implements BinaryInput {
52   private static final Logger JavaDoc log
53     = Logger.getLogger(ReadStreamInput.class.getName());
54
55   private ReadStream _is;
56
57   public ReadStreamInput()
58   {
59   }
60
61   public ReadStreamInput(ReadStream is)
62   {
63     init(is);
64   }
65
66   public void init(ReadStream is)
67   {
68     _is = is;
69   }
70
71   /**
72    * Returns the input stream.
73    */

74   public InputStream JavaDoc getInputStream()
75   {
76     return _is;
77   }
78
79   /**
80    * Opens a copy.
81    */

82   public BinaryInput openCopy()
83     throws IOException JavaDoc
84   {
85     return new ReadStreamInput(_is.getPath().openRead());
86   }
87
88   public void setEncoding(String JavaDoc encoding)
89     throws UnsupportedEncodingException JavaDoc
90   {
91     if (_is != null)
92       _is.setEncoding(encoding);
93   }
94
95   /**
96    *
97    */

98   public void unread()
99     throws IOException JavaDoc
100   {
101     if (_is != null)
102       _is.unread();
103   }
104
105   /**
106    * Reads a character from a file, returning -1 on EOF.
107    */

108   public int read()
109     throws IOException JavaDoc
110   {
111     if (_is != null)
112       return _is.read();
113     else
114       return -1;
115   }
116
117   /**
118    * Reads a buffer from a file, returning -1 on EOF.
119    */

120   public int read(byte []buffer, int offset, int length)
121     throws IOException JavaDoc
122   {
123     if (_is != null) {
124       return _is.read(buffer, offset, length);
125     }
126     else
127       return -1;
128   }
129
130   /**
131    * Reads into a binary builder.
132    */

133   public BinaryValue read(int length)
134     throws IOException JavaDoc
135     {
136       if (_is == null)
137         return null;
138
139       BinaryBuilderValue bb = new BinaryBuilderValue();
140
141       while (length > 0) {
142         bb.prepareReadBuffer();
143
144         int sublen = bb.getLength() - bb.getOffset();
145
146         if (length < sublen)
147           sublen = length;
148
149         sublen = read(bb.getBuffer(), bb.getOffset(), sublen);
150
151         if (sublen > 0) {
152           bb.setOffset(bb.getOffset() + sublen);
153           length -= sublen;
154         }
155         else
156           return bb;
157       }
158
159       return bb;
160     }
161
162   /**
163    * Reads the optional linefeed character from a \r\n
164    */

165   public boolean readOptionalLinefeed()
166     throws IOException JavaDoc
167   {
168     if (_is == null)
169       return false;
170     
171     int ch = _is.read();
172
173     if (ch == '\n') {
174       return true;
175     }
176     else {
177       _is.unread();
178       return false;
179     }
180   }
181
182   public void writeToStream(OutputStream JavaDoc os, int length)
183     throws IOException JavaDoc
184   {
185     if (_is != null) {
186       _is.writeToStream(os, length);
187     }
188   }
189
190   /**
191    * Reads a line from a file, returning null on EOF.
192    */

193   public StringValue readLine(long length)
194     throws IOException JavaDoc
195   {
196     if (_is == null)
197       return null;
198     
199     StringBuilderValue sb = new StringBuilderValue();
200
201     int ch;
202
203     for (; length > 0 && (ch = _is.readChar()) >= 0; length--) {
204       if (ch == '\n') {
205     sb.append((char) ch);
206     return sb;
207       }
208       else if (ch == '\r') {
209     sb.append('\r');
210     
211     int ch2 = _is.read();
212
213     if (ch == '\n')
214       sb.append('\n');
215     else
216       _is.unread();
217     
218     return sb;
219       }
220       else
221     sb.append((char) ch);
222     }
223
224     if (sb.length() == 0)
225       return null;
226     else
227       return sb;
228   }
229
230   /**
231    * Returns true on the EOF.
232    */

233   public boolean isEOF()
234   {
235     if (_is == null)
236       return true;
237     else {
238       try {
239         // XXX: not quite right for sockets
240
return _is.available() <= 0;
241       } catch (IOException JavaDoc e) {
242         log.log(Level.FINE, e.toString(), e);
243
244         return true;
245       }
246     }
247   }
248
249   /**
250    * Returns the current location in the file.
251    */

252   public long getPosition()
253   {
254     if (_is == null)
255       return -1;
256     else
257       return _is.getPosition();
258   }
259
260   /**
261    * Returns the current location in the file.
262    */

263   public boolean setPosition(long offset)
264   {
265     if (_is == null)
266       return false;
267
268     try {
269       _is.setPosition(offset);
270
271       return true;
272     } catch (IOException JavaDoc e) {
273       throw new QuercusModuleException(e);
274     }
275   }
276
277   public long seek(long offset, int whence)
278   {
279     switch (whence) {
280       case BinaryInput.SEEK_CUR:
281         offset = getPosition() + offset;
282         break;
283       case BinaryInput.SEEK_END:
284         // don't necessarily have an end
285
offset = getPosition();
286         break;
287       case SEEK_SET:
288         break;
289       default:
290         break;
291     }
292
293     setPosition(offset);
294
295     return offset;
296   }
297
298   public Value stat()
299   {
300     return BooleanValue.FALSE;
301   }
302
303   /**
304    * Closes the stream for reading.
305    */

306   public void closeRead()
307   {
308     close();
309   }
310
311   /**
312    * Closes the file.
313    */

314   public void close()
315   {
316     ReadStream is = _is;
317     _is = null;
318
319     if (is != null)
320       is.close();
321   }
322
323   public Object JavaDoc toJavaObject()
324   {
325     return this;
326   }
327
328   public String JavaDoc getResourceType()
329   {
330     return "stream";
331   }
332
333   /**
334    * Converts to a string.
335    */

336   public String JavaDoc toString()
337   {
338     return "ReadStreamInput[" + _is.getPath() + "]";
339   }
340 }
341
342
Popular Tags