[kaffe] CVS kaffe (robilad): Resynced with GNU Classpath: new IO decoders/encoders

Kaffe CVS cvs-commits at kaffe.org
Sun Apr 17 17:32:11 PDT 2005


PatchSet 5687 
Date: 2005/04/18 00:25:23
Author: robilad
Branch: HEAD
Tag: (none) 
Log:
Resynced with GNU Classpath: new IO decoders/encoders

2005-04-17  Dalibor Topic  <robilad at kaffe.org>

        Resynced with GNU Classpath.

        2005-03-28  Sven de Marothy  <sven at physto.se>

        * gnu/java/io/decode/DecoderUnicodeBig.java,
        * gnu/java/io/decode/DecoderUnicodeLittle.java,
        * gnu/java/io/encode/EncoderUnicodeBig.java,
        * gnu/java/io/encode/EncoderUnicodeLittle.java:
        New files.

Members: 
	ChangeLog:1.3853->1.3854 
	libraries/javalib/Makefile.am:1.335->1.336 
	libraries/javalib/Makefile.in:1.423->1.424 
	libraries/javalib/all.files:1.110->1.111 
	libraries/javalib/gnu/java/io/decode/DecoderUnicodeBig.java:INITIAL->1.1 
	libraries/javalib/gnu/java/io/decode/DecoderUnicodeLittle.java:INITIAL->1.1 
	libraries/javalib/gnu/java/io/encode/EncoderUnicodeBig.java:INITIAL->1.1 
	libraries/javalib/gnu/java/io/encode/EncoderUnicodeLittle.java:INITIAL->1.1 

Index: kaffe/ChangeLog
diff -u kaffe/ChangeLog:1.3853 kaffe/ChangeLog:1.3854
--- kaffe/ChangeLog:1.3853	Mon Apr 18 00:06:24 2005
+++ kaffe/ChangeLog	Mon Apr 18 00:25:23 2005
@@ -2,6 +2,18 @@
 
 	Resynced with GNU Classpath.
 
+	2005-03-28  Sven de Marothy  <sven at physto.se>
+
+        * gnu/java/io/decode/DecoderUnicodeBig.java,
+        * gnu/java/io/decode/DecoderUnicodeLittle.java,
+        * gnu/java/io/encode/EncoderUnicodeBig.java,
+        * gnu/java/io/encode/EncoderUnicodeLittle.java:
+        New files.
+
+2005-04-17  Dalibor Topic  <robilad at kaffe.org>
+
+	Resynced with GNU Classpath.
+
 	2005-03-26  Chris Burdess  <dog at gnu.org>
 
         * gnu/xml/dom/DomNode.java (notifyNode): grow listener array as
Index: kaffe/libraries/javalib/Makefile.am
diff -u kaffe/libraries/javalib/Makefile.am:1.335 kaffe/libraries/javalib/Makefile.am:1.336
--- kaffe/libraries/javalib/Makefile.am:1.335	Sun Apr 17 23:37:29 2005
+++ kaffe/libraries/javalib/Makefile.am	Mon Apr 18 00:25:28 2005
@@ -1148,6 +1148,8 @@
 	gnu/java/io/decode/DecoderUTF16LE.java \
 	gnu/java/io/decode/DecoderUTF32BE.java \
 	gnu/java/io/decode/DecoderUTF32LE.java \
+	gnu/java/io/decode/DecoderUnicodeBig.java \
+	gnu/java/io/decode/DecoderUnicodeLittle.java \
 	gnu/java/io/decode/DecoderWindows1250.java \
 	gnu/java/io/decode/DecoderWindows1252.java \
 	gnu/java/io/decode/KaffeDecoder.java
@@ -1167,6 +1169,8 @@
 	gnu/java/io/encode/EncoderUTF16LE.java \
 	gnu/java/io/encode/EncoderUTF32BE.java \
 	gnu/java/io/encode/EncoderUTF32LE.java \
+	gnu/java/io/encode/EncoderUnicodeBig.java \
+	gnu/java/io/encode/EncoderUnicodeLittle.java \
 	gnu/java/io/encode/EncoderWindows1250.java \
 	gnu/java/io/encode/EncoderWindows1252.java \
 	gnu/java/io/encode/KaffeEncoder.java
Index: kaffe/libraries/javalib/Makefile.in
diff -u kaffe/libraries/javalib/Makefile.in:1.423 kaffe/libraries/javalib/Makefile.in:1.424
--- kaffe/libraries/javalib/Makefile.in:1.423	Sun Apr 17 23:37:30 2005
+++ kaffe/libraries/javalib/Makefile.in	Mon Apr 18 00:25:29 2005
@@ -1577,6 +1577,8 @@
 	gnu/java/io/decode/DecoderUTF16LE.java \
 	gnu/java/io/decode/DecoderUTF32BE.java \
 	gnu/java/io/decode/DecoderUTF32LE.java \
+	gnu/java/io/decode/DecoderUnicodeBig.java \
+	gnu/java/io/decode/DecoderUnicodeLittle.java \
 	gnu/java/io/decode/DecoderWindows1250.java \
 	gnu/java/io/decode/DecoderWindows1252.java \
 	gnu/java/io/decode/KaffeDecoder.java
@@ -1597,6 +1599,8 @@
 	gnu/java/io/encode/EncoderUTF16LE.java \
 	gnu/java/io/encode/EncoderUTF32BE.java \
 	gnu/java/io/encode/EncoderUTF32LE.java \
+	gnu/java/io/encode/EncoderUnicodeBig.java \
+	gnu/java/io/encode/EncoderUnicodeLittle.java \
 	gnu/java/io/encode/EncoderWindows1250.java \
 	gnu/java/io/encode/EncoderWindows1252.java \
 	gnu/java/io/encode/KaffeEncoder.java
Index: kaffe/libraries/javalib/all.files
diff -u kaffe/libraries/javalib/all.files:1.110 kaffe/libraries/javalib/all.files:1.111
--- kaffe/libraries/javalib/all.files:1.110	Sun Apr 17 23:37:30 2005
+++ kaffe/libraries/javalib/all.files	Mon Apr 18 00:25:31 2005
@@ -722,6 +722,8 @@
 gnu/java/io/decode/DecoderEBCDIC_XML_US.java
 gnu/java/io/decode/DecoderEightBitLookup.java
 gnu/java/io/decode/Decoder.java
+gnu/java/io/decode/DecoderUnicodeBig.java
+gnu/java/io/decode/DecoderUnicodeLittle.java
 gnu/java/io/decode/DecoderUTF16BE.java
 gnu/java/io/decode/DecoderUTF16LE.java
 gnu/java/io/decode/DecoderUTF32BE.java
@@ -740,6 +742,8 @@
 gnu/java/io/encode/EncoderEBCDIC_XML_US.java
 gnu/java/io/encode/EncoderEightBitLookup.java
 gnu/java/io/encode/Encoder.java
+gnu/java/io/encode/EncoderUnicodeBig.java
+gnu/java/io/encode/EncoderUnicodeLittle.java
 gnu/java/io/encode/EncoderUTF16BE.java
 gnu/java/io/encode/EncoderUTF16LE.java
 gnu/java/io/encode/EncoderUTF32BE.java
===================================================================
Checking out kaffe/libraries/javalib/gnu/java/io/decode/DecoderUnicodeBig.java
RCS:  /home/cvs/kaffe/kaffe/libraries/javalib/gnu/java/io/decode/DecoderUnicodeBig.java,v
VERS: 1.1
***************
--- /dev/null	Sun Aug  4 19:57:58 2002
+++ kaffe/libraries/javalib/gnu/java/io/decode/DecoderUnicodeBig.java	Mon Apr 18 00:32:11 2005
@@ -0,0 +1,284 @@
+/* DecoderUnicodeBig.java -- Decoder for the UTF16BE encoding with a byte-order marker.
+   Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING.  If not, write to the
+Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+02111-1307 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library.  Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module.  An independent module is a module which is not derived from
+or based on this library.  If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so.  If you do not wish to do so, delete this
+exception statement from your version. */
+
+
+package gnu.java.io.decode;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.math.BigInteger;
+
+/**
+ * This class implements character decoding in the UCS Transformation Format 16
+ * Big Endian (UTF-16BE) encoding scheme, with a byte-order marker.
+ * 
+ * @version 0.0
+ * @author Sven de Marothy
+ */
+public class DecoderUnicodeBig extends Decoder
+{
+
+  // If we're operating in stream mode and we encounter a surrogate pair that
+  // we can't fit in the output buffer, we use this field to store the
+  // second half of the surrogate pair.
+  private int pendingChar = -1;
+
+  public DecoderUnicodeBig(InputStream in)
+  {
+    super(in, "UnicodeBig");
+  }
+
+  /**
+   * Counts the number of characters in a given byte array
+   */
+  public int charsInByteArray(byte[] buf, int offset, int len)
+  {
+    int num_chars = 0;
+    // Scan the buffer with minimal validation checks
+    for (int i = offset; i < offset + len; )
+      {
+	if (i + 1 < buf.length)
+	  {
+	    int word = new BigInteger(new byte[]
+		{ buf[i], buf[i + 1] }).intValue();
+
+	    if (word == -257)
+	      {
+		i += 2;
+		if (i >= buf.length)
+		  break;
+	      }
+	    else if (word >= 0xD800 && word <= 0xDBFF)
+	      {
+		if ((i + 3) >= buf.length)
+		  break;
+		int wordtrail = new BigInteger(new byte[]
+		    { buf[i + 2], buf[i + 3] }).intValue();
+
+		if (wordtrail >= 0xDC00 && wordtrail <= 0xDFFF)
+		  {
+		    ++num_chars;
+		    ++num_chars;
+		    i += 4;
+		    if (i >= buf.length)
+		      break;
+		  }
+		else
+		  {
+		    i += 2;
+		    if (i >= buf.length)
+		      break;
+		  }
+	      }
+	    else if (word >= 0xDC00 && word <= 0xDFFF)
+	      {
+		i += 2;
+		if (i >= buf.length)
+		  break;
+	      }
+	    else
+	      {
+		++num_chars;
+		i += 2;
+		if (i >= buf.length)
+		  break;
+	      }
+	  }
+	else
+	  break;
+      }
+
+    return num_chars;
+  }
+  
+  /**
+   * Transform the specified UnicodeBig encoded buffer to Unicode characters
+   */
+  public char[] convertToChars(byte[] buf, int buf_offset, int len,
+			       char cbuf[], int cbuf_offset)
+  {
+    int val;
+    // Scan the buffer with full validation checks
+    for (int i = buf_offset; i < buf_offset + len; )
+      {
+	if (i + 1 < buf.length)
+	  {
+	    int word = new BigInteger(new byte[]
+		{ buf[i], buf[i + 1] }).intValue();
+
+	    if (word == -257)
+	      {
+		// Byte order marker
+		i += 2;
+		if (i >= buf.length)
+		  break;
+	      }
+	    else if (word >= 0xD800 && word <= 0xDBFF)
+	      {
+		if ((i + 3) >= buf.length)
+		  break;
+		int wordtrail = new BigInteger(new byte[]
+		    { buf[i + 2], buf[i + 3] }).intValue();
+
+		if (wordtrail >= 0xDC00 && wordtrail <= 0xDFFF)
+		  {
+		    cbuf[cbuf_offset++] = (char) word;
+		    cbuf[cbuf_offset++] = (char) wordtrail;
+		    i += 4;
+		    if (i >= buf.length)
+		      break;
+		  }
+		else
+		  {
+		    // ELSE NOT LEGAL SURROGATE POSITION, IGNORE
+		    i += 2;
+		    if (i >= buf.length)
+		      break;
+		  }
+	      }
+	    else if (word >= 0xDC00 && word <= 0xDFFF)
+	      {
+		// NOT LEGAL SURROGATE POSITION, IGNORE
+		i += 2;
+		if (i >= buf.length)
+		  break;
+	      }
+	    else
+	      {
+		cbuf[cbuf_offset++] = (char) word;
+		i += 2;
+		if (i >= buf.length)
+		  break;
+	      }
+	  }
+	else
+	  {
+	    // UNEXPECTED END, IGNORE
+	    break;
+	  }
+      }
+
+    return cbuf;
+  }
+  
+  /**
+   * Reads chars from a UnicodeBig encoded byte stream
+   */
+  public int read(char[] cbuf, int offset, int len) throws IOException
+  {
+    if (pendingChar != -1 && len > 0)
+      {
+	cbuf[offset++] = (char) pendingChar;
+	pendingChar = -1;
+      }
+
+    byte[] buffer = new byte[4];
+    for (int i = offset; i < offset + len; )
+      {
+	// Read a byte
+	int b = in.read();
+	if (b == -1)
+	  if (i == offset)
+	    return -1;
+	  else
+	    return i - offset;
+	buffer[0] = (byte) b;
+	b = in.read();
+	if (b == -1)
+	  break;
+	buffer[1] = (byte) b;
+	int word = new BigInteger(new byte[]
+	    { buffer[0], buffer[1] }).intValue();
+
+	if (word == -257)
+	  {
+	    if (in.available() <= 0)
+	      return 1 + i - offset;
+	    continue;
+	  }
+	else if (word >= 0xD800 && word <= 0xDBFF)
+	  {
+	    b = in.read();
+	    if (b == -1)
+	      break;
+	    buffer[2] = (byte) b;
+	    b = in.read();
+	    if (b == -1)
+	      break;
+	    buffer[3] = (byte) b;
+	    int wordtrail = new BigInteger(new byte[]
+		{ buffer[2], buffer[3] }).intValue();
+
+	    if (wordtrail >= 0xDC00 && wordtrail <= 0xDFFF)
+	      {
+		cbuf[i++] = (char) word;
+		if (i < offset + len)
+		  cbuf[i] = (char) wordtrail;
+		else
+		  pendingChar = (char) wordtrail;
+	      }
+	    else
+	      {
+		cbuf[i] = (char) wordtrail;
+		if (in.available() <= 0)
+		  return (1 + i - offset);
+		continue;
+	      }
+	  }
+	else if (word >= 0xDC00 && word <= 0xDFFF)
+	  {
+	    if (in.available() <= 0)
+	      return (1 + i - offset);
+	    continue;
+	  }
+	else
+	  cbuf[i] = (char) word;
+
+	// if no more bytes available, terminate loop early, instead of
+	// blocking in in.read().
+	// Do not test this in the for condition: it must call in.read() at
+	// least once (and thus block if "in" is empty).
+	if (in.available() <= 0)
+	  return (1 + i - offset);
+	i++;
+      }
+
+    return len;
+  }
+
+} // class DecoderUnicodeBig
+
===================================================================
Checking out kaffe/libraries/javalib/gnu/java/io/decode/DecoderUnicodeLittle.java
RCS:  /home/cvs/kaffe/kaffe/libraries/javalib/gnu/java/io/decode/DecoderUnicodeLittle.java,v
VERS: 1.1
***************
--- /dev/null	Sun Aug  4 19:57:58 2002
+++ kaffe/libraries/javalib/gnu/java/io/decode/DecoderUnicodeLittle.java	Mon Apr 18 00:32:11 2005
@@ -0,0 +1,277 @@
+/* DecoderUnicodeLittle.java -- Decoder for the UTF16LE character set, with a byte order marker.
+   Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING.  If not, write to the
+Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+02111-1307 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library.  Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module.  An independent module is a module which is not derived from
+or based on this library.  If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so.  If you do not wish to do so, delete this
+exception statement from your version. */
+
+
+package gnu.java.io.decode;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.math.BigInteger;
+
+/**
+ * This class implements character decoding in the UCS Transformation Format 16
+ * Little Endian (UTF-16LE) encoding scheme with a byte-order marker.
+ * 
+ * @version 0.0
+ * @author Sven de Marothy
+ */
+public class DecoderUnicodeLittle extends Decoder
+{
+
+  // If we're operating in stream mode and we encounter a surrogate pair that
+  // we can't fit in the output buffer, we use this field to store the
+  // second half of the surrogate pair.
+  private int pendingChar = -1;
+
+  public DecoderUnicodeLittle(InputStream in)
+  {
+    super(in, "UnicodeLittle");
+  }
+
+  /**
+   * Counts the number of characters in a given byte array
+   */
+  public int charsInByteArray(byte[] buf, int offset, int len)
+  {
+    int num_chars = 0;
+    // Scan the buffer with minimal validation checks
+    for (int i = offset; i < offset + len; )
+      {
+	if (i + 1 < buf.length)
+	  {
+	    int word = new BigInteger(new byte[]
+		{ buf[i + 1], buf[i] }).intValue();
+	    if (word == -257)
+	      {
+		i += 2;
+		if (i >= buf.length)
+		  break;
+	      }
+	    else if (word >= 0xD800 && word <= 0xDBFF)
+	      {
+		if ((i + 3) >= buf.length)
+		  break;
+		int wordtrail = new BigInteger(new byte[]
+		    { buf[i + 3], buf[i + 2] }).intValue();
+
+		if (wordtrail >= 0xDC00 && wordtrail <= 0xDFFF)
+		  {
+		    ++num_chars;
+		    i += 4;
+		    if (i >= buf.length)
+		      break;
+		  }
+		else
+		  {
+		    i += 2;
+		    if (i >= buf.length)
+		      break;
+		  }
+	      }
+	    else if (word >= 0xDC00 && word <= 0xDFFF)
+	      {
+		i += 2;
+		if (i >= buf.length)
+		  break;
+	      }
+	    else
+	      {
+		++num_chars;
+		i += 2;
+		if (i >= buf.length)
+		  break;
+	      }
+	  }
+	else
+	  break;
+      }
+
+    return num_chars;
+  }
+  
+  /**
+   * Transform the specified UnicodeLittle encoded buffer to Unicode characters
+   */
+  public char[] convertToChars(byte[] buf, int buf_offset, int len,
+			       char cbuf[], int cbuf_offset)
+  {
+    int val;
+    // Scan the buffer with full validation checks
+    for (int i = buf_offset; i < buf_offset + len; )
+      {
+	if (i + 1 < buf.length)
+	  {
+	    int word = new BigInteger(new byte[]
+		{ buf[i + 1], buf[i] }).intValue();
+
+	    if (word == -257)
+	      {
+		i += 2;
+		if (i >= buf.length)
+		  break;
+	      }
+	    else if (word >= 0xD800 && word <= 0xDBFF)
+	      {
+		if ((i + 3) >= buf.length)
+		  break;
+		int wordtrail = new BigInteger(new byte[]
+		    { buf[i + 3], buf[i + 2] }).intValue();
+		
+		if (wordtrail >= 0xDC00 && wordtrail <= 0xDFFF)
+		  {
+		    cbuf[cbuf_offset++] = (char) word;
+		    cbuf[cbuf_offset++] = (char) wordtrail;
+		    i += 4;
+		    if (i >= buf.length)
+		      break;
+		  }
+		else
+		  {
+		    // ELSE NOT LEGAL SURROGATE POSITION, IGNORE
+		    i += 2;
+		    if (i >= buf.length)
+		      break;
+		  }
+	      }
+	    else if (word >= 0xDC00 && word <= 0xDFFF)
+	      {
+		// NOT LEGAL SURROGATE POSITION, IGNORE
+		i += 2;
+		if (i >= buf.length)
+		  break;
+	      }
+	    else
+	      {
+		cbuf[cbuf_offset++] = (char) word;
+		i += 2;
+		if (i >= buf.length)
+		  break;
+	      }
+	  }
+	else
+	  {
+	    // UNEXPECTED END, IGNORE
+	    break;
+	  }
+      }
+
+    return cbuf;
+  }
+  
+  /** ********************************************************************** */
+  
+  /**
+   * Reads chars from a UnicodeLittle encoded byte stream
+   */
+  public int read(char[] cbuf, int offset, int len) throws IOException
+  {
+    byte[] buffer = new byte[4];
+    for (int i = offset; i < offset + len; )
+      {
+	// Read a byte
+	int b = in.read();
+	if (b == -1)
+	  if (i == offset)
+	    return -1;
+	  else
+	    return i - offset;
+	buffer[0] = (byte) b;
+	b = in.read();
+	if (b == -1)
+	  break;
+	buffer[1] = (byte) b;
+	int word = new BigInteger(new byte[]
+	    { buffer[1], buffer[0] }).intValue();
+
+	if (word == -257)
+	  {
+	    if (in.available() <= 0)
+	      return (1 + i - offset);
+	    continue;
+	  }
+	else if (word >= 0xD800 && word <= 0xDBFF)
+	  {
+	    b = in.read();
+	    if (b == -1)
+	      break;
+	    buffer[2] = (byte) b;
+	    b = in.read();
+	    if (b == -1)
+	      break;
+	    buffer[3] = (byte) b;
+	    int wordtrail = new BigInteger(new byte[]
+		{ buffer[3], buffer[2] }).intValue();
+
+	    if (wordtrail >= 0xDC00 && wordtrail <= 0xDFFF)
+	      {
+		cbuf[i++] = (char) word;
+		if (i < offset + len)
+		  cbuf[i] = (char) wordtrail;
+		else
+		  pendingChar = (char) wordtrail;
+	      }
+	    else
+	      {
+		cbuf[i] = (char) wordtrail;
+		if (in.available() <= 0)
+		  return (1 + i - offset);
+		continue;
+	      }
+	  }
+	else if (word >= 0xDC00 && word <= 0xDFFF)
+	  {
+	    if (in.available() <= 0)
+	      return (1 + i - offset);
+	    continue;
+	  }
+	else
+	  cbuf[i] = (char) word;
+
+	// if no more bytes available, terminate loop early, instead of
+	// blocking in in.read().
+	// Do not test this in the for condition: it must call in.read() at
+	// least once (and thus block if "in" is empty).
+	if (in.available() <= 0)
+	  return 1 + i - offset;
+
+	i++;
+      }
+
+    return len;
+  }
+  
+} // class DecoderUnicodeLittle
===================================================================
Checking out kaffe/libraries/javalib/gnu/java/io/encode/EncoderUnicodeBig.java
RCS:  /home/cvs/kaffe/kaffe/libraries/javalib/gnu/java/io/encode/EncoderUnicodeBig.java,v
VERS: 1.1
***************
--- /dev/null	Sun Aug  4 19:57:58 2002
+++ kaffe/libraries/javalib/gnu/java/io/encode/EncoderUnicodeBig.java	Mon Apr 18 00:32:11 2005
@@ -0,0 +1,110 @@
+/* EncoderUnicodeBig.java -- Encoder for the UTF-16BE encoding with byte-order marker.
+   Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING.  If not, write to the
+Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+02111-1307 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library.  Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module.  An independent module is a module which is not derived from
+or based on this library.  If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so.  If you do not wish to do so, delete this
+exception statement from your version. */
+
+
+package gnu.java.io.encode;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.math.BigInteger;
+
+/**
+ * This class implements character encoding in the UCS Transformation Format 16
+ * Big Endian (UTF-16BE) encoding scheme, with byte-order markers.
+ * 
+ * @version 0.0
+ * @author Sven de Marothy
+ */
+public class EncoderUnicodeBig extends Encoder
+{
+
+  public EncoderUnicodeBig(OutputStream out)
+  {
+    super(out, "UnicodeBig");
+  }
+
+  /**
+   * Returns the number of bytes the specified char array will be encoded
+   * into
+   */
+  public int bytesInCharArray(char[] buf, int offset, int len)
+  {
+      return 2 + 2 * len; // 2 extra bytes for the byte-order marker
+  }
+
+  /**
+   * This method converts a char array to bytes
+   */
+  public byte[] convertToBytes(char[] buf, int buf_offset, int len,
+			       byte[] bbuf, int bbuf_offset)
+  {
+    int val;
+    // Write byte-order marker
+    bbuf[bbuf_offset++] = (byte)0xFE;
+    bbuf[bbuf_offset++] = (byte)0xFF;
+
+    // Scan the buffer with full validation checks
+    for (int i = buf_offset; i < buf_offset + len; i++) {
+      byte [] newArray = new BigInteger("" + ((int) buf[i])).toByteArray();
+      if (newArray.length < 2)
+	{
+	  bbuf[bbuf_offset++] = 0;
+	  bbuf[bbuf_offset++] = newArray[0];
+	}
+      else
+	{
+	  bbuf[bbuf_offset++] = newArray[0];
+	  bbuf[bbuf_offset++] = newArray[1];
+	}
+    }
+    return bbuf;
+  }
+
+  /**
+   * Writes a char array as bytes to the underlying stream.
+   */
+  public void write(char[] buf, int offset, int len) throws IOException
+  {
+    byte[] bbuf = new byte[bytesInCharArray(buf, offset, len)];
+    convertToBytes(buf, offset, len, bbuf, 0);
+    out.write(bbuf);
+  }
+
+} // class EncoderUnicodeBig
+
+
+
===================================================================
Checking out kaffe/libraries/javalib/gnu/java/io/encode/EncoderUnicodeLittle.java
RCS:  /home/cvs/kaffe/kaffe/libraries/javalib/gnu/java/io/encode/EncoderUnicodeLittle.java,v
VERS: 1.1
***************
--- /dev/null	Sun Aug  4 19:57:58 2002
+++ kaffe/libraries/javalib/gnu/java/io/encode/EncoderUnicodeLittle.java	Mon Apr 18 00:32:11 2005
@@ -0,0 +1,110 @@
+/* EncoderUnicodeLittle.java -- Encoder for the UTF-16LE encoding, with byte-order markers.
+   Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING.  If not, write to the
+Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+02111-1307 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library.  Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module.  An independent module is a module which is not derived from
+or based on this library.  If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so.  If you do not wish to do so, delete this
+exception statement from your version. */
+
+
+package gnu.java.io.encode;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.math.BigInteger;
+
+/**
+ * This class implements character encoding in the UCS Transformation Format 16
+ * Big Endian (UTF-16BE) encoding scheme.
+ * 
+ * @version 0.0
+ * @author Sven de Marothy
+ */
+public class EncoderUnicodeLittle extends Encoder
+{
+
+  public EncoderUnicodeLittle(OutputStream out)
+  {
+    super(out, "UnicodeLittle");
+  }
+
+  /**
+   * Returns the number of bytes the specified char array will be encoded
+   * into
+   */
+  public int bytesInCharArray(char[] buf, int offset, int len)
+  {
+      return 2 + 2 * len; // 2 extra bytes for the byte-order marker
+  }
+
+  /**
+   * This method converts a char array to bytes
+   */
+  public byte[] convertToBytes(char[] buf, int buf_offset, int len,
+			       byte[] bbuf, int bbuf_offset)
+  {
+    int val;
+    // Write byte-order marker
+    bbuf[bbuf_offset++] = (byte)0xFF;
+    bbuf[bbuf_offset++] = (byte)0xFE;
+
+    // Scan the buffer with full validation checks
+    for (int i = buf_offset; i < buf_offset + len; i++) {
+      byte [] newArray = new BigInteger("" + ((int) buf[i])).toByteArray();
+      if (newArray.length < 2)
+	{
+	  bbuf[bbuf_offset++] = newArray[0];
+	  bbuf[bbuf_offset++] = 0;
+	}
+      else
+	{
+	  bbuf[bbuf_offset++] = newArray[1];
+	  bbuf[bbuf_offset++] = newArray[0];
+	}
+    }
+    return bbuf;
+  }
+
+  /**
+   * Writes a char array as bytes to the underlying stream.
+   */
+  public void write(char[] buf, int offset, int len) throws IOException
+  {
+    byte[] bbuf = new byte[bytesInCharArray(buf, offset, len)];
+    convertToBytes(buf, offset, len, bbuf, 0);
+    out.write(bbuf);
+  }
+
+} // class EncoderUnicodeLittle
+
+
+




More information about the kaffe mailing list