22
22
import java.io.InputStream;
23
23
import java.io.InputStreamReader;
24
24
import java.io.UnsupportedEncodingException;
25
import java.nio.charset.Charset;
26
import java.util.HashMap;
27
import java.util.Locale;
30
import org.apache.tomcat.util.res.StringManager;
26
32
/** Efficient conversion of bytes to character .
40
46
private static final org.apache.juli.logging.Log log=
41
47
org.apache.juli.logging.LogFactory.getLog( B2CConverter.class );
49
private static final StringManager sm =
50
StringManager.getManager(Constants.Package);
52
private static final Map<String, Charset> encodingToCharsetCache =
53
new HashMap<String, Charset>();
56
for (Charset charset: Charset.availableCharsets().values()) {
57
encodingToCharsetCache.put(
58
charset.name().toLowerCase(Locale.US), charset);
59
for (String alias : charset.aliases()) {
60
encodingToCharsetCache.put(
61
alias.toLowerCase(Locale.US), charset);
66
public static Charset getCharset(String enc)
67
throws UnsupportedEncodingException{
69
// Encoding names should all be ASCII
70
String lowerCaseEnc = enc.toLowerCase(Locale.US);
72
Charset charset = encodingToCharsetCache.get(lowerCaseEnc);
74
if (charset == null) {
75
// Pre-population of the cache means this must be invalid
76
throw new UnsupportedEncodingException(
77
sm.getString("b2cConvertor.unknownEncoding", enc));
43
82
private IntermediateInputStream iis;
44
83
private ReadConvertor conv;
45
84
private String encoding;
105
144
// destroy the reader/iis
106
145
iis=new IntermediateInputStream();
107
conv=new ReadConvertor( iis, encoding );
146
conv = new ReadConvertor(iis, getCharset(encoding));
121
160
/** Create a converter.
123
public ReadConvertor( IntermediateInputStream in, String enc )
124
throws UnsupportedEncodingException
162
public ReadConvertor(IntermediateInputStream in, Charset charset) {
129
166
/** Overridden - will do nothing but reset internal state.