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 synchronized (BytesToUnicode
.class)
81 for (i
= 0; i
< decoderCache
.length
; ++i
)
83 if (decoderCache
[i
] != null
84 && encoding
.equals(decoderCache
[i
].getName ()))
86 BytesToUnicode rv
= decoderCache
[i
];
87 decoderCache
[i
] = null;
93 // It's not in the cache, so now we have to do real work.
94 String className
= "gnu.gcj.convert.Input_" + canonicalize (encoding
);
98 decodingClass
= Class
.forName(className
);
99 return (BytesToUnicode
) decodingClass
.newInstance();
105 // We pass the original name to iconv and let it handle
107 return new Input_iconv (encoding
);
111 throw new java
.io
.UnsupportedEncodingException(encoding
117 /** Make input bytes available to the conversion.
118 * @param buffer source of input bytes
119 * @param pos index of first available byte
120 * @param length one more than index of last available byte
122 public final void setInput(byte[] buffer
, int pos
, int length
)
129 /** Convert bytes to chars.
130 * Input bytes are taken from this.inbuffer. The available input
131 * bytes start at inbuffer[inpos], and end at inbuffer[inlength-1].
132 * @param outbuffer buffer for the converted character
133 * @param outpos position in buffer to start putting converted characters
134 * @param count the maximum number of characters to convert
135 * @return number of chars placed in outbuffer.
136 * Also, this.inpos is incremented by the number of bytes consumed.
138 * (Note the asymmetry in that the input upper bound is inbuffer[inlength-1],
139 * while the output upper bound is outbuffer[outpos+count-1]. The
140 * justification is that inlength is like the count field of a
141 * BufferedInputStream, while the count parameter is like the
142 * length parameter of a read request.) The count parameter is
143 * also defined to be <= outbuffer.length - outpos (per the specification
144 * of the length parameter for a read request).
146 public abstract int read (char[] outbuffer
, int outpos
, int count
);
148 /** Indicate that the converter is resuable.
149 * This class keeps track of converters on a per-encoding basis.
150 * When done with an encoder you may call this method to indicate
151 * that it can be reused later.
155 synchronized (BytesToUnicode
.class)
157 this.inbuffer
= null;
161 decoderCache
[currCachePos
] = this;
162 currCachePos
= (currCachePos
+ 1) % CACHE_SIZE
;