01: /*=============================================================================
02: * Copyright Texas Instruments 2000-2003. All Rights Reserved.
03: *
04: * This program is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License as published by the Free Software Foundation; either
07: * version 2 of the License, or (at your option) any later version.
08: *
09: * This program is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12: * Lesser General Public License for more details.
13: *
14: * You should have received a copy of the GNU Lesser General Public
15: * License along with this library; if not, write to the Free Software
16: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17: */
18:
19: package oscript.util;
20:
21: import java.io.*;
22:
23: /**
24: * Used to convert Reader -> InputStream... right now AbstractFile is
25: * strictly binary stream based, not character stream. Maybe in the
26: * future AbstractFile will optionally support both, so this can go
27: * away... and we won't be tied to sun's JDK for no good reason.
28: *
29: * @author Rob Clark (rob@ti.com)
30: */
31: public class ReaderInputStream extends InputStream {
32: private Reader r;
33: private sun.io.CharToByteConverter ctb;
34:
35: ReaderInputStream(Reader r) {
36: this .r = r;
37: this .ctb = sun.io.CharToByteConverter.getDefault();
38: }
39:
40: public int read() throws IOException {
41: byte[] b = new byte[1];
42: while (read(b, 0, 1) != 1)
43: ;
44: return b[0];
45: }
46:
47: public int read(byte[] b, int off, int len) throws IOException {
48: char[] c = new char[len];
49: int in = r.read(c, 0, len);
50:
51: ctb.convert(c, 0, in, b, off, off + in);
52:
53: return in;
54: }
55:
56: public void close() throws IOException {
57: r.close();
58: }
59: }
60:
61: /*
62: * Local Variables:
63: * tab-width: 2
64: * indent-tabs-mode: nil
65: * mode: java
66: * c-indentation-style: java
67: * c-basic-offset: 2
68: * eval: (c-set-offset 'substatement-open '0)
69: * eval: (c-set-offset 'case-label '+)
70: * eval: (c-set-offset 'inclass '+)
71: * eval: (c-set-offset 'inline-open '0)
72: * End:
73: */
|