1 /* Copyright (C) 1999, 2000 Red Hat, Inc.
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 UnicodeToBytes
13 /** Buffer to emit bytes to.
14 * The locations buf[count] ... buf[buf.length-1] are available. */
18 static Class defaultEncodingClass
;
20 static synchronized void getDefaultEncodingClass()
22 // Test (defaultEncodingClass == null) again in case of race condition.
23 if (defaultEncodingClass
== null)
25 String encoding
= System
.getProperty("file.encoding");
26 String className
= "gnu.gcj.convert.Output_"+encoding
;
29 defaultEncodingClass
= Class
.forName(className
);
31 catch (ClassNotFoundException ex
)
33 throw new NoClassDefFoundError("missing default encoding "
34 + encoding
+ " (class "
35 + className
+ " not found)");
41 public abstract String
getName();
43 public static UnicodeToBytes
getDefaultEncoder()
47 if (defaultEncodingClass
== null)
48 getDefaultEncodingClass();
49 return (UnicodeToBytes
) defaultEncodingClass
.newInstance();
53 return new Output_8859_1();
57 /** Get a char-stream->byte-stream converter given an encoding name. */
58 public static UnicodeToBytes
getEncoder (String encoding
)
59 throws java
.io
.UnsupportedEncodingException
61 String className
= "gnu.gcj.convert.Output_"+encoding
;
65 encodingClass
= Class
.forName(className
);
66 return (UnicodeToBytes
) encodingClass
.newInstance();
72 return new Output_iconv (encoding
);
76 // Put the original exception in the throwable.
77 throw new java
.io
.UnsupportedEncodingException(encoding
+ " ("
83 public final void setOutput(byte[] buffer
, int count
)
89 /** Convert chars to bytes.
90 * Converted bytes are written to buf, starting at count.
91 * @param inbuffer source of characters to convert
92 * @param inpos index of initial character in inbuffer to convert
93 * @param inlength number of characters to convert
94 * @return number of chars converted
95 * Also, this.count is increment by the number of bytes converted.
97 public abstract int write (char[] inbuffer
, int inpos
, int inlength
);
99 /** Convert chars to bytes.
100 * Converted bytes are written to buf, starting at count.
101 * @param str source of characters to convert
102 * @param inpos index of initial character in str to convert
103 * @param inlength number of characters to convert
104 * @param work if non-null, a buffer than can be used
105 * @return number of chars converted
106 * Also, this.count is increment by the number of bytes converted.
108 public int write (String str
, int inpos
, int inlength
, char[] work
)
111 work
= new char[inlength
];
112 int srcEnd
= inpos
+ (inlength
> work
.length ? work
.length
: inlength
);
113 str
.getChars(inpos
, srcEnd
, work
, 0);
114 return write(work
, inpos
, inlength
);