KickJava   Java API By Example, From Geeks To Geeks.

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


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.env.StringValue;
33 import com.caucho.quercus.env.StringValueImpl;
34 import com.caucho.vfs.Path;
35 import com.caucho.vfs.ReadStream;
36
37 import java.io.IOException JavaDoc;
38 import java.io.OutputStream JavaDoc;
39 import java.util.logging.Level JavaDoc;
40 import java.util.logging.Logger JavaDoc;
41
42 /**
43  * Represents a Quercus open file
44  */

45 public class FileReadValue extends FileValue {
46   private static final Logger JavaDoc log
47     = Logger.getLogger(FileReadValue.class.getName());
48
49   private ReadStream _is;
50   private long _offset;
51
52   public FileReadValue(Path path)
53     throws IOException JavaDoc
54   {
55     super(path);
56
57     _is = path.openRead();
58   }
59
60   /**
61    * Returns the number of bytes available to be read, 0 if no known.
62    */

63   public long getLength()
64   {
65     return getPath().getLength();
66   }
67
68   /**
69    * Reads a character from a file, returning -1 on EOF.
70    */

71   public int read()
72     throws IOException JavaDoc
73   {
74     if (_is != null) {
75       int v = _is.read();
76
77       if (v >= 0)
78         _offset++;
79       else
80         close();
81
82       return v;
83     }
84     else
85       return -1;
86   }
87
88   /**
89    * Reads a buffer from a file, returning -1 on EOF.
90    */

91   public int read(byte []buffer, int offset, int length)
92     throws IOException JavaDoc
93   {
94     if (_is != null) {
95       int len = _is.read(buffer, offset, length);
96
97       if (len >= 0)
98         _offset += len;
99       else
100         close();
101
102       return len;
103     }
104     else
105       return -1;
106   }
107
108   /**
109    * Reads the optional linefeed character from a \r\n
110    */

111   public boolean readOptionalLinefeed()
112     throws IOException JavaDoc
113   {
114     if (_is != null) {
115       int ch = _is.read();
116
117       if (ch == '\n') {
118         _offset++;
119         return true;
120       }
121       else {
122         _is.unread();
123         return false;
124       }
125     }
126     else
127       return false;
128   }
129
130   @Override JavaDoc
131   public void writeToStream(OutputStream JavaDoc os, int length)
132     throws IOException JavaDoc
133   {
134     if (_is != null) {
135       _is.writeToStream(os, length);
136     }
137   }
138
139   /**
140    * Reads a line from a file, returning null on EOF.
141    */

142   public StringValue readLine()
143     throws IOException JavaDoc
144   {
145     // XXX: offset messed up
146

147     if (_is != null)
148       return new StringValueImpl(_is.readLineNoChop());
149     else
150       return null;
151   }
152
153   /**
154    * Returns true on the EOF.
155    */

156   public boolean isEOF()
157   {
158     if (_is == null)
159       return true;
160     else {
161       try {
162         // XXX: not quite right for sockets
163
return _is.available() <= 0;
164       } catch (IOException JavaDoc e) {
165         log.log(Level.FINE, e.toString(), e);
166
167         return true;
168       }
169     }
170   }
171
172   /**
173    * Returns the current location in the file.
174    */

175   public long getPosition()
176   {
177     if (_is == null)
178       return -1;
179     else
180       return _is.getPosition();
181   }
182
183   /**
184    * Closes the file.
185    */

186   public void close()
187   {
188     ReadStream is = _is;
189     _is = null;
190
191     if (is != null)
192       is.close();
193   }
194
195   /**
196    * Converts to a string.
197    * @param env
198    */

199   public String JavaDoc toString()
200   {
201     return "File[" + getPath() + "]";
202   }
203
204 }
205
206
Popular Tags