KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > resources > StreamReadWrite


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.resources;
31
32 import com.caucho.quercus.QuercusException;
33 import com.caucho.quercus.env.Env;
34 import com.caucho.quercus.env.StringValue;
35 import com.caucho.quercus.env.StringValueImpl;
36 import com.caucho.vfs.ReadStream;
37 import com.caucho.vfs.WriteStream;
38
39 import java.io.IOException JavaDoc;
40
41 /**
42  * Represents read/write stream
43  */

44 public class StreamReadWrite extends StreamResource {
45   private ReadStream _is;
46   private WriteStream _os;
47
48   public StreamReadWrite(Env env)
49   {
50     env.addClose(this);
51   }
52
53   public StreamReadWrite(Env env, ReadStream is, WriteStream os)
54   {
55     this(env);
56
57     init(is, os);
58   }
59
60   protected void init(ReadStream is, WriteStream os)
61   {
62     _is = is;
63     _os = os;
64   }
65   
66   /**
67    * Reads the next byte, returning -1 on eof.
68    */

69   public int read()
70     throws IOException JavaDoc
71   {
72     if (_is != null)
73       return _is.read();
74     else
75       return -1;
76   }
77   
78   /**
79    * Reads a buffer, returning -1 on eof.
80    */

81   public int read(byte []buffer, int offset, int length)
82     throws IOException JavaDoc
83   {
84     if (_is != null)
85       return _is.read(buffer, offset, length);
86     else
87       return -1;
88   }
89
90   /**
91    * Reads the optional linefeed character from a \r\n
92    */

93   public boolean readOptionalLinefeed()
94     throws IOException JavaDoc
95   {
96     if (_is != null) {
97       int ch = _is.read();
98
99       if (ch == '\n') {
100         return true;
101       }
102       else {
103         _is.unread();
104         return false;
105       }
106     }
107     else
108       return false;
109   }
110   
111   /**
112    * Reads a line from the buffer.
113    */

114   public StringValue readLine()
115     throws IOException JavaDoc
116   {
117     if (_is != null)
118       return new StringValueImpl(_is.readLineNoChop());
119     else
120       return StringValueImpl.EMPTY;
121   }
122
123   /**
124    * Reads a line from the stream into a buffer.
125    */

126   public int readLine(char []buffer)
127   {
128     try {
129       if (_is != null)
130         return _is.readLine(buffer, buffer.length, false);
131       else
132         return -1;
133     } catch (IOException JavaDoc e) {
134       return -1;
135     }
136   }
137   
138   /**
139    * Writes to a buffer.
140    */

141   public int write(byte []buffer, int offset, int length)
142     throws IOException JavaDoc
143   {
144     if (_os != null) {
145       _os.write(buffer, offset, length);
146
147       return length;
148     } else {
149       return -1;
150     }
151   }
152   
153   /**
154    * prints
155    */

156   public void print(char ch)
157     throws IOException JavaDoc
158   {
159     print(String.valueOf(ch));
160   }
161   
162   /**
163    * prints
164    */

165   public void print(String JavaDoc s)
166     throws IOException JavaDoc
167   {
168     if (_os != null)
169       _os.print(s);
170   }
171
172   /**
173    * Returns true on the end of file.
174    */

175   public boolean isEOF()
176   {
177     return true;
178   }
179
180   /**
181    * Flushes the output
182    */

183   public void flush()
184   {
185     try {
186       if (_os != null)
187     _os.flush();
188     } catch (IOException JavaDoc e) {
189       throw new QuercusException(e);
190     }
191   }
192
193   /**
194    * Returns the current location in the file.
195    */

196   public long getPosition()
197   {
198     return 0;
199   }
200
201   /**
202    * Closes the stream for reading.
203    */

204   public void closeRead()
205   {
206     ReadStream is = _is;
207     _is = null;
208
209     if (is != null)
210       is.close();
211   }
212
213   /**
214    * Closes the stream for writing
215    */

216   public void closeWrite()
217   {
218     WriteStream os = _os;
219     _os = null;
220
221     try {
222       if (os != null)
223     os.close();
224     } catch (IOException JavaDoc e) {
225     }
226   }
227
228   /**
229    * Closes the stream.
230    */

231   public void close()
232   {
233     ReadStream is = _is;
234     _is = null;
235     
236     WriteStream os = _os;
237     _os = null;
238
239     if (is != null)
240       is.close();
241
242     try {
243       if (os != null)
244     os.close();
245     } catch (IOException JavaDoc e) {
246     }
247   }
248 }
249
250
Popular Tags