KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.FileInputStream JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.io.InputStream JavaDoc;
35
36 /**
37  * Stream encapsulating FileInputStream
38  */

39 public class FileReadStream extends StreamImpl {
40   private FileInputStream JavaDoc _is;
41   
42   /**
43    * Create a new FileReadStream.
44    */

45   public FileReadStream()
46   {
47   }
48   
49   /**
50    * Create a new FileReadStream based on the java.io.* stream.
51    *
52    * @param is the underlying input stream.
53    */

54   public FileReadStream(FileInputStream JavaDoc is)
55   {
56     init(is);
57   }
58   
59   /**
60    * Create a new FileReadStream based on the java.io.* stream.
61    *
62    * @param is the underlying input stream.
63    * @param path the associated Path.
64    */

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

78   public void init(FileInputStream JavaDoc is)
79   {
80     _is = is;
81     setPath(null);
82   }
83
84   /**
85    * Returns true if there's an associated file.
86    */

87   public boolean canSkip()
88   {
89     return _is != null;
90   }
91
92   /**
93    * Skips bytes in the file.
94    *
95    * @param n the number of bytes to skip
96    *
97    * @return the actual bytes skipped.
98    */

99   public long skip(long n)
100     throws IOException JavaDoc
101   {
102     if (_is != null)
103       return _is.skip(n);
104     else
105       return -1;
106   }
107
108   /**
109    * Seeks based on the start.
110    */

111   public void seekStart(long offset)
112     throws IOException JavaDoc
113   {
114     if (_is != null)
115       _is.getChannel().position(offset);
116   }
117
118   /**
119    * Returns true if there's an associated file.
120    */

121   public boolean canRead()
122   {
123     return _is != null;
124   }
125
126   /**
127    * Reads bytes from the file.
128    *
129    * @param buf a byte array receiving the data.
130    * @param offset starting index to receive data.
131    * @param length number of bytes to read.
132    *
133    * @return the number of bytes read or -1 on end of file.
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     return len;
143   }
144
145   /**
146    * Returns the number of bytes available for reading.
147    */

148   public int getAvailable() throws IOException JavaDoc
149   {
150     if (_is == null)
151       return -1;
152     else {
153       return _is.available();
154     }
155   }
156
157   /**
158    * Closes the underlying stream.
159    */

160   public void close() throws IOException JavaDoc
161   {
162     InputStream JavaDoc is = _is;
163     _is = null;
164     
165     if (is != null)
166       is.close();
167   }
168 }
169
Popular Tags