[kaffe] CVS kaffe (kaz): Added new files as a preparation for importing GNU Classpath's

Kaffe CVS cvs-commits at kaffe.org
Mon Jan 19 08:30:03 PST 2004


PatchSet 4353 
Date: 2004/01/19 16:27:19
Author: kaz
Branch: HEAD
Tag: (none) 
Log:
Added new files as a preparation for importing GNU Classpath's
java.io.InputStreamReader and java.io.OutputStreamWriter.

Members: 
	libraries/javalib/gnu/java/io/EncodingManager.java:INITIAL->1.1 
	libraries/javalib/gnu/java/io/decode/Decoder.java:INITIAL->1.1 
	libraries/javalib/gnu/java/io/decode/KaffeDecoder.java:INITIAL->1.1 
	libraries/javalib/gnu/java/io/encode/Encoder.java:INITIAL->1.1 
	libraries/javalib/gnu/java/io/encode/KaffeEncoder.java:INITIAL->1.1 

===================================================================
Checking out kaffe/libraries/javalib/gnu/java/io/EncodingManager.java
RCS:  /home/cvs/kaffe/kaffe/libraries/javalib/gnu/java/io/EncodingManager.java,v
VERS: 1.1
***************
--- /dev/null	Sun Aug  4 19:57:58 2002
+++ kaffe/libraries/javalib/gnu/java/io/EncodingManager.java	Mon Jan 19 16:29:50 2004
@@ -0,0 +1,435 @@
+/* EncodingManager.java -- Manages character encoding translators
+   Copyright (C) 1998, 1999 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;
+
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.UnsupportedEncodingException;
+import java.util.Hashtable;
+import gnu.java.io.decode.*;
+import gnu.java.io.encode.*;
+
+
+import java.io.FileOutputStream;
+
+// gnu.java.io.EncodingManager that works with kaffe
+
+/**
+  * This class is used to create new instances of Decoders for a specified
+  * encoding scheme.  These instances are cache for fast subsequent retrieval 
+  * if necessary.
+  *
+  * @version 0.0
+  *
+  * @author Aaron M. Renn (arenn at urbanophile.com),
+  * Ito Kazumitsu (kaz at maczuka.gcd.org)
+  */
+public class EncodingManager
+{
+
+/*************************************************************************/
+
+/*
+ * Class Variables
+ */
+
+/**
+  * This is the system default character encoding
+  */
+private static String default_encoding;
+
+/**
+  * This is the default instance of the default <code>Decoder</code>, put
+  * here to make access even faster than through the Hashtable
+  */
+private static Decoder default_decoder_instance;
+
+/**
+  * This is the default instance of the default <code>Encoder</code>, put
+  * here to make access even faster than through the Hashtable
+  */
+private static Encoder default_encoder_instance;
+
+/**
+  * This is hash table of cached instances of <code>Decoder</code> objects
+  */
+private static Hashtable decoder_instances;
+
+/**
+  * This is hash table of cached instances of <code>Encoder</code> objects
+  */
+private static Hashtable encoder_instances;
+
+
+static 
+{
+  // Initialize hashtables
+  decoder_instances = new Hashtable();
+  encoder_instances = new Hashtable();
+
+  // Find the system default encoding name
+  String default_encoding = System.getProperty("file.encoding","8859_1");
+
+  // Load the class
+  try
+    {
+      // First the Decoder side
+
+      default_decoder_instance = 
+            new KaffeDecoder(null, default_encoding);
+        
+      // Now the Encoder side
+
+      default_encoder_instance = 
+            new KaffeEncoder(null, default_encoding);
+        
+      // Add items to the hashtable;
+      decoder_instances.put(default_encoding, default_decoder_instance);
+      encoder_instances.put(default_encoding, default_encoder_instance);
+    }
+  catch(Exception e)
+    {
+      throw new Error("Cannot load system default encoding '" + 
+                     default_encoding + "': " + e.getMessage());
+    }
+}  
+
+/*************************************************************************/
+
+/*
+ * Class Methods
+ */
+
+/*************************************************************************/
+
+/**
+  * This method returns the default instance of the default <code>Decoder</code> 
+  * which must be used only for calling the static byte array conversion methods.
+  * Calling any instance methods on this object will result in a 
+  * <code>NullPointerException</code>.
+  *
+  * @return An instance of the default <code>Decoder</code>.
+  */
+public static Decoder
+getDecoder()
+{
+  return(default_decoder_instance);
+}
+
+/*************************************************************************/
+
+/**
+  * This method returns the default instance of the <code>Decoder</code>
+  * for the named encoding.  This must be used only for calling the static
+  * byte array conversion methods.  Calling any instance methods on this
+  * object will result in a <code>NullPointerException</code>
+  *
+  * This form of <code>getDecoder</code> caches the instance that is returned.  If
+  * this decoder is for a complex character encoding that may use lots of
+  * memory and is only needed once or infrequently, consider using the form 
+  * of the <code>getDecoder</code> method that does not cache the results 
+  * to save resources.
+  *
+  * @param encoding The name of the encoding to retrieve a <code>Decoder</code> for.
+  *
+  * @return An instance of the <code>Decoder</code> for the named encoding.
+  *
+  * @exception UnsupportedEncodingException If a <code>Decoder</code> for the named encoding cannot be found
+  */
+public static Decoder
+getDecoder(String encoding) throws UnsupportedEncodingException
+{
+  return(getDecoder(encoding, true));
+}
+
+/*************************************************************************/
+
+/**
+  * This method returns the default instance of the <code>Decoder</code>
+  * for the named encoding.  This must be used only for calling the static
+  * byte array conversion methods.  Calling any instance methods on this
+  * object will result in a <code>NullPointerException</code>
+  *
+  * @param encoding The name of the encoding to retrieve a <code>Decoder</code> for.
+  * @param cache <code>true</code> to cache this encoding, <code>false</code> otherwise
+  *
+  * @return An instance of the <code>Decoder</code> for the named encoding.
+  *
+  * @exception UnsupportedEncodingException If a <code>Decoder</code> for the named encoding cannot be found
+  */
+public static Decoder
+getDecoder(String encoding, boolean cache) throws UnsupportedEncodingException
+{
+  Decoder dec = (Decoder)decoder_instances.get(encoding);
+  if (dec != null)
+    return(dec);
+ 
+  dec = getDecoder(null, encoding, cache);
+
+  if (cache)
+    decoder_instances.put(encoding, dec);
+
+  return(dec);
+}
+
+/*************************************************************************/
+
+/**
+  * This method returns a <code>Decoder</code> object that can read
+  * from the specified <code>InputStream</code> using the default 
+  * encoding.
+  *
+  * @param in The <code>InputStream</code> to read from
+  */
+public static Decoder
+getDecoder(InputStream in)
+{
+  try
+    {
+      return(getDecoder(in, default_encoding, false));
+    }
+  catch(Exception e)
+    {
+      throw new Error("Unexpected problems with default decoder");
+    }
+}
+
+/*************************************************************************/
+
+/**
+  * This method returns a <code>Decoder</code> object that can read from
+  * the specified <code>InputStream</code> using the named encoding
+  *
+  * This form of <code>getDecoder</code> caches the instance that is returned.  If
+  * this decoder is for a complex character encoding that may use lots of
+  * memory and is only needed once or infrequently, consider using the form 
+  * of the <code>getDecoder</code> method that does not cache the results 
+  * to save resources.
+  *
+  * @param in The <code>InputStream</code> to read from
+  * @param encoding The name of the character encoding scheme to use
+  *
+  * @exception UnsupportedEncodingException If a <code>Decoder</code> for this encoding cannot be found
+  */
+public static Decoder
+getDecoder(InputStream in, String encoding) throws UnsupportedEncodingException
+{
+  return(getDecoder(in, encoding, true));
+}
+
+/*************************************************************************/
+
+/**
+  * This method returns a <code>Decoder</code> object that can read from
+  * the specified <code>InputStream</code> using the named encoding
+  *
+  * @param in The <code>InputStream</code> to read from
+  * @param encoding The name of the character encoding scheme to use
+  * @param cache <code>true</code> to cache the returned <code>Decoder</code>, <code>false</code> otherwise.  Actually, not used.
+  *
+  * @exception UnsupportedEncodingException If a <code>Decoder</code> for this encoding cannot be found
+  */
+public static Decoder
+getDecoder(InputStream in, String encoding, boolean cache) 
+                              throws UnsupportedEncodingException
+{
+  Decoder dec = null;
+  try
+    {
+      dec = new KaffeDecoder(in, encoding);
+    }
+  catch(Exception e)
+    {
+      throw new UnsupportedEncodingException(encoding + ": " + e.getMessage());
+    }
+
+  return(dec);
+}
+
+/*************************************************************************/
+
+/**
+  * This method returns the default instance of the default <code>Encoder</code> 
+  * which must be used only for calling the static byte array conversion methods.
+  * Calling any instance methods on this object will result in a 
+  * <code>NullPointerException</code>.
+  *
+  * @return An instance of the default <code>Encoder</code>.
+  */
+public static Encoder
+getEncoder()
+{
+  return(default_encoder_instance);
+}
+
+/*************************************************************************/
+
+/**
+  * This method returns the default instance of the <code>Encoder</code>
+  * for the named encoding.  This must be used only for calling the static
+  * byte array conversion methods.  Calling any instance methods on this
+  * object will result in a <code>NullPointerException</code>
+  *
+  * This form of <code>getEncoder</code> caches the instance that is returned.  If
+  * this decoder is for a complex character encoding that may use lots of
+  * memory and is only needed once or infrequently, consider using the form 
+  * of the <code>getEncoder</code> method that does not cache the results 
+  * to save resources.
+  *
+  * @param encoding The name of the encoding to retrieve a <code>Encoder</code> for.
+  *
+  * @return An instance of the <code>Encoder</code> for the named encoding.
+  *
+  * @exception UnsupportedEncodingException If a <code>Encoder</code> for the named encoding cannot be found
+  */
+public static Encoder
+getEncoder(String encoding) throws UnsupportedEncodingException
+{
+  return(getEncoder(encoding, true));
+}
+
+/*************************************************************************/
+
+/**
+  * This method returns the default instance of the <code>Encoder</code>
+  * for the named encoding.  This must be used only for calling the static
+  * byte array conversion methods.  Calling any instance methods on this
+  * object will result in a <code>NullPointerException</code>
+  *
+  * @param encoding The name of the encoding to retrieve a <code>Encoder</code> for.
+  * @param cache <code>true</code> to cache this encoding, <code>false</code> otherwise
+  *
+  * @return An instance of the <code>Encoder</code> for the named encoding.
+  *
+  * @exception UnsupportedEncodingException If a <code>Encoder</code> for the named encoding cannot be found
+  */
+public static Encoder
+getEncoder(String encoding, boolean cache) throws UnsupportedEncodingException
+{
+  Encoder enc = (Encoder)encoder_instances.get(encoding);
+  if (enc != null)
+    return(enc);
+ 
+  enc = getEncoder(null, encoding, cache);
+
+  if (cache)
+    encoder_instances.put(encoding, enc);
+
+  return(enc);
+}
+
+/*************************************************************************/
+
+/**
+  * This method returns an <code>Encoder</code> object that can write
+  * to the specified <code>OutputStream</code> using the default 
+  * encoding.
+  *
+  * @param out The <code>OutputStream</code> to read from
+  */
+public static Encoder
+getEncoder(OutputStream out)
+{
+  Encoder enc = null;
+  try
+    {
+      enc = getEncoder(out, default_encoding, false);
+    }
+  catch(Exception e)
+    {
+      throw new Error("Unexpected problems with default encoder");
+    }
+
+  return(enc);
+}
+
+/*************************************************************************/
+
+/**
+  * This method returns an <code>Encoder</code> object that can write to
+  * the specified <code>OutputStream</code> using the named encoding
+  *
+  * This form of <code>getencoder</code> caches the instance that is returned.  If
+  * this encoder is for a complex character encoding that may use lots of
+  * memory and is only needed once or infrequently, consider using the form 
+  * of the <code>getEncoder</code> method that does not cache the results 
+  * to save resources.
+  *
+  * @param in The <code>OutputStream</code> to read from
+  * @param encoding The name of the character encoding scheme to use
+  *
+  * @exception UnsupportedEncodingException If an <code>Encoder</code> for this encoding cannot be found
+  */
+public static Encoder
+getEncoder(OutputStream in, String encoding) throws UnsupportedEncodingException
+{
+  return(getEncoder(in, encoding, true));
+}
+
+/*************************************************************************/
+
+/**
+  * This method returns an <code>Encoder</code> object that can write to
+  * the specified <code>OutputStream</code> using the named encoding
+  *
+  * @param in The <code>OutputStream</code> to read from
+  * @param encoding The name of the character encoding scheme to use
+  * @param cache <code>true</code> to cache the returned <code>Encoder</code>, <code>false</code> otherwise. Actually, not used.
+  *
+  * @exception UnsupportedEncodingException If a <code>Decoder</code> for this encoding cannot be found
+  */
+public static Encoder
+getEncoder(OutputStream out, String encoding, boolean cache) 
+                              throws UnsupportedEncodingException
+{
+  Encoder enc = null;
+  try
+    {
+      enc = new KaffeEncoder(out, encoding); 
+    }
+  catch(Exception e)
+    {
+      throw new UnsupportedEncodingException(encoding + ": " + e.getMessage());
+    }
+
+  return(enc);
+}
+
+} // class EncodingManager
+
===================================================================
Checking out kaffe/libraries/javalib/gnu/java/io/decode/Decoder.java
RCS:  /home/cvs/kaffe/kaffe/libraries/javalib/gnu/java/io/decode/Decoder.java,v
VERS: 1.1
***************
--- /dev/null	Sun Aug  4 19:57:58 2002
+++ kaffe/libraries/javalib/gnu/java/io/decode/Decoder.java	Mon Jan 19 16:29:50 2004
@@ -0,0 +1,308 @@
+/* Decoder.java -- Base class for byte->char decoders
+   Copyright (C) 1998 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.InputStream;
+import java.io.Reader;
+import java.io.IOException;
+import java.io.CharConversionException;
+
+/**
+  * This class is the base class for decoding bytes into character. 
+  *
+  * @version 0.0
+  *
+  * @author Aaron M. Renn (arenn at urbanophile.com)
+  */
+public abstract class Decoder extends Reader
+{
+
+/*************************************************************************/
+
+/*
+ * Class Variables
+ */
+
+/**
+  * This is the name of the current encoding. MUST be overriden by
+  * subclasses.
+  */
+protected static String scheme_name = "undefined";
+
+/**
+  * This is a description of the current encoding.  MUST be overridden
+  * by subclasses.
+  */
+protected static String scheme_description = "undefined";
+
+/*************************************************************************/
+
+/*
+ * Instance Variables
+ */
+
+/**
+  * This is the <code>InputStream</code> bytes are read from
+  */
+protected InputStream in;
+
+/*************************************************************************/
+
+/*
+ * Class Methods
+ */
+
+/**
+  * This method returns the name of the encoding scheme in use
+  *
+  * @return The name of the encoding scheme
+  */
+public static String
+getSchemeName()
+{
+  return(scheme_name);
+}
+
+/*************************************************************************/
+
+/**
+  * This method returns a description of the encoding scheme in use
+  *
+  * @param A description of the decoding scheme.
+  */
+public static String
+getSchemeDescription()
+{
+  return(scheme_description);
+}
+
+/*************************************************************************/
+
+/*
+ * Constructors
+ */
+
+/**
+  * This method initializes a new <code>Decoder</code> to read from the
+  * specified <code>InputStream</code>.
+  *
+  * @param in The <code>InputStream</code> to read from
+  */
+public
+Decoder(InputStream in)
+{
+  this.in = in;
+}
+
+/*************************************************************************/
+
+/*
+ * Instance Methods
+ */
+
+/**
+  * For a given set of bytes, this method returns the number of characters
+  * that byte array will translate into.  If the bytes do not all translate 
+  * into an even number of charcters, an exception will be thrown.    
+  * Additionally, an exception may be thrown if any of the bytes are not
+  * valid for the given encoding.  (This is not guaranteed to happen however).
+  *
+  * @param buf The array of bytes to determine the number of characters from.
+  *
+  * @return The number of characters than can be decoded from the byte array
+  *
+  * @exception CharConversionException If the bytes do not evenly translate to characters, or an invalid byte is encountered.
+  */
+public int
+charsInByteArray(byte[] buf) throws CharConversionException
+{
+  return(charsInByteArray(buf, 0, buf.length));
+}
+
+/*************************************************************************/
+
+/**
+  * For a <code>len</code> bytes in the specified array, starting from
+  * index <code>offset</code>, this method returns the number of characters
+  * that byte array will translate into.  If the bytes do not all translate 
+  * into an even number of charcters, an exception will be thrown.    
+  * Additionally, an exception may be thrown if any of the bytes are not
+  * valid for the given encoding.  (This is not guaranteed to happen however).
+  *
+  * @param buf The array of bytes to determine the number of characters from.
+  * @param offset The index to start examining bytes from
+  * @param len The number of bytes to be converted
+  *
+  * @return The number of characters than can be decoded from the byte array
+  *
+  * @exception CharConversionException If the bytes do not evenly translate to characters, or an invalid byte is encountered.
+  */
+public abstract int
+charsInByteArray(byte[] buf, int offset, int len) throws CharConversionException;
+
+/*************************************************************************/
+
+/**
+  * This method converts an array of bytes to chars, returning the result in
+  * a newly allocated char array.
+  *
+  * @param buf The byte array to convert
+  *
+  * @return The converted char array
+  *
+  * @exception CharConversionException If an error occurs, such as an invalid byte in the source array.
+  */
+public char[]
+convertToChars(byte[] buf) throws CharConversionException
+{
+  return(convertToChars(buf, 0, buf.length));
+}
+
+/*************************************************************************/
+
+/**
+  * This method converts <code>len<code> bytes from a specified array to
+  * characters starting at index <code>offset</code> into the array.  The
+  * results are returned in a newly allocated char array.
+  *
+  * @param buf The byte array to convert
+  * @param offset The index into the array to start converting from
+  * @param len The number of bytes to convert
+  *
+  * @return The converted char array
+  *
+  * @exception CharConversionException If an error occurs, such as an invalid byte in the source array.
+  */
+public char[]
+convertToChars(byte[] buf, int offset, int len) throws CharConversionException
+{
+  char[] cbuf = new char[charsInByteArray(buf, offset, len)];
+
+  return(convertToChars(buf, offset, len, cbuf, 0));
+}
+
+/*************************************************************************/
+
+/**
+  * This method converts all the bytes in the specified array to characters
+  * and stores them into the supplied character array starting at index
+  * <code>cbuf_offset</code> into the destination char array.  The array itself
+  * is returned as a convenience for passing to other methods.
+  *
+  * Note that there must be enough space in the destination array to hold
+  * all the converted bytes, or an exception will be thrown.
+  *
+  * @param buf The byte array to convert
+  * @param cbuf The char array to store converted characters into
+  * @param cbuf_offset The index into the char array to start storing converted characters.
+  *
+  * @return The char array passed by the caller as a param, now filled with converted characters.
+  *
+  * @exception ArrayIndexOutOfBoundsException If the destination char array is not big enough to hold all the converted characters
+  * @exception CharConversionException If an error occurs, such as an invalid byte in the source array.
+  */
+public char[]
+convertToChars(byte[] buf, char[] cbuf, int cbuf_offset) throws
+                  CharConversionException
+{
+  return(convertToChars(buf, 0, buf.length, cbuf, cbuf_offset));
+}
+
+/*************************************************************************/
+
+/**
+  * This method converts <code>len</code> bytes in the specified array to 
+  * characters starting at position <code>buf_offset</code> in the array
+  * and stores them into the supplied character array starting at index
+  * <code>cbuf_offset</code> into the destination char array.  The array itself
+  * is returned as a convenience for passing to other methods.
+  *
+  * Note that there must be enough space in the destination array to hold
+  * all the converted bytes, or an exception will be thrown.
+  *
+  * @param buf The byte array to convert
+  * @param buf_offset The index into the byte array to start converting from
+  * @param len The number of bytes to convert
+  * @param cbuf The char array to store converted characters into
+  * @param cbuf_offset The index into the char array to start storing converted characters.
+  *
+  * @return The char array passed by the caller as a param, now filled with converted characters.
+  *
+  * @exception ArrayIndexOutOfBoundsException If the destination char array is not big enough to hold all the converted characters
+  * @exception CharConversionException If an error occurs, such as an invalid byte in the source array.
+  */
+public abstract char[]
+convertToChars(byte[] buf, int buf_offset, int len, char[] cbuf,
+               int cbuf_offset) throws CharConversionException;
+
+/*************************************************************************/
+
+/**
+  * Closes this stream and the underlying <code>InputStream</code>
+  * 
+  * @exception IOException If an error occurs
+  */
+public void
+close() throws IOException
+{
+  in.close();
+}
+
+/*************************************************************************/
+
+/**
+  * This method returns <code>false</code> to indicate that there is no
+  * guarantee this stream can be read successfully without blocking.  This
+  * is because even if bytes are available from the underlying
+  * <code>InputStream</code>, this method does not know if a particular
+  * encoding requires more than that number of bytes or not.  Subclasses
+  * that can make that determination should override this method.
+  * 
+  * @return <code>false</code> since there is no guarantee this stream is ready to be read
+  *
+  * @exception IOException If an error occurs
+  */
+public boolean
+ready() throws IOException
+{
+  return(false);
+}
+
+} // class Decoder
+
===================================================================
Checking out kaffe/libraries/javalib/gnu/java/io/decode/KaffeDecoder.java
RCS:  /home/cvs/kaffe/kaffe/libraries/javalib/gnu/java/io/decode/KaffeDecoder.java,v
VERS: 1.1
***************
--- /dev/null	Sun Aug  4 19:57:58 2002
+++ kaffe/libraries/javalib/gnu/java/io/decode/KaffeDecoder.java	Mon Jan 19 16:29:50 2004
@@ -0,0 +1,164 @@
+/*
+ * Java core library component.
+ *
+ * Copyright (c) 2004
+ *	Ito Kazumitsu <kaz at maczuka.gcd.org>
+ *
+ * See the file "license.terms" for information on usage and redistribution
+ * of this file.
+ */
+
+package gnu.java.io.decode;
+
+import java.io.IOException;
+import java.io.InputStream;
+import kaffe.io.ByteToCharConverter;
+
+/**
+  * This class behaves as gnu.java.io.decode.Decoder
+  * wrapping Kaffe's kaffe.io.ByteToCharConverter.
+  *
+  * @version 0.0
+  *
+  * @author Ito Kazumitsu (kaz at maczuka.gcd.org)
+  */
+public class KaffeDecoder extends Decoder
+{
+
+/*************************************************************************/
+
+/*
+ * Class Variables
+ */
+ 
+/*************************************************************************/
+
+/*
+ * Constructors
+ */
+public
+KaffeDecoder(InputStream in, String enc)
+{
+  super(in);
+  try {
+      converter = ByteToCharConverter.getConverter(enc);
+  }
+  catch (java.io.UnsupportedEncodingException _) {
+      converter = null;
+  }
+}
+
+/*
+ * Instance Variables
+ */
+
+ByteToCharConverter converter;
+
+/*************************************************************************/
+
+/*
+ * Instance Methods
+ */
+
+/**
+  * This method is supposed to return the number of chars that can be
+  * converted out of the <code>len</code> bytes in the specified array
+  * starting at <code>offset</code>.  But it is often the case that
+  * it is impossible to calculate such number without actually executing
+  * the conversion, and the converter may return a wrong value.
+  * So, you should avoid using this method.
+  */
+
+public int
+charsInByteArray(byte[] buf, int offset, int len)
+{
+  return (converter.getNumberOfChars(buf, offset, len));
+}
+
+/**
+  * Convert the requested bytes to chars
+  */
+public char[]
+convertToChars(byte[] buf, int buf_offset, int len)
+{
+  char[] cbuf = new char[len];
+  int olen = converter.convert(buf, buf_offset, len,
+                    cbuf, 0, cbuf.length);
+  char[] ret = new char[olen];
+  System.arraycopy(cbuf, 0, ret, 0, olen);
+  return(ret);
+}
+
+/**
+  * Convert the requested bytes to chars
+  */
+public char[]
+convertToChars(byte[] buf, int buf_offset, int len,
+               char[] cbuf, int cbuf_offset)
+{
+  converter.convert(buf, buf_offset, len,
+                    cbuf, cbuf_offset, cbuf.length - cbuf_offset);
+  return(cbuf);
+}
+
+/**
+  * Read the requested number of chars from the underlying stream.
+  * Some byte fragments may remain in the converter and they are
+  * used by the following read.  So read and convertToChars must
+  * not be used for the same converter instance.
+  */
+// copied from kaffe's java/io/InputStreamReader.java
+public int
+read ( char cbuf[], int off, int len ) throws IOException
+{
+    if (len < 0 || off < 0 || off + len > cbuf.length) {
+            throw new IndexOutOfBoundsException();
+    }
+
+    int outlen = 0;
+    boolean seenEOF = false;
+
+    byte[] inbuf = new byte[2048];
+
+    while (len > outlen) {
+        // First we retreive anything left in the converter
+        int inpos = converter.withdraw(inbuf, 0, inbuf.length);
+        int n = len - outlen;
+        int m = inbuf.length - inpos;
+        if (n > m) {
+            n = m;
+        }
+        int inlen = in.read(inbuf, inpos, n);
+        if (inlen < 0) {
+            inlen = 0;
+            seenEOF = true;
+        }
+        outlen += converter.convert(inbuf, 0, inpos+inlen, cbuf,
+                           off+outlen, len-outlen);
+        if (inlen < n || !ready()) {
+            break;
+        }
+    }
+    if (seenEOF && !converter.havePending()) {
+        return (-1);
+    }
+    return (outlen);
+}
+
+
+/**
+  * Checks if bytes are available in the underlying inputstream. If this 
+  * is true at least one character can be read without blocking. 
+  * 
+  * @return <code>true</code> iff there are > 0 bytes available for
+  * the underlying InputStream.
+  *
+  * @exception IOException If an error occurs
+  */
+public boolean
+ready() throws IOException
+{
+  return (in.available() > 0);
+}
+
+} // class KaffeDecoder
===================================================================
Checking out kaffe/libraries/javalib/gnu/java/io/encode/Encoder.java
RCS:  /home/cvs/kaffe/kaffe/libraries/javalib/gnu/java/io/encode/Encoder.java,v
VERS: 1.1
***************
--- /dev/null	Sun Aug  4 19:57:58 2002
+++ kaffe/libraries/javalib/gnu/java/io/encode/Encoder.java	Mon Jan 19 16:29:50 2004
@@ -0,0 +1,340 @@
+/* Encoder.java -- Base class for char->byte encoders
+   Copyright (C) 1998 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.OutputStream;
+import java.io.Writer;
+import java.io.IOException;
+import java.io.CharConversionException;
+

*** Patch too long, truncated ***




More information about the kaffe mailing list