KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.vfs;
30
31 import java.io.IOException JavaDoc;
32 import java.io.InputStream JavaDoc;
33 import java.io.OutputStream JavaDoc;
34
35 /**
36  * Stream encapsulating InputStream/OutputStream.
37  */

38 public class VfsStream extends StreamImpl {
39   private static byte []unixNewline = new byte[] { (byte) '\n' };
40
41   private InputStream JavaDoc is;
42   private OutputStream JavaDoc os;
43   private boolean flushOnNewline;
44   private boolean closeChildOnClose = true;
45   private byte []newline = unixNewline;
46
47   private long position;
48
49   /**
50    * Create an empty VfsStream.
51    */

52   public VfsStream()
53   {
54   }
55
56   /**
57    * Create a new VfsStream based on the java.io.* stream.
58    */

59   public VfsStream(InputStream JavaDoc is, OutputStream JavaDoc os)
60   {
61     init(is, os);
62   }
63
64   public VfsStream(InputStream JavaDoc is, OutputStream JavaDoc os, Path path)
65   {
66     init(is, os);
67     setPath(path);
68   }
69
70   /**
71    * Initializes a VfsStream with an input/output stream pair. Before a
72    * read, the output will be flushed to avoid deadlocks.
73    *
74    * @param is the underlying InputStream.
75    * @param os the underlying OutputStream.
76    */

77   public void init(InputStream JavaDoc is, OutputStream JavaDoc os)
78   {
79     this.is = is;
80     this.os = os;
81     setPath(null);
82     flushOnNewline = false;
83     closeChildOnClose = true;
84     position = 0;
85   }
86
87   public void setNewline(byte []newline)
88   {
89     this.newline = newline;
90   }
91
92   public byte []getNewline()
93   {
94     return newline;
95   }
96
97   public static ReadWritePair openReadWrite(InputStream JavaDoc is, OutputStream JavaDoc os)
98   {
99     VfsStream s = new VfsStream(is, os);
100     WriteStream writeStream = new WriteStream(s);
101     ReadStream readStream = new ReadStream(s, writeStream);
102     return new ReadWritePair(readStream, writeStream);
103   }
104
105   /**
106    * Opens a read stream based on a java.io.InputStream.
107    *
108    * @param is the underlying InputStream.
109    *
110    * @return the new ReadStream
111    */

112   public static ReadStream openRead(InputStream JavaDoc is)
113   {
114     VfsStream s = new VfsStream(is, null);
115     return new ReadStream(s);
116   }
117
118   public static ReadStream openRead(InputStream JavaDoc is, WriteStream ws)
119   {
120     VfsStream s = new VfsStream(is, null);
121     return new ReadStream(s, ws);
122   }
123
124   public static WriteStream openWrite(OutputStream JavaDoc os)
125   {
126     VfsStream s = new VfsStream(null, os);
127     return new WriteStream(s);
128   }
129
130   public boolean canRead()
131   {
132     return is != null;
133   }
134
135   public int read(byte []buf, int offset, int length) throws IOException JavaDoc
136   {
137     if (is == null)
138       return -1;
139
140     int len = is.read(buf, offset, length);
141
142     if (len > 0)
143       position += len;
144
145     return len;
146   }
147
148   public boolean hasSkip()
149   {
150     return true;
151   }
152
153   public long skip(long n)
154     throws IOException JavaDoc
155   {
156     return is.skip(n);
157   }
158
159   public int getAvailable() throws IOException JavaDoc
160   {
161     if (is == null)
162       return -1;
163     else
164       return is.available();
165   }
166
167   public long getReadPosition()
168   {
169     return position;
170   }
171
172   public boolean canWrite()
173   {
174     return os != null;
175   }
176
177   public boolean getFlushOnNewline()
178   {
179     return flushOnNewline;
180   }
181
182   public void setFlushOnNewline(boolean value)
183   {
184     flushOnNewline = value;
185   }
186
187   /**
188    * Writes a buffer to the underlying stream.
189    *
190    * @param buffer the byte array to write.
191    * @param offset the offset into the byte array.
192    * @param length the number of bytes to write.
193    * @param isEnd true when the write is flushing a close.
194    */

195   public void write(byte []buf, int offset, int length, boolean isEnd)
196     throws IOException JavaDoc
197   {
198     if (os != null)
199       os.write(buf, offset, length);
200   }
201
202   public void flushToDisk() throws IOException JavaDoc
203   {
204     flush();
205   }
206
207   public void flush() throws IOException JavaDoc
208   {
209     if (os != null) {
210       os.flush();
211     }
212   }
213
214   public void setCloseChildOnClose(boolean close)
215   {
216     closeChildOnClose = close;
217   }
218
219   public void close() throws IOException JavaDoc
220   {
221     try {
222       if (os != null && closeChildOnClose) {
223         os.close();
224         os = null;
225       }
226     } finally {
227       if (is != null && closeChildOnClose) {
228         is.close();
229         is = null;
230       }
231     }
232   }
233 }
234
Popular Tags