summaryrefslogtreecommitdiff
path: root/libjava/gnu
diff options
context:
space:
mode:
Diffstat (limited to 'libjava/gnu')
-rw-r--r--libjava/gnu/gcj/convert/BytesToUnicode.java13
-rw-r--r--libjava/gnu/gcj/convert/Input_iconv.java42
-rw-r--r--libjava/gnu/gcj/convert/Output_iconv.java42
-rw-r--r--libjava/gnu/gcj/convert/UnicodeToBytes.java15
-rw-r--r--libjava/gnu/gcj/convert/natIconv.cc142
5 files changed, 247 insertions, 7 deletions
diff --git a/libjava/gnu/gcj/convert/BytesToUnicode.java b/libjava/gnu/gcj/convert/BytesToUnicode.java
index e5302b73f21..1d96409f372 100644
--- a/libjava/gnu/gcj/convert/BytesToUnicode.java
+++ b/libjava/gnu/gcj/convert/BytesToUnicode.java
@@ -1,4 +1,4 @@
-/* Copyright (C) 1999 Red Hat, Inc.
+/* Copyright (C) 1999, 2000 Red Hat, Inc.
This file is part of libgcj.
@@ -69,8 +69,15 @@ public abstract class BytesToUnicode
}
catch (Throwable ex)
{
- throw new java.io.UnsupportedEncodingException(encoding
- + " (" + ex + ')');
+ try
+ {
+ return new Input_iconv (encoding);
+ }
+ catch (Throwable _)
+ {
+ throw new java.io.UnsupportedEncodingException(encoding
+ + " (" + ex + ')');
+ }
}
}
diff --git a/libjava/gnu/gcj/convert/Input_iconv.java b/libjava/gnu/gcj/convert/Input_iconv.java
new file mode 100644
index 00000000000..7b5fb03a54b
--- /dev/null
+++ b/libjava/gnu/gcj/convert/Input_iconv.java
@@ -0,0 +1,42 @@
+// Input_iconv.java -- Java side of iconv() reader.
+
+/* Copyright (C) 2000 Red Hat, Inc.
+
+ This file is part of libgcj.
+
+This software is copyrighted work licensed under the terms of the
+Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
+details. */
+
+package gnu.gcj.convert;
+import gnu.gcj.RawData;
+import java.io.UnsupportedEncodingException;
+
+/**
+ * Convert bytes in some iconv-supported encoding to Unicode.
+ * @author Tom Tromey <tromey@redhat.com>
+ * @date January 30, 2000
+ */
+
+public class Input_iconv extends BytesToUnicode
+{
+ public Input_iconv (String encoding) throws UnsupportedEncodingException
+ {
+ this.encoding = encoding;
+ this.handle = null;
+ init (encoding);
+ }
+
+ public String getName() { return encoding; }
+
+ public native void finalize ();
+ private native void init (String encoding)
+ throws UnsupportedEncodingException;
+ public native int read (char[] outbuffer, int outpos, int count);
+
+ // The encoding we're using.
+ private String encoding;
+
+ // The iconv handle.
+ private RawData handle;
+}
diff --git a/libjava/gnu/gcj/convert/Output_iconv.java b/libjava/gnu/gcj/convert/Output_iconv.java
new file mode 100644
index 00000000000..386028aee11
--- /dev/null
+++ b/libjava/gnu/gcj/convert/Output_iconv.java
@@ -0,0 +1,42 @@
+// Output_iconv.java -- Java side of iconv() writer.
+
+/* Copyright (C) 2000 Red Hat, Inc.
+
+ This file is part of libgcj.
+
+This software is copyrighted work licensed under the terms of the
+Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
+details. */
+
+package gnu.gcj.convert;
+import gnu.gcj.RawData;
+import java.io.UnsupportedEncodingException;
+
+/**
+ * Convert Unicode to bytes in some iconv-supported encoding.
+ * @author Tom Tromey <tromey@redhat.com>
+ * @date January 30, 2000
+ */
+
+public class Output_iconv extends UnicodeToBytes
+{
+ public Output_iconv (String encoding) throws UnsupportedEncodingException
+ {
+ this.encoding = encoding;
+ this.handle = null;
+ init (encoding);
+ }
+
+ public String getName() { return encoding; }
+
+ public native void finalize ();
+ private native void init (String encoding)
+ throws UnsupportedEncodingException;
+ public native int write (char[] inbuffer, int inpos, int count);
+
+ // The encoding we're using.
+ private String encoding;
+
+ // The iconv handle.
+ private RawData handle;
+}
diff --git a/libjava/gnu/gcj/convert/UnicodeToBytes.java b/libjava/gnu/gcj/convert/UnicodeToBytes.java
index 5a33003d6bc..b91a8cc8489 100644
--- a/libjava/gnu/gcj/convert/UnicodeToBytes.java
+++ b/libjava/gnu/gcj/convert/UnicodeToBytes.java
@@ -1,4 +1,4 @@
-/* Copyright (C) 1999 Red Hat, Inc.
+/* Copyright (C) 1999, 2000 Red Hat, Inc.
This file is part of libgcj.
@@ -67,8 +67,16 @@ public abstract class UnicodeToBytes
}
catch (Throwable ex)
{
- throw new java.io.UnsupportedEncodingException(encoding + " ("
- + ex + ')');
+ try
+ {
+ return new Output_iconv (encoding);
+ }
+ catch (Throwable _)
+ {
+ // Put the original exception in the throwable.
+ throw new java.io.UnsupportedEncodingException(encoding + " ("
+ + ex + ')');
+ }
}
}
@@ -105,5 +113,4 @@ public abstract class UnicodeToBytes
str.getChars(inpos, srcEnd, work, 0);
return write(work, inpos, inlength);
}
-
}
diff --git a/libjava/gnu/gcj/convert/natIconv.cc b/libjava/gnu/gcj/convert/natIconv.cc
new file mode 100644
index 00000000000..129db7ecf05
--- /dev/null
+++ b/libjava/gnu/gcj/convert/natIconv.cc
@@ -0,0 +1,142 @@
+// Input_iconv.java -- Java side of iconv() reader.
+
+/* Copyright (C) 2000 Red Hat, Inc.
+
+ This file is part of libgcj.
+
+This software is copyrighted work licensed under the terms of the
+Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
+details. */
+
+/* Author: Tom Tromey <tromey@redhat.com>. */
+
+#include <config.h>
+
+#include <gcj/cni.h>
+#include <jvm.h>
+
+#include <gnu/gcj/convert/Input_iconv.h>
+#include <gnu/gcj/convert/Output_iconv.h>
+#include <java/io/UnsupportedEncodingException.h>
+
+#ifdef HAVE_ICONV
+#include <iconv.h>
+#endif
+
+void
+gnu::gcj::convert::Input_iconv::init (jstring encoding)
+{
+#ifdef HAVE_ICONV
+ jsize len = _Jv_GetStringUTFLength (encoding);
+ char buffer[len];
+ _Jv_GetStringUTFRegion (encoding, 0, len, buffer);
+
+ iconv_t h = iconv_open ("UCS-2", buffer);
+ if (h == (iconv_t) -1)
+ JvThrow (new java::io::UnsupportedEncodingException);
+
+ JvAssert (h != NULL);
+ handle = reinterpret_cast<gnu::gcj::RawData *> (h);
+#else /* HAVE_ICONV */
+ // If no iconv, just throw an exception.
+ JvThrow (new java::io::UnsupportedEncodingException);
+#endif /* HAVE_ICONV */
+}
+
+void
+gnu::gcj::convert::Input_iconv::finalize (void)
+{
+#ifdef HAVE_ICONV
+ if (handle == NULL)
+ {
+ iconv_close ((iconv_t) handle);
+ handle = NULL;
+ }
+#endif /* HAVE_ICONV */
+}
+
+jint
+gnu::gcj::convert::Input_iconv::read (jcharArray outbuffer,
+ jint outpos, jint count)
+{
+#ifdef HAVE_ICONV
+ jint origpos = outpos;
+
+ jbyte *bytes = elements (inbuffer);
+ jchar *out = elements (outbuffer);
+ size_t inavail = inlength - inpos;
+ size_t old_in = inavail;
+ size_t outavail = count;
+ size_t old_out = outavail;
+
+ size_t r = iconv ((iconv_t) handle,
+ &bytes[inpos], &inavail,
+ &out[outpos], &outavail);
+ // FIXME: what if R==-1?
+
+ inpos += old_in - inavail;
+ return old_out - outavail;
+#else /* HAVE_ICONV */
+ return -1;
+#endif /* HAVE_ICONV */
+}
+
+void
+gnu::gcj::convert::Output_iconv::init (jstring encoding)
+{
+#ifdef HAVE_ICONV
+ jsize len = _Jv_GetStringUTFLength (encoding);
+ char buffer[len];
+ _Jv_GetStringUTFRegion (encoding, 0, len, buffer);
+
+ iconv_t h = iconv_open (buffer, "UCS-2");
+ if (h == (iconv_t) -1)
+ JvThrow (new java::io::UnsupportedEncodingException);
+
+ JvAssert (h != NULL);
+ handle = reinterpret_cast<gnu::gcj::RawData *> (h);
+#else /* HAVE_ICONV */
+ // If no iconv, just throw an exception.
+ JvThrow (new java::io::UnsupportedEncodingException);
+#endif /* HAVE_ICONV */
+}
+
+void
+gnu::gcj::convert::Output_iconv::finalize (void)
+{
+#ifdef HAVE_ICONV
+ if (handle == NULL)
+ {
+ iconv_close ((iconv_t) handle);
+ handle = NULL;
+ }
+#endif /* HAVE_ICONV */
+}
+
+jint
+gnu::gcj::convert::Output_iconv::write (jcharArray inbuffer,
+ jint inpos, jint count)
+{
+#ifdef HAVE_ICONV
+ jint origpos = outpos;
+
+ jchar *chars = elements (inbuffer);
+ jbyte *out = elements (buf);
+
+ size_t inavail = count;
+ size_t old_in = count;
+
+ size_t outavail = buf->length - count;
+ size_t old_out = outavail;
+
+ size_t r = iconv ((iconv_t) handle,
+ &chars[inpos], &inavail,
+ &out[count], &outavail);
+ // FIXME: what if R==-1?
+
+ count += old_out - outavail;
+ return old_in - inavail;
+#else /* HAVE_ICONV */
+ return -1;
+#endif /* HAVE_ICONV */
+}