KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > vfs > StreamImpl


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.vfs;
31
32 import com.caucho.util.NullIterator;
33
34 import java.io.IOException JavaDoc;
35 import java.util.Iterator JavaDoc;
36
37 /**
38  * This is the service provider's interface for a stream supported by
39  * the VFS.
40  */

41 public class StreamImpl {
42   protected static NullPath _nullPath;
43   
44   private static final byte _newline[] = new byte[] {(byte) '\n'};
45   
46   protected Path _path;
47
48   /**
49    * Returns the stream's natural newline character.
50    */

51   public byte []getNewline()
52   {
53     return _newline;
54   }
55
56   /**
57    * Returns true if the stream implements skip.
58    */

59   public boolean hasSkip()
60   {
61     return false;
62   }
63
64   /**
65    * Skips a number of bytes, returning the bytes skipped.
66    *
67    * @param n the number of types to skip.
68    *
69    * @return the actual bytes skipped.
70    */

71   public long skip(long n)
72     throws IOException JavaDoc
73   {
74     return 0;
75   }
76
77   /**
78    * Returns true if this is a read stream.
79    */

80   public boolean canRead()
81   {
82     return false;
83   }
84
85   /**
86    * Returns the read buffer.
87    */

88   public byte []getReadBuffer()
89   {
90     return null;
91   }
92
93   /**
94    * Reads the next chunk from the stream.
95    *
96    * @param buffer byte array receiving the data.
97    * @param offset starting offset into the array.
98    * @param length number of bytes to read.
99    *
100    * @return the number of bytes read or -1 on end of file.
101    */

102   public int read(byte []buffer, int offset, int length) throws IOException JavaDoc
103   {
104     throw new UnsupportedOperationException JavaDoc(String.valueOf(this));
105   }
106
107   /**
108    * Reads the next chunk from the stream in non-blocking mode.
109    *
110    * @param buffer byte array receiving the data.
111    * @param offset starting offset into the array.
112    * @param length number of bytes to read.
113    *
114    * @return the number of bytes read or -1 on end of file.
115    */

116   public int readNonBlock(byte []buffer, int offset, int length)
117     throws IOException JavaDoc
118   {
119     return 0;
120   }
121
122   /**
123    * Reads the next chunk from the stream in non-blocking mode.
124    *
125    * @param buffer byte array receiving the data.
126    * @param offset starting offset into the array.
127    * @param length number of bytes to read.
128    *
129    * @return the number of bytes read or -1 on end of file.
130    */

131   public int readTimeout(byte []buffer, int offset, int length,
132              long timeout)
133     throws IOException JavaDoc
134   {
135     return 0;
136   }
137
138   /**
139    * Returns the number of bytes available without blocking. Depending on
140    * the stream, this may return less than the actual bytes, but will always
141    * return a number > 0 if there is any data available.
142    */

143   public int getAvailable() throws IOException JavaDoc
144   {
145     throw new UnsupportedOperationException JavaDoc(String.valueOf(this));
146   }
147
148   /**
149    * Returns the current read position of the underlying file.
150    */

151   public long getReadPosition()
152   {
153     return -1;
154   }
155
156   /**
157    * Returns true if this is a writable stream.
158    */

159   public boolean canWrite()
160   {
161     return false;
162   }
163
164   /**
165    * Returns true if the buffer should be flushed on every newline. This is
166    * typically only true for error streams like stderr:.
167    */

168   public boolean getFlushOnNewline()
169   {
170     return false;
171   }
172
173   /**
174    * Sets the write encoding.
175    */

176   public void setWriteEncoding(String JavaDoc encoding)
177   {
178   }
179
180   /**
181    * Writes a buffer to the underlying stream.
182    *
183    * @param buffer the byte array to write.
184    * @param offset the offset into the byte array.
185    * @param length the number of bytes to write.
186    * @param isEnd true when the write is flushing a close.
187    */

188   public void write(byte []buffer, int offset, int length, boolean isEnd)
189     throws IOException JavaDoc
190   {
191     throw new UnsupportedOperationException JavaDoc(String.valueOf(this));
192   }
193
194   /**
195    * Writes a pair of buffer to the underlying stream.
196    *
197    * @param buf1 the byte array to write.
198    * @param off1 the offset into the byte array.
199    * @param len1 the number of bytes to write.
200    * @param buf2 the byte array to write.
201    * @param off2 the offset into the byte array.
202    * @param len2 the number of bytes to write.
203    * @param isEnd true when the write is flushing a close.
204    */

205   public boolean write(byte []buf1, int off1, int len1,
206                byte []buf2, int off2, int len2,
207                boolean isEnd)
208     throws IOException JavaDoc
209   {
210     if (len1 == 0) {
211       write(buf2, off2, len2, isEnd);
212
213       return true;
214     }
215     else
216       return false;
217   }
218
219   /**
220    * Clears any buffered values in the write.
221    */

222   public void clearWrite()
223   {
224   }
225
226   /**
227    * Seeks based on the start.
228    */

229   public void seekStart(long offset)
230     throws IOException JavaDoc
231   {
232     throw new UnsupportedOperationException JavaDoc(getClass().getName());
233   }
234
235   /**
236    * Seeks based on the end.
237    */

238   public void seekEnd(long offset)
239     throws IOException JavaDoc
240   {
241     throw new UnsupportedOperationException JavaDoc(getClass().getName());
242   }
243
244   /**
245    * Flushes buffered writes.
246    */

247   public void flushBuffer() throws IOException JavaDoc
248   {
249   }
250
251   /**
252    * Flushes the write output.
253    */

254   public void flush() throws IOException JavaDoc
255   {
256   }
257
258   /**
259    * Flushes the write output, forcing to disk.
260    */

261   public void flushToDisk() throws IOException JavaDoc
262   {
263     throw new UnsupportedOperationException JavaDoc();
264   }
265
266   /**
267    * Returns the Path associated with the stream.
268    */

269   public Path getPath()
270   {
271     if (_path != null)
272       return _path;
273
274     if (_nullPath == null)
275       _nullPath = new NullPath("stream");
276
277     return _nullPath;
278   }
279
280   /**
281    * Sets the Path associated with the stream.
282    */

283   public void setPath(Path path)
284   {
285     _path = path;
286   }
287
288   /**
289    * Returns a stream attribute.
290    *
291    * @param name the attribute name.
292    *
293    * @return the attribute value.
294    */

295   public Object JavaDoc getAttribute(String JavaDoc name)
296     throws IOException JavaDoc
297   {
298     return null;
299   }
300
301   /**
302    * Sets a stream attribute.
303    *
304    * @param name the attribute name.
305    * @param value the attribute value.
306    */

307   public void setAttribute(String JavaDoc name, Object JavaDoc value)
308     throws IOException JavaDoc
309   {
310   }
311
312   /**
313    * Removes a stream attribute.
314    *
315    * @param name the attribute name.
316    */

317   public void removeAttribute(String JavaDoc name)
318     throws IOException JavaDoc
319   {
320   }
321
322   /**
323    * Returns an iterator of the attribute names.
324    */

325   public Iterator getAttributeNames()
326     throws IOException JavaDoc
327   {
328     return NullIterator.create();
329   }
330
331   /**
332    * Closes the write half of the stream.
333    */

334   public void closeWrite() throws IOException JavaDoc
335   {
336     close();
337   }
338
339   /**
340    * Closes the stream.
341    */

342   public void close() throws IOException JavaDoc
343   {
344   }
345 }
346
Popular Tags