diff options
author | Andrew John Hughes <gnu_andrew@member.fsf.org> | 2008-12-23 02:04:53 +0000 |
---|---|---|
committer | Andrew John Hughes <gnu_andrew@member.fsf.org> | 2008-12-23 02:04:53 +0000 |
commit | f90629af6fef9c3e56ea8ae063cb288378093868 (patch) | |
tree | f96f309e77b0c30b45099661c78bb9082c683b1b | |
parent | 752e048c55929f4cbe57720c5063a63f26e21d0d (diff) | |
download | classpath-f90629af6fef9c3e56ea8ae063cb288378093868.tar.gz |
Add missing 1.6 String methods.
2008-12-22 Andrew John Hughes <gnu_andrew@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.
-rw-r--r-- | ChangeLog | 11 | ||||
-rw-r--r-- | java/lang/String.java | 123 |
2 files changed, 110 insertions, 24 deletions
@@ -1,3 +1,14 @@ +2008-12-22 Andrew John Hughes <gnu_andrew@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. + 2008-12-18 Andrew John Hughes <gnu_andrew@member.fsf.org> * native/jni/gtk-peer/gnu_java_awt_peer_gtk_FreetypeGlyphVector.c: diff --git a/java/lang/String.java b/java/lang/String.java index 95c88399d..c8c6cb962 100644 --- a/java/lang/String.java +++ b/java/lang/String.java @@ -337,9 +337,59 @@ public final class String * @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 @@ public final class String + (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 @@ public final class String 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 @@ public final class String } /** + * 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 @@ public final class String * @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 @@ public final class String 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. |