[kaffe] CVS kaffe (kaz): kaffe/libraries/javalib/kaffe/io/KaffeCharset.java,
Kaffe CVS
cvs-commits at kaffe.org
Mon Jan 10 07:17:55 PST 2005
PatchSet 5833
Date: 2005/01/10 15:13:30
Author: kaz
Branch: HEAD
Tag: (none)
Log:
2005-01-10 Ito Kazumitsu < kaz at maczuka.gcd.org>
* kaffe/libraries/javalib/kaffe/io/KaffeCharset.java,
kaffe/libraries/javalib/kaffe/io/KaffeCharsetProvider.java:
New files.
Members:
ChangeLog:1.3377->1.3378
libraries/javalib/kaffe/io/KaffeCharset.java:INITIAL->1.1
libraries/javalib/kaffe/io/KaffeCharsetProvider.java:INITIAL->1.1
Index: kaffe/ChangeLog
diff -u kaffe/ChangeLog:1.3377 kaffe/ChangeLog:1.3378
--- kaffe/ChangeLog:1.3377 Mon Jan 10 12:43:14 2005
+++ kaffe/ChangeLog Mon Jan 10 15:13:30 2005
@@ -1,3 +1,9 @@
+2005-01-10 Ito Kazumitsu < kaz at maczuka.gcd.org>
+
+ * kaffe/libraries/javalib/kaffe/io/KaffeCharset.java,
+ kaffe/libraries/javalib/kaffe/io/KaffeCharsetProvider.java:
+ New files.
+
2005-01-10 Guilhem Lavaux <guilhem at kaffe.org>
* kaffe/kaffevm/external.c
===================================================================
Checking out kaffe/libraries/javalib/kaffe/io/KaffeCharset.java
RCS: /home/cvs/kaffe/kaffe/libraries/javalib/kaffe/io/KaffeCharset.java,v
VERS: 1.1
***************
--- /dev/null Sun Aug 4 19:57:58 2002
+++ kaffe/libraries/javalib/kaffe/io/KaffeCharset.java Mon Jan 10 15:17:55 2005
@@ -0,0 +1,226 @@
+/*
+ * Java core library component.
+ *
+ * Copyright (c) 2005
+ * Ito Kazumitsu <kaz at maczuka.gcd.org>
+ *
+ * See the file "license.terms" for information on usage and redistribution
+ * of this file.
+ */
+
+package kaffe.io;
+import java.nio.charset.*;
+import java.nio.ByteBuffer;
+import java.nio.CharBuffer;
+
+public class KaffeCharset extends Charset
+{
+
+ float averageCharsPerByte;
+ float maxCharsPerByte;
+ float averageBytesPerChar;
+ float maxBytesPerChar;
+ String kaffeIOName;
+
+ KaffeCharset(String canonicalName, String kaffeIOName,
+ String[] aliases, float[] factors)
+ {
+ super (canonicalName, aliases);
+ this.kaffeIOName = kaffeIOName;
+ this.averageCharsPerByte = factors[0];
+ this.maxCharsPerByte = factors[1];
+ this.averageBytesPerChar = factors[2];
+ this.maxBytesPerChar = factors[3];
+ }
+
+ public CharsetDecoder newDecoder ()
+ {
+ return new Decoder (this, kaffeIOName, averageCharsPerByte, maxCharsPerByte);
+ }
+
+ public CharsetEncoder newEncoder ()
+ {
+ return new Encoder (this, kaffeIOName, averageBytesPerChar, maxBytesPerChar);
+ }
+
+ // I know nothing about any charset other than myself.
+ public boolean contains (Charset cs)
+ {
+ return this.getClass().isInstance(cs);
+ }
+
+ private static final class Decoder extends CharsetDecoder
+ {
+ // Package-private to avoid a trampoline constructor.
+ Decoder (Charset cs, String kaffeIOName,
+ float averageCharsPerByte, float maxCharsPerByte)
+ {
+ super (cs, averageCharsPerByte, maxCharsPerByte);
+ try
+ {
+ b2c = kaffe.io.ByteToCharConverter.getConverter(kaffeIOName);
+ }
+ catch (java.io.UnsupportedEncodingException e)
+ {
+ throw new UnsupportedOperationException(e.toString());
+ }
+ }
+
+ private kaffe.io.ByteToCharConverter b2c;
+
+ protected CoderResult decodeLoop (ByteBuffer in, CharBuffer out)
+ {
+ byte[] inbuf = null;
+ int inPos = 0;
+ int inLen = 0;
+ char[] outbuf = null;
+ int outPos = 0;
+ int outLen = 0;
+
+ if (in.hasArray())
+ {
+ inbuf = in.array();
+ inPos = in.position();
+ inLen = in.remaining();
+ in.position(in.limit());
+ }
+ else {
+ inbuf = new byte[in.remaining()];
+ inPos = 0;
+ inLen = inbuf.length;
+ in.get(inbuf, inPos, inLen);
+ }
+
+ if (out.hasArray())
+ {
+ outbuf = out.array();
+ outPos = out.position();
+ outLen = out.remaining();
+ }
+ else {
+ outbuf = new char[out.remaining()];
+ outPos = 0;
+ outLen = outbuf.length;
+ }
+
+ int l = b2c.convert(inbuf, inPos, inLen, outbuf, outPos, outLen);
+
+ if (out.hasArray())
+ {
+ out.position(outPos + l);
+ }
+ else
+ {
+ out.put(outbuf, outPos, l);
+ }
+
+ int n = 0;
+ if (b2c.havePending())
+ {
+ n = b2c.pendingLength();
+ b2c.reset();
+ }
+ in.position(in.position() - n);
+ if (n > 0 && out.remaining() == 0)
+ {
+ return CoderResult.OVERFLOW;
+ }
+ else
+ {
+ return CoderResult.UNDERFLOW;
+ }
+ }
+
+ protected void implReset() {
+ b2c.reset();
+ }
+ }
+
+ private static final class Encoder extends CharsetEncoder
+ {
+ // Package-private to avoid a trampoline constructor.
+ Encoder (Charset cs, String kaffeIOName,
+ float averageBytesPerChar, float maxBytesPerChar)
+ {
+ super (cs, averageBytesPerChar, maxBytesPerChar);
+ try
+ {
+ c2b = kaffe.io.CharToByteConverter.getConverter(kaffeIOName);
+ }
+ catch (java.io.UnsupportedEncodingException e)
+ {
+ throw new UnsupportedOperationException(e.toString());
+ }
+ }
+
+ private kaffe.io.CharToByteConverter c2b;
+
+ protected CoderResult encodeLoop (CharBuffer in, ByteBuffer out)
+ {
+ char[] inbuf = null;
+ int inPos = 0;
+ int inLen = 0;
+ byte[] outbuf = null;
+ int outPos = 0;
+ int outLen = 0;
+
+ if (in.hasArray())
+ {
+ inbuf = in.array();
+ inPos = in.position();
+ inLen = in.remaining();
+ in.position(in.limit());
+ }
+ else {
+ inbuf = new char[in.remaining()];
+ inPos = 0;
+ inLen = inbuf.length;
+ in.get(inbuf, inPos, inLen);
+ }
+
+ if (out.hasArray())
+ {
+ outbuf = out.array();
+ outPos = out.position();
+ outLen = out.remaining();
+ }
+else {
+ outbuf = new byte[out.remaining()];
+ outPos = 0;
+ outLen = outbuf.length;
+ }
+
+ int l = c2b.convert(inbuf, inPos, inLen, outbuf, outPos, outLen);
+
+ if (out.hasArray())
+ {
+ out.position(outPos + l);
+ }
+ else
+ {
+ out.put(outbuf, outPos, l);
+ }
+
+ int n = 0;
+ if (c2b.havePending())
+ {
+ n = c2b.pendingLength();
+ c2b.reset();
+ }
+ in.position(in.position() - n);
+ if (n > 0 && out.remaining() == 0)
+ {
+ return CoderResult.OVERFLOW;
+ }
+ else
+ {
+ return CoderResult.UNDERFLOW;
+ }
+ }
+
+ protected void implReset() {
+ c2b.reset();
+ }
+ }
+
+}
===================================================================
Checking out kaffe/libraries/javalib/kaffe/io/KaffeCharsetProvider.java
RCS: /home/cvs/kaffe/kaffe/libraries/javalib/kaffe/io/KaffeCharsetProvider.java,v
VERS: 1.1
***************
--- /dev/null Sun Aug 4 19:57:58 2002
+++ kaffe/libraries/javalib/kaffe/io/KaffeCharsetProvider.java Mon Jan 10 15:17:55 2005
@@ -0,0 +1,126 @@
+/*
+ * Java core library component.
+ *
+ * Copyright (c) 2005
+ * Ito Kazumitsu <kaz at maczuka.gcd.org>
+ *
+ * See the file "license.terms" for information on usage and redistribution
+ * of this file.
+ */
+
+package kaffe.io;
+
+import java.nio.charset.spi.CharsetProvider;
+import java.nio.charset.Charset;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Iterator;
+
+/**
+ * Charset provider wrapping kaffe.io.{ByteToChar|CharToByte}Converter.
+ * In order that this class is used, a file named
+ * java.nio.charset.spi.CharsetProvider
+ * must exist in the resource directory META-INF/services, and
+ * it must contain a line that reads "kaffe.io.KaffeCharsetProvider".
+ *
+ * @author Ito Kazumitsu
+ * @see Charset
+ */
+public final class KaffeCharsetProvider extends CharsetProvider
+{
+
+ /**
+ * Map from canonical name (in upper case) to objects whose contents are:
+ * [0]: cached instance of the charset,
+ * [1]: name used to get kaffe.io.{ByteToChar|CharToByte}Converter
+ * [2]: an array of aliases and
+ * [3]: an array of float values used for creating a new Charset instance,
+ * averageCharsPerByte, maxCharsPerByte, averageBytesPerChar and
+ * maxBytesPerChar.
+ */
+ private HashMap charsets;
+ private HashMap canonicalNames;
+
+ private static final int CSOBJ_CACHE = 0;
+ private static final int CSOBJ_KAFFEIONAME = 1;
+ private static final int CSOBJ_ALIASES = 2;
+ private static final int CSOBJ_FACTORS = 3;
+
+ public KaffeCharsetProvider ()
+ {
+ charsets = new HashMap();
+
+ // Name supported charsets here.
+
+ charsets.put("EUC-JP",
+ new Object[] {null, "EUC-JP",
+ new String[] {"EUC_JP", "EUCJP"}, // Sun's JDK supports these names.
+ new float[] {0.5f, 1.0f, 2.0f, 2.0f}});
+
+ charsets.put("ISO-2022-JP",
+ new Object[] {null, "ISO-2022-JP",
+ null,
+ new float[] {0.5f, 1.0f, 2.0f, 8.0f}});
+
+ charsets.put("WINDOWS-31J",
+ new Object[] {null, "MS932",
+ new String[] {"MS932"},
+ new float[] {0.5f, 1.0f, 2.0f, 2.0f}});
+
+ charsets.put("SHIFT_JIS",
+ new Object[] {null, "SHIFT_JIS",
+ new String[] {"SJIS", "SHIFT-JIS"}, // Sun's JDK supports these names.
+ new float[] {0.5f, 1.0f, 2.0f, 2.0f}});
+
+ canonicalNames = new HashMap();
+ for (Iterator i = charsets.keySet().iterator (); i.hasNext (); )
+ {
+ String name = (String)(i.next ());
+ Object[] objs = (Object[])(charsets.get(name));
+ if (objs[CSOBJ_ALIASES] != null)
+ {
+ String[] aliases = (String[])(objs[CSOBJ_ALIASES]);
+ for (int j = 0; j < aliases.length; j++)
+ {
+ canonicalNames.put (aliases[j].toUpperCase(), name);
+ }
+ }
+ }
+ }
+
+ public Iterator charsets ()
+ {
+ HashMap map = new HashMap();
+ for (Iterator i = charsets.keySet().iterator (); i.hasNext (); )
+ {
+ String name = (String)(i.next ());
+ map.put(name, charsetForName(name));
+ }
+ return Collections.unmodifiableCollection (map.values ())
+ .iterator ();
+ }
+
+ public Charset charsetForName (String charsetName)
+ {
+ charsetName = canonicalize (charsetName);
+ Object[] objs = (Object[])(charsets.get(charsetName));
+ if (objs == null) return null;
+ if (objs[CSOBJ_CACHE] == null)
+ {
+ String kaffeIOName = (String)(objs[CSOBJ_KAFFEIONAME]);
+ String[] aliases = (String[])(objs[CSOBJ_ALIASES]);
+ float[] factors = (float[])(objs[CSOBJ_FACTORS]);
+ objs[CSOBJ_CACHE] = new KaffeCharset(charsetName, kaffeIOName,
+ aliases, factors);
+ }
+ return (Charset)(objs[CSOBJ_CACHE]);
+ }
+
+ private String canonicalize (String charsetName)
+ {
+ charsetName = charsetName.toUpperCase();
+ Object o = canonicalNames.get (charsetName);
+ return o == null ? charsetName : (String)o;
+ }
+
+}
More information about the kaffe
mailing list