01: /*
02: * Created on May 27, 2003
03: *
04: */
05: package org.axiondb.util;
06:
07: import java.io.IOException;
08: import java.io.InputStream;
09: import java.io.UnsupportedEncodingException;
10:
11: /**
12: * @author mdelagrange
13: *
14: */
15: public class Utf8InputStreamConverter extends InputStream {
16:
17: // this class will require modification
18: // if we need non-ascii conversions
19:
20: private String _targetEncoding = null;
21: private InputStream _utf8Stream = null;
22:
23: /**
24: * Currently only supports "US-ASCII"
25: *
26: * @param targetEncoding "US-ASCII"
27: * @throws UnsupportedEncodingException
28: */
29: public Utf8InputStreamConverter(InputStream utf8Stream,
30: String targetEncoding) throws UnsupportedEncodingException {
31: if (targetEncoding.equals("US-ASCII") == false) {
32: throw new UnsupportedEncodingException(targetEncoding);
33: }
34:
35: _targetEncoding = targetEncoding;
36: _utf8Stream = utf8Stream;
37: }
38:
39: /**
40: * Returns a byte encoded as ASCII. If non-ASCII characters are encountered in the
41: * underlying UTF-8 stream, an IOException is thrown.
42: *
43: * @see java.io.InputStream#read()
44: */
45: public int read() throws IOException {
46: int theByte = _utf8Stream.read();
47:
48: if (theByte > 127) {
49: throw new IOException(
50: "Could not convert stream from UTF-8 to "
51: + _targetEncoding);
52: }
53:
54: return theByte;
55: }
56:
57: }
|