1 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 41 import java.io.IOException ; 42 import java.io.InputStream ; 43 import java.io.OutputStream ; 44 import java.io.UnsupportedEncodingException ; 45 import java.util.logging.Level ; 46 import java.util.logging.Logger ; 47 48 51 public class ReadStreamInput extends InputStream implements BinaryInput { 52 private static final Logger log 53 = Logger.getLogger(ReadStreamInput.class.getName()); 54 55 private ReadStream _is; 56 57 public ReadStreamInput() 58 { 59 } 60 61 public ReadStreamInput(ReadStream is) 62 { 63 init(is); 64 } 65 66 public void init(ReadStream is) 67 { 68 _is = is; 69 } 70 71 74 public InputStream getInputStream() 75 { 76 return _is; 77 } 78 79 82 public BinaryInput openCopy() 83 throws IOException 84 { 85 return new ReadStreamInput(_is.getPath().openRead()); 86 } 87 88 public void setEncoding(String encoding) 89 throws UnsupportedEncodingException 90 { 91 if (_is != null) 92 _is.setEncoding(encoding); 93 } 94 95 98 public void unread() 99 throws IOException 100 { 101 if (_is != null) 102 _is.unread(); 103 } 104 105 108 public int read() 109 throws IOException 110 { 111 if (_is != null) 112 return _is.read(); 113 else 114 return -1; 115 } 116 117 120 public int read(byte []buffer, int offset, int length) 121 throws IOException 122 { 123 if (_is != null) { 124 return _is.read(buffer, offset, length); 125 } 126 else 127 return -1; 128 } 129 130 133 public BinaryValue read(int length) 134 throws IOException 135 { 136 if (_is == null) 137 return null; 138 139 BinaryBuilderValue bb = new BinaryBuilderValue(); 140 141 while (length > 0) { 142 bb.prepareReadBuffer(); 143 144 int sublen = bb.getLength() - bb.getOffset(); 145 146 if (length < sublen) 147 sublen = length; 148 149 sublen = read(bb.getBuffer(), bb.getOffset(), sublen); 150 151 if (sublen > 0) { 152 bb.setOffset(bb.getOffset() + sublen); 153 length -= sublen; 154 } 155 else 156 return bb; 157 } 158 159 return bb; 160 } 161 162 165 public boolean readOptionalLinefeed() 166 throws IOException 167 { 168 if (_is == null) 169 return false; 170 171 int ch = _is.read(); 172 173 if (ch == '\n') { 174 return true; 175 } 176 else { 177 _is.unread(); 178 return false; 179 } 180 } 181 182 public void writeToStream(OutputStream os, int length) 183 throws IOException 184 { 185 if (_is != null) { 186 _is.writeToStream(os, length); 187 } 188 } 189 190 193 public StringValue readLine(long length) 194 throws IOException 195 { 196 if (_is == null) 197 return null; 198 199 StringBuilderValue sb = new StringBuilderValue(); 200 201 int ch; 202 203 for (; length > 0 && (ch = _is.readChar()) >= 0; length--) { 204 if (ch == '\n') { 205 sb.append((char) ch); 206 return sb; 207 } 208 else if (ch == '\r') { 209 sb.append('\r'); 210 211 int ch2 = _is.read(); 212 213 if (ch == '\n') 214 sb.append('\n'); 215 else 216 _is.unread(); 217 218 return sb; 219 } 220 else 221 sb.append((char) ch); 222 } 223 224 if (sb.length() == 0) 225 return null; 226 else 227 return sb; 228 } 229 230 233 public boolean isEOF() 234 { 235 if (_is == null) 236 return true; 237 else { 238 try { 239 return _is.available() <= 0; 241 } catch (IOException e) { 242 log.log(Level.FINE, e.toString(), e); 243 244 return true; 245 } 246 } 247 } 248 249 252 public long getPosition() 253 { 254 if (_is == null) 255 return -1; 256 else 257 return _is.getPosition(); 258 } 259 260 263 public boolean setPosition(long offset) 264 { 265 if (_is == null) 266 return false; 267 268 try { 269 _is.setPosition(offset); 270 271 return true; 272 } catch (IOException e) { 273 throw new QuercusModuleException(e); 274 } 275 } 276 277 public long seek(long offset, int whence) 278 { 279 switch (whence) { 280 case BinaryInput.SEEK_CUR: 281 offset = getPosition() + offset; 282 break; 283 case BinaryInput.SEEK_END: 284 offset = getPosition(); 286 break; 287 case SEEK_SET: 288 break; 289 default: 290 break; 291 } 292 293 setPosition(offset); 294 295 return offset; 296 } 297 298 public Value stat() 299 { 300 return BooleanValue.FALSE; 301 } 302 303 306 public void closeRead() 307 { 308 close(); 309 } 310 311 314 public void close() 315 { 316 ReadStream is = _is; 317 _is = null; 318 319 if (is != null) 320 is.close(); 321 } 322 323 public Object toJavaObject() 324 { 325 return this; 326 } 327 328 public String getResourceType() 329 { 330 return "stream"; 331 } 332 333 336 public String toString() 337 { 338 return "ReadStreamInput[" + _is.getPath() + "]"; 339 } 340 } 341 342 | Popular Tags |