1 /* Copyright (C) 1999 Cygnus Solutions
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
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 static Class defaultDecodingClass;
23 static synchronized void getDefaultDecodingClass()
25 // Test (defaultDecodingClass == null) again in case of race condition.
26 if (defaultDecodingClass == null)
28 String encoding = System.getProperty("file.encoding");
29 String className = "gnu.gcj.convert.Input_"+encoding;
32 defaultDecodingClass = Class.forName(className);
34 catch (ClassNotFoundException ex)
36 throw new NoClassDefFoundError("missing default encoding "
37 + encoding + " (class "
38 + className + " not found)");
43 public abstract String getName();
45 public static BytesToUnicode getDefaultDecoder()
49 if (defaultDecodingClass == null)
50 getDefaultDecodingClass();
51 return (BytesToUnicode) defaultDecodingClass.newInstance();
55 return new Input_8859_1();
59 /** Get a byte-stream->char-stream converter given an encoding name. */
60 public static BytesToUnicode getDecoder (String encoding)
61 throws java.io.UnsupportedEncodingException
63 String className = "gnu.gcj.convert.Input_"+encoding;
67 decodingClass = Class.forName(className);
68 return (BytesToUnicode) decodingClass.newInstance();
72 throw new java.io.UnsupportedEncodingException(encoding
77 /** Make input bytes available to the conversion.
78 * @param buffer source of input bytes
79 * @param pos index of first available byte
80 * @param length one more than index of last available byte
82 public final void setInput(byte[] buffer, int pos, int length)
89 /** Convert bytes to chars.
90 * Input bytes are taken from this.inbuffer. The available input
91 * bytes start at inbuffer[inpos], and end at inbuffer[inlength-1].
92 * @param outbuffer buffer for the converted character
93 * @param outpos position in buffer to start putting converted characters
94 * @param count the maximum number of characters to convert
95 * @return number of chars placed in outbuffer.
96 * Also, this.inpos is incremented by the number of bytes consumed.
98 * (Note the asymmetry in that the input upper bound is inbuffer[inlength-1],
99 * while the output upper bound is outbuffer[outpos+count-1]. The
100 * justification is that inlength is like the count field of a
101 * BufferedInputStream, while the count parameter is like the
102 * length parameter of a read request.) The count parameter is
103 * also defined to be <= outbuffer.length - outpos (per the specification
104 * of the length parameter for a read request).
106 public abstract int read (char[] outbuffer, int outpos, int count);