1:
37:
38: package ;
39:
40: import ;
41: import ;
42: import ;
43:
44:
50: public class ReaderInputStream
51: extends InputStream
52: {
53:
54: private Reader reader;
55: private String encoding;
56:
57:
58: private byte extra[];
59: private int pos;
60:
61: private byte extra_marked[];
62: private int pos_marked;
63:
64: public ReaderInputStream(Reader reader)
65: {
66: this.reader = reader;
67: this.encoding = "UTF-8";
68: }
69:
70: void setEncoding(String encoding)
71: {
72: this.encoding = encoding;
73: }
74:
75: public int read()
76: throws IOException
77: {
78: if (extra != null)
79: {
80: int result = extra[pos];
81: pos++;
82: if (pos >= extra.length)
83: {
84: extra = null;
85: }
86: return result;
87: }
88: return reader.read();
89: }
90:
91: public int read(byte[] b)
92: throws IOException
93: {
94: return read(b, 0, b.length);
95: }
96:
97: public int read(byte[] b, int off, int len)
98: throws IOException
99: {
100: if (len == 0)
101: {
102: return 0;
103: }
104:
105: if (extra != null)
106: {
107: int available = extra.length - pos;
108: int l = available < len ? available : len;
109: System.arraycopy(extra, 0, b, off, l);
110: pos += l;
111: if (pos >= extra.length)
112: {
113: extra = null;
114: }
115: return l;
116: }
117:
118: char[] c = new char[len];
119: int l = reader.read(c, 0, len);
120: if (l == -1)
121: {
122: return -1;
123: }
124:
125: String s = new String(c, 0, l);
126: byte[] d = s.getBytes(encoding);
127:
128: int available = d.length;
129: int more = d.length - len;
130: if (more > 0)
131: {
132: extra = new byte[more];
133: pos = 0;
134: System.arraycopy(d, len, extra, 0, more);
135: available -= more;
136: }
137:
138: System.arraycopy(d, 0, b, off, available);
139: return available;
140: }
141:
142: public void close()
143: throws IOException
144: {
145: reader.close();
146: }
147:
148: public boolean markSupported()
149: {
150: return reader.markSupported();
151: }
152:
153: public void mark(int limit)
154: {
155: if (extra != null)
156: {
157: extra_marked = new byte[extra.length];
158: System.arraycopy(extra, 0, extra_marked, 0, extra.length);
159: pos_marked = pos;
160: }
161: else
162: {
163: extra_marked = null;
164: }
165:
166: try
167: {
168:
169:
170:
171: reader.mark(limit);
172: }
173: catch (IOException ioe)
174: {
175: throw new RuntimeException(ioe);
176: }
177: }
178:
179: public void reset()
180: throws IOException
181: {
182: extra = extra_marked;
183: pos = pos_marked;
184: extra_marked = null;
185:
186: reader.reset();
187: }
188:
189: public long skip(long n)
190: throws IOException
191: {
192: long done = 0;
193: if (extra != null)
194: {
195: int available = extra.length - pos;
196: done = available < n ? available : n;
197: pos += done;
198: if (pos >= extra.length)
199: {
200: extra = null;
201: }
202: }
203:
204: n -= done;
205: if (n > 0)
206: {
207: return reader.skip(n) + done;
208: }
209: else
210: {
211: return done;
212: }
213: }
214:
215:
220: public int available()
221: throws IOException
222: {
223: if (extra != null)
224: {
225: return pos - extra.length;
226: }
227:
228: return reader.ready() ? 1 : 0;
229: }
230:
231: public String toString()
232: {
233: return getClass().getName() + "[" + reader + ", " + encoding + "]";
234: }
235:
236: }