KickJava   Java API By Example, From Geeks To Geeks.

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


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 import com.caucho.vfs.TempBuffer;
41 import com.caucho.vfs.WriteStream;
42
43 import java.io.Closeable JavaDoc;
44 import java.io.IOException JavaDoc;
45 import java.io.InputStream JavaDoc;
46 import java.io.OutputStream JavaDoc;
47 import java.io.UnsupportedEncodingException JavaDoc;
48 import java.util.logging.Level JavaDoc;
49 import java.util.logging.Logger JavaDoc;
50
51 /**
52  * Represents a Quercus file open for reading
53  */

54 public class AbstractBinaryInputOutput
55   implements BinaryInput, BinaryOutput, Closeable JavaDoc
56 {
57   private static final Logger JavaDoc log
58     = Logger.getLogger(AbstractBinaryInputOutput.class.getName());
59
60   private ReadStream _is;
61   private WriteStream _os;
62
63   protected AbstractBinaryInputOutput()
64   {
65   }
66
67   protected AbstractBinaryInputOutput(ReadStream is, WriteStream os)
68   {
69     init(is, os);
70   }
71
72   public void init(ReadStream is, WriteStream os)
73   {
74     _is = is;
75     _os = os;
76   }
77
78   //
79
// read methods
80
//
81

82   /**
83    * Returns the input stream.
84    */

85   public InputStream JavaDoc getInputStream()
86   {
87     return _is;
88   }
89
90   /**
91    * Opens a copy.
92    */

93   public BinaryInput openCopy()
94     throws IOException JavaDoc
95   {
96     throw new UnsupportedOperationException JavaDoc(getClass().getName());
97   }
98
99   public void setEncoding(String JavaDoc encoding)
100     throws UnsupportedEncodingException JavaDoc
101   {
102     if (_is != null)
103       _is.setEncoding(encoding);
104   }
105
106   /**
107    *
108    */

109   public void unread()
110     throws IOException JavaDoc
111   {
112     if (_is != null)
113       _is.unread();
114   }
115
116   /**
117    * Reads a character from a file, returning -1 on EOF.
118    */

119   public int read()
120     throws IOException JavaDoc
121   {
122     if (_is != null)
123       return _is.read();
124     else
125       return -1;
126   }
127
128   /**
129    * Reads a buffer from a file, returning -1 on EOF.
130    */

131   public int read(byte []buffer, int offset, int length)
132     throws IOException JavaDoc
133   {
134     if (_is != null) {
135       return _is.read(buffer, offset, length);
136     }
137     else
138       return -1;
139   }
140
141   /**
142    * Reads into a binary builder.
143    */

144   public BinaryValue read(int length)
145     throws IOException JavaDoc
146   {
147     if (_is == null)
148       return null;
149
150     BinaryBuilderValue bb = new BinaryBuilderValue();
151
152     while (length > 0) {
153       bb.prepareReadBuffer();
154
155       int sublen = bb.getLength() - bb.getOffset();
156
157       if (length < sublen)
158     sublen = length;
159
160       sublen = read(bb.getBuffer(), bb.getOffset(), sublen);
161
162       if (sublen > 0) {
163     bb.setOffset(bb.getOffset() + sublen);
164     length -= sublen;
165       }
166       else
167     return bb;
168     }
169
170     return bb;
171   }
172
173   /**
174    * Reads the optional linefeed character from a \r\n
175    */

176   public boolean readOptionalLinefeed()
177     throws IOException JavaDoc
178   {
179     if (_is == null)
180       return false;
181     
182     int ch = _is.read();
183
184     if (ch == '\n') {
185       return true;
186     }
187     else {
188       _is.unread();
189       return false;
190     }
191   }
192
193   public void writeToStream(OutputStream JavaDoc os, int length)
194     throws IOException JavaDoc
195   {
196     if (_is != null) {
197       _is.writeToStream(os, length);
198     }
199   }
200
201   /**
202    * Reads a line from a file, returning null on EOF.
203    */

204   public StringValue readLine(long length)
205     throws IOException JavaDoc
206   {
207     if (_is == null)
208       return null;
209     
210     StringBuilderValue sb = new StringBuilderValue();
211
212     int ch;
213
214     for (; length > 0 && (ch = _is.readChar()) >= 0; length--) {
215       if (ch == '\n') {
216     sb.append((char) ch);
217     return sb;
218       }
219       else if (ch == '\r') {
220     sb.append('\r');
221     
222     int ch2 = _is.read();
223
224     if (ch2 == '\n')
225       sb.append('\n');
226     else
227       _is.unread();
228     
229     return sb;
230       }
231       else
232     sb.append((char) ch);
233     }
234
235     if (sb.length() == 0)
236       return null;
237     else
238       return sb;
239   }
240
241   /**
242    * Returns true on the EOF.
243    */

244   public boolean isEOF()
245   {
246     if (_is == null)
247       return true;
248     else {
249       try {
250         // XXX: not quite right for sockets
251
return _is.available() <= 0;
252       } catch (IOException JavaDoc e) {
253         log.log(Level.FINE, e.toString(), e);
254
255         return true;
256       }
257     }
258   }
259
260   /**
261    * Returns the current location in the file.
262    */

263   public long getPosition()
264   {
265     if (_is == null)
266       return -1;
267     else
268       return _is.getPosition();
269   }
270
271   /**
272    * Returns the current location in the file.
273    */

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

317   public void closeRead()
318   {
319     ReadStream is = _is;
320     _is = null;
321
322     if (is != null)
323       is.close();
324   }
325
326   //
327
// write methods
328
//
329

330   /**
331    * Returns self as the output stream.
332    */

333   public OutputStream JavaDoc getOutputStream()
334   {
335     return _os;
336   }
337
338   public void write(int ch)
339     throws IOException JavaDoc
340   {
341     _os.write(ch);
342   }
343
344   public void write(byte []buffer, int offset, int length)
345     throws IOException JavaDoc
346   {
347     _os.write(buffer, offset, length);
348   }
349
350   /**
351    * Writes to a stream.
352    */

353   public int write(InputStream JavaDoc is, int length)
354   {
355     int writeLength = 0;
356
357     TempBuffer tb = TempBuffer.allocate();
358     byte []buffer = tb.getBuffer();
359
360     try {
361       while (length > 0) {
362     int sublen;
363
364     if (length < buffer.length)
365       sublen = length;
366     else
367       sublen = buffer.length;
368
369     sublen = is.read(buffer, 0, sublen);
370
371     if (sublen < 0)
372       break;
373
374     write(buffer, 0, sublen);
375
376     writeLength += sublen;
377     length -= sublen;
378       }
379
380       return writeLength;
381     } catch (IOException JavaDoc e) {
382       throw new QuercusModuleException(e);
383     } finally {
384       TempBuffer.free(tb);
385     }
386   }
387   
388   /**
389    * Prints a string to a file.
390    */

391   public void print(char v)
392     throws IOException JavaDoc
393   {
394     write((byte) v);
395   }
396
397   /**
398    * Prints a string to a file.
399    */

400   public void print(String JavaDoc v)
401     throws IOException JavaDoc
402   {
403     _os.print(v);
404   }
405
406   /**
407    * Flushes the output.
408    */

409   public void flush()
410     throws IOException JavaDoc
411   {
412     _os.flush();
413   }
414
415
416   /**
417    * Closes the file.
418    */

419   public void closeWrite()
420   {
421     try {
422       WriteStream os = _os;
423       _os = null;
424
425       if (os != null)
426         os.close();
427     } catch (IOException JavaDoc e) {
428       log.log(Level.FINE, e.toString(), e);
429     }
430   }
431
432   /**
433    * Closes the file.
434    */

435   public void close()
436   {
437     closeRead();
438     closeWrite();
439   }
440
441   public Object JavaDoc toJavaObject()
442   {
443     return this;
444   }
445
446   public String JavaDoc getResourceType()
447   {
448     return "stream";
449   }
450
451   /**
452    * Converts to a string.
453    */

454   public String JavaDoc toString()
455   {
456     if (_is != null)
457       return "AbstractBinaryInputOutput[" + _is.getPath() + "]";
458     else
459       return "AbstractBinaryInputOutput[closed]";
460   }
461 }
462
463
Popular Tags