This adds a few simple methods that were added to String in 1.6.

ChangeLog:

2008-12-22  Andrew John Hughes  <gnu_and...@member.fsf.org>

        * java/lang/String.java:
        (byte[],int,int,String): Call new Charset method.
        (stringToCharset(String)): Private method added to
        handle exception conversion.
        (byte[],int,int,Charset): Implemented.
        (byte[], Charset): Likewise.
        (getBytes(String)): Call new Charset method.
        (getBytes(Charset)): Implemented.

-- 
Andrew :)

Support Free Java!
Contribute to GNU Classpath and the OpenJDK
http://www.gnu.org/software/classpath
http://openjdk.java.net
PGP Key: 94EFD9D8 (http://subkeys.pgp.net)
Fingerprint = F8EF F1EA 401E 2E60 15FA  7927 142C 2591 94EF D9D8
Index: java/lang/String.java
===================================================================
RCS file: /sources/classpath/classpath/java/lang/String.java,v
retrieving revision 1.90
diff -u -u -r1.90 String.java
--- java/lang/String.java       6 May 2008 02:42:05 -0000       1.90
+++ java/lang/String.java       23 Dec 2008 01:34:04 -0000
@@ -337,9 +337,59 @@
    * @throws Error if the decoding fails
    * @since 1.1
    */
-  public String(byte[] data, int offset, int count, String encoding)
+  public String(byte[] data, int offset, int count, final String encoding)
     throws UnsupportedEncodingException
   {
+    this(data, offset, count, stringToCharset(encoding));
+  }
+
+  /**
+   * Wrapper method to convert exceptions resulting from
+   * the selection of a {...@link java.nio.charset.Charset} based on
+   * a String.
+   *
+   * @throws UnsupportedEncodingException if encoding is not found
+   */
+  private static final Charset stringToCharset(final String encoding)
+    throws UnsupportedEncodingException
+  {
+    try
+      {
+       return Charset.forName(encoding);
+      }
+    catch(IllegalCharsetNameException e)
+      {
+       throw new UnsupportedEncodingException("Encoding: "+encoding+
+                                              " not found.");
+      }
+    catch(UnsupportedCharsetException e)
+      {
+       throw new UnsupportedEncodingException("Encoding: "+encoding+
+                                              " not found.");
+      }    
+  }
+
+  /**
+   * Creates a new String using the portion of the byte array starting at the
+   * offset and ending at offset + count. Uses the specified encoding type
+   * to decode the byte array, so the resulting string may be longer or
+   * shorter than the byte array. For more decoding control, use
+   * {...@link java.nio.charset.CharsetDecoder}, and for valid character sets,
+   * see {...@link java.nio.charset.Charset}. Malformed input and unmappable
+   * character sequences are replaced with the default replacement string
+   * provided by the {...@link java.nio.charset.Charset}.
+   *
+   * @param data byte array to copy
+   * @param offset the offset to start at
+   * @param count the number of bytes in the array to use
+   * @param encoding the encoding to use
+   * @throws NullPointerException if data or encoding is null
+   * @throws IndexOutOfBoundsException if offset or count is incorrect
+   *         (while unspecified, this is a StringIndexOutOfBoundsException)
+   * @since 1.6
+   */
+  public String(byte[] data, int offset, int count, Charset encoding)
+  {
     if (offset < 0)
       throw new StringIndexOutOfBoundsException("offset: " + offset);
     if (count < 0)
@@ -350,7 +400,7 @@
                                                + (offset + count));
     try 
       {
-        CharsetDecoder csd = Charset.forName(encoding).newDecoder();
+        CharsetDecoder csd = encoding.newDecoder();
        csd.onMalformedInput(CodingErrorAction.REPLACE);
        csd.onUnmappableCharacter(CodingErrorAction.REPLACE);
        CharBuffer cbuf = csd.decode(ByteBuffer.wrap(data, offset, count));
@@ -366,16 +416,12 @@
            this.offset = 0;
            this.count = value.length;
          }
-      } catch(CharacterCodingException e){
-         throw new UnsupportedEncodingException("Encoding: "+encoding+
-                                                " not found.");          
-      } catch(IllegalCharsetNameException e){
-         throw new UnsupportedEncodingException("Encoding: "+encoding+
-                                                " not found.");
-      } catch(UnsupportedCharsetException e){
-         throw new UnsupportedEncodingException("Encoding: "+encoding+
-                                                " not found.");
-      }    
+      } 
+    catch(CharacterCodingException e)
+      {
+       // This shouldn't ever happen.
+       throw (InternalError) new InternalError().initCause(e);
+      }          
   }
 
   /**
@@ -402,6 +448,26 @@
   }
 
   /**
+   * Creates a new String using the byte array. Uses the specified encoding
+   * type to decode the byte array, so the resulting string may be longer or
+   * shorter than the byte array. For more decoding control, use
+   * {...@link java.nio.charset.CharsetDecoder}, and for valid character sets,
+   * see {...@link java.nio.charset.Charset}. Malformed input and unmappable
+   * character sequences are replaced with the default replacement string
+   * provided by the {...@link java.nio.charset.Charset}.
+   *
+   * @param data byte array to copy
+   * @param encoding the name of the encoding to use
+   * @throws NullPointerException if data or encoding is null
+   * @see #String(byte[], int, int, java.nio.Charset)
+   * @since 1.6
+   */
+  public String(byte[] data, Charset encoding)
+  {
+    this(data, 0, data.length, encoding);
+  }
+
+  /**
    * Creates a new String using the portion of the byte array starting at the
    * offset and ending at offset + count. Uses the encoding of the platform's
    * default charset, so the resulting string may be longer or shorter than
@@ -726,11 +792,30 @@
    * @throws UnsupportedEncodingException if encoding is not supported
    * @since 1.1
    */
-  public byte[] getBytes(String enc) throws UnsupportedEncodingException
+  public byte[] getBytes(final String enc)
+    throws UnsupportedEncodingException
+  {
+    return getBytes(stringToCharset(enc));
+  }
+
+  /**
+   * Converts the Unicode characters in this String to a byte array. Uses the
+   * specified encoding method, so the result may be longer or shorter than
+   * the String. For more encoding control, use
+   * {...@link java.nio.charset.CharsetEncoder}, and for valid character sets,
+   * see {...@link java.nio.charset.Charset}. Unsupported characters get
+   * replaced by the {...@link java.nio.charset.Charset}'s default replacement.
+   *
+   * @param enc encoding name
+   * @return the resulting byte array
+   * @throws NullPointerException if enc is null
+   * @since 1.6
+   */
+  public byte[] getBytes(Charset enc)
   {
     try 
       {
-       CharsetEncoder cse = Charset.forName(enc).newEncoder();
+       CharsetEncoder cse = enc.newEncoder();
        cse.onMalformedInput(CodingErrorAction.REPLACE);
        cse.onUnmappableCharacter(CodingErrorAction.REPLACE);
        ByteBuffer bbuf = cse.encode(CharBuffer.wrap(value, offset, count));
@@ -742,16 +827,6 @@
        bbuf.get(bytes);
        return bytes;
       } 
-    catch(IllegalCharsetNameException e)
-      {
-       throw new UnsupportedEncodingException("Encoding: " + enc
-                                              + " not found.");
-      } 
-    catch(UnsupportedCharsetException e)
-      {
-       throw new UnsupportedEncodingException("Encoding: " + enc
-                                              + " not found.");
-      } 
     catch(CharacterCodingException e)
       {
        // This shouldn't ever happen.

Reply via email to