1 /* Copyright (C) 1999, 2000, 2001 Free Software Foundation
3 This file is part of libgcj.
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9 package gnu
.gcj
.convert
;
11 public abstract class BytesToUnicode
extends IOConverter
13 /** Buffer to read bytes from.
14 * The characters inbuffer[inpos] ... inbuffer[inlength-1] are available. */
15 public byte[] inbuffer
;
16 /** Starting index in buffer to read bytes from. */
18 /** End of valid bytes in buffer. */
21 // The name of the default encoding.
22 static String defaultEncoding
;
24 /* These keep a small cache of decoders for reuse. The array holds
25 the actual decoders. The currCachePos is the next value we are
26 going to replace in the cache. We don't just throw the data away
27 if the cache is full, because if the cache filled up with stuff
28 we don't need then the cache would be worthless. We instead
29 circulate through the cache the implement kind of an LRU
31 private static final int CACHE_SIZE
= 4; // A power of 2 for speed
32 private static BytesToUnicode
[] decoderCache
33 = new BytesToUnicode
[CACHE_SIZE
];
34 private static int currCachePos
= 0;
36 public abstract String
getName();
38 public static BytesToUnicode
getDefaultDecoder()
42 synchronized (BytesToUnicode
.class)
44 if (defaultEncoding
== null)
47 = canonicalize (System
.getProperty("file.encoding",
49 String className
= "gnu.gcj.convert.Input_" + encoding
;
52 Class defaultDecodingClass
= Class
.forName(className
);
53 defaultEncoding
= encoding
;
55 catch (ClassNotFoundException ex
)
57 throw new NoClassDefFoundError("missing default encoding "
58 + encoding
+ " (class "
64 return getDecoder (defaultEncoding
);
68 return new Input_8859_1();
72 /** Get a byte-stream->char-stream converter given an encoding name. */
73 public static BytesToUnicode
getDecoder (String encoding
)
74 throws java
.io
.UnsupportedEncodingException
76 /* First hunt in our cache to see if we have a decoder that is
78 String canonicalEncoding
= canonicalize(encoding
);
79 synchronized (BytesToUnicode
.class)
82 for (i
= 0; i
< decoderCache
.length
; ++i
)
84 if (decoderCache
[i
] != null
85 && canonicalEncoding
.equals(decoderCache
[i
].getName ()))
87 BytesToUnicode rv
= decoderCache
[i
];
88 decoderCache
[i
] = null;
94 // It's not in the cache, so now we have to do real work.
95 String className
= "gnu.gcj.convert.Input_" + canonicalEncoding
;
99 decodingClass
= Class
.forName(className
);
100 return (BytesToUnicode
) decodingClass
.newInstance();
106 // We pass the original name to iconv and let it handle
108 return new Input_iconv (encoding
);
112 throw new java
.io
.UnsupportedEncodingException(encoding
118 /** Make input bytes available to the conversion.
119 * @param buffer source of input bytes
120 * @param pos index of first available byte
121 * @param length one more than index of last available byte
123 public final void setInput(byte[] buffer
, int pos
, int length
)
130 /** Convert bytes to chars.
131 * Input bytes are taken from this.inbuffer. The available input
132 * bytes start at inbuffer[inpos], and end at inbuffer[inlength-1].
133 * @param outbuffer buffer for the converted character
134 * @param outpos position in buffer to start putting converted characters
135 * @param count the maximum number of characters to convert
136 * @return number of chars placed in outbuffer.
137 * Also, this.inpos is incremented by the number of bytes consumed.
139 * (Note the asymmetry in that the input upper bound is inbuffer[inlength-1],
140 * while the output upper bound is outbuffer[outpos+count-1]. The
141 * justification is that inlength is like the count field of a
142 * BufferedInputStream, while the count parameter is like the
143 * length parameter of a read request.) The count parameter is
144 * also defined to be <= outbuffer.length - outpos (per the specification
145 * of the length parameter for a read request).
147 public abstract int read (char[] outbuffer
, int outpos
, int count
);
149 /** Indicate that the converter is resuable.
150 * This class keeps track of converters on a per-encoding basis.
151 * When done with an encoder you may call this method to indicate
152 * that it can be reused later.
156 synchronized (BytesToUnicode
.class)
158 this.inbuffer
= null;
162 decoderCache
[currCachePos
] = this;
163 currCachePos
= (currCachePos
+ 1) % CACHE_SIZE
;