01: /*
02: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
03: *
04: * This file is part of Resin(R) Open Source
05: *
06: * Each copy or derived work must preserve the copyright notice and this
07: * notice unmodified.
08: *
09: * 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 Nam Nguyen
27: */
28:
29: package com.caucho.vfs;
30:
31: import java.io.IOException;
32: import java.io.Reader;
33:
34: public class ReaderStream extends StreamImpl {
35: private Reader _reader;
36:
37: ReaderStream(Reader reader) {
38: _reader = reader;
39: }
40:
41: public static ReadStream open(Reader reader) {
42: ReaderStream ss = new ReaderStream(reader);
43: return new ReadStream(ss);
44: }
45:
46: public Path getPath() {
47: throw new UnsupportedOperationException();
48: }
49:
50: public boolean canRead() {
51: return true;
52: }
53:
54: // XXX: encoding issues
55: public int read(byte[] buf, int offset, int length)
56: throws IOException {
57: int i = 0;
58:
59: for (; i < length; i++) {
60: int ch = _reader.read();
61:
62: if (ch < 0)
63: break;
64:
65: if (ch < 0x80)
66: buf[offset++] = (byte) ch;
67: else if (ch < 0x800) {
68: buf[offset++] = (byte) (0xc0 | (ch >> 6));
69: buf[offset++] = (byte) (0x80 | (ch & 0x3f));
70: } else if (ch < 0x8000) {
71: buf[offset++] = (byte) (0xe0 | (ch >> 12));
72: buf[offset++] = (byte) (0x80 | ((ch >> 6) & 0x3f));
73: buf[offset++] = (byte) (0x80 | ((ch >> 6) & 0x3f));
74: }
75: }
76:
77: return i;
78: }
79:
80: public int getAvailable() throws IOException {
81: throw new UnsupportedOperationException();
82: }
83: }
|