You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by te...@apache.org on 2007/06/13 16:10:42 UTC
svn commit: r546883 - in
/harmony/enhanced/classlib/branches/java6/modules/luni/src:
main/java/java/lang/String.java
test/java/org/apache/harmony/luni/tests/java/lang/StringTest.java
Author: tellison
Date: Wed Jun 13 07:10:41 2007
New Revision: 546883
URL: http://svn.apache.org/viewvc?view=rev&rev=546883
Log:
Apply patch HARMONY-4139 ([classlib][luni][java6] new methods isEmpty, getBytes in java.lang.String)
Modified:
harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/lang/String.java
harmony/enhanced/classlib/branches/java6/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/StringTest.java
Modified: harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/lang/String.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/lang/String.java?view=diff&rev=546883&r1=546882&r2=546883
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/lang/String.java (original)
+++ harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/lang/String.java Wed Jun 13 07:10:41 2007
@@ -983,6 +983,24 @@
}
return charset;
}
+
+ /**
+ * Converts this String to a byte encoding using the specified encoding.
+ *
+ * @param encoding
+ * the encoding
+ * @return the byte array encoding of this String
+ *
+ * @see String
+ * @since 1.6
+ */
+ public byte[] getBytes(Charset encoding) {
+ ByteBuffer buffer = encoding.encode(CharBuffer.wrap(this.value,
+ this.offset, this.count));
+ byte[] bytes = new byte[buffer.limit()];
+ buffer.get(bytes);
+ return bytes;
+ }
/**
* Copies the specified characters in this String to the character array
@@ -1302,6 +1320,16 @@
*/
public int length() {
return count;
+ }
+
+ /**
+ * Answers if the size of this String is zero.
+ *
+ * @return true if the size of this String is zero, false otherwise
+ * @since 1.6
+ */
+ public boolean isEmpty() {
+ return 0 == count;
}
/**
Modified: harmony/enhanced/classlib/branches/java6/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/StringTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/StringTest.java?view=diff&rev=546883&r1=546882&r2=546883
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/StringTest.java (original)
+++ harmony/enhanced/classlib/branches/java6/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/StringTest.java Wed Jun 13 07:10:41 2007
@@ -20,6 +20,7 @@
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Constructor;
import java.nio.charset.Charset;
+import java.util.SortedMap;
import junit.framework.TestCase;
@@ -710,5 +711,53 @@
// expected
}
new String(new byte[0], Charset.defaultCharset());
+ }
+
+ /**
+ * @tests {@link java.lang.String#isEmpty()}
+ *
+ * @since 1.6
+ */
+ public void test_isEmpty() throws Exception {
+ assertTrue(new String(new byte[0], Charset.defaultCharset()).isEmpty());
+ assertTrue(new String(new byte[8], Charset.defaultCharset()).substring(0, 0).isEmpty());
+ }
+
+ /**
+ * @tests {@link java.lang.String#getBytes(Charset)}
+ *
+ * @since 1.6
+ */
+ public void test_getBytesLCharset() throws Exception {
+ byte[] emptyBytes = new byte[0];
+ byte[] someBytes = new byte[]{'T','h','i','s',' ',' ','i','s',' ','t','e','s','t',' ','b','y','t','e','s'};
+ assertEquals(0, new String(emptyBytes, Charset.defaultCharset()).getBytes(Charset.defaultCharset()).length);
+ try{
+ new String(emptyBytes, Charset.defaultCharset()).getBytes((Charset)null);
+ fail("should throw NPE");
+ } catch (NullPointerException e){
+ // correct
+ }
+ assertTrue(bytesEquals(someBytes,new String(someBytes, Charset.defaultCharset()).getBytes(Charset.defaultCharset())));
+ SortedMap<String, Charset> charsets = Charset.availableCharsets();
+
+ Charset ascii = charsets.get("US-ASCII");
+ Charset utf8 = charsets.get("UTF-8");
+ if (charsets.size() >= 2){
+ assertTrue(bytesEquals(someBytes,new String(someBytes, charsets.get(charsets.firstKey())).getBytes(charsets.get(charsets.lastKey()))));
+ assertFalse(bytesEquals("\u4f60\u597d".getBytes(ascii), "\u4f60\u597d".getBytes(utf8)));
+ }
+ }
+
+ boolean bytesEquals(byte[] bytes1, byte[] bytes2){
+ if (bytes1.length == bytes2.length){
+ for (int i = 0; i < bytes1.length; i++){
+ if (bytes1[i] != bytes2[i]){
+ return false;
+ }
+ }
+ return true;
+ }
+ return false;
}
}
[classlib][luni] String modified
Posted by Tim Ellison <t....@gmail.com>.
FYI, String has got two new methods as required by Java 6 spec. These
don't change the shape of String instances so should be non-controvertial.
Regards,
Tim
tellison@apache.org wrote:
> Author: tellison
> Date: Wed Jun 13 07:10:41 2007
> New Revision: 546883
>
> URL: http://svn.apache.org/viewvc?view=rev&rev=546883
> Log:
> Apply patch HARMONY-4139 ([classlib][luni][java6] new methods isEmpty, getBytes in java.lang.String)
>
> Modified:
> harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/lang/String.java
> harmony/enhanced/classlib/branches/java6/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/StringTest.java
>
> Modified: harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/lang/String.java
> URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/lang/String.java?view=diff&rev=546883&r1=546882&r2=546883
> ==============================================================================
> --- harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/lang/String.java (original)
> +++ harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/lang/String.java Wed Jun 13 07:10:41 2007
> @@ -983,6 +983,24 @@
> }
> return charset;
> }
> +
> + /**
> + * Converts this String to a byte encoding using the specified encoding.
> + *
> + * @param encoding
> + * the encoding
> + * @return the byte array encoding of this String
> + *
> + * @see String
> + * @since 1.6
> + */
> + public byte[] getBytes(Charset encoding) {
> + ByteBuffer buffer = encoding.encode(CharBuffer.wrap(this.value,
> + this.offset, this.count));
> + byte[] bytes = new byte[buffer.limit()];
> + buffer.get(bytes);
> + return bytes;
> + }
>
> /**
> * Copies the specified characters in this String to the character array
> @@ -1302,6 +1320,16 @@
> */
> public int length() {
> return count;
> + }
> +
> + /**
> + * Answers if the size of this String is zero.
> + *
> + * @return true if the size of this String is zero, false otherwise
> + * @since 1.6
> + */
> + public boolean isEmpty() {
> + return 0 == count;
> }
>
> /**
>
> Modified: harmony/enhanced/classlib/branches/java6/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/StringTest.java
> URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/StringTest.java?view=diff&rev=546883&r1=546882&r2=546883
> ==============================================================================
> --- harmony/enhanced/classlib/branches/java6/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/StringTest.java (original)
> +++ harmony/enhanced/classlib/branches/java6/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/StringTest.java Wed Jun 13 07:10:41 2007
> @@ -20,6 +20,7 @@
> import java.io.UnsupportedEncodingException;
> import java.lang.reflect.Constructor;
> import java.nio.charset.Charset;
> +import java.util.SortedMap;
>
> import junit.framework.TestCase;
>
> @@ -710,5 +711,53 @@
> // expected
> }
> new String(new byte[0], Charset.defaultCharset());
> + }
> +
> + /**
> + * @tests {@link java.lang.String#isEmpty()}
> + *
> + * @since 1.6
> + */
> + public void test_isEmpty() throws Exception {
> + assertTrue(new String(new byte[0], Charset.defaultCharset()).isEmpty());
> + assertTrue(new String(new byte[8], Charset.defaultCharset()).substring(0, 0).isEmpty());
> + }
> +
> + /**
> + * @tests {@link java.lang.String#getBytes(Charset)}
> + *
> + * @since 1.6
> + */
> + public void test_getBytesLCharset() throws Exception {
> + byte[] emptyBytes = new byte[0];
> + byte[] someBytes = new byte[]{'T','h','i','s',' ',' ','i','s',' ','t','e','s','t',' ','b','y','t','e','s'};
> + assertEquals(0, new String(emptyBytes, Charset.defaultCharset()).getBytes(Charset.defaultCharset()).length);
> + try{
> + new String(emptyBytes, Charset.defaultCharset()).getBytes((Charset)null);
> + fail("should throw NPE");
> + } catch (NullPointerException e){
> + // correct
> + }
> + assertTrue(bytesEquals(someBytes,new String(someBytes, Charset.defaultCharset()).getBytes(Charset.defaultCharset())));
> + SortedMap<String, Charset> charsets = Charset.availableCharsets();
> +
> + Charset ascii = charsets.get("US-ASCII");
> + Charset utf8 = charsets.get("UTF-8");
> + if (charsets.size() >= 2){
> + assertTrue(bytesEquals(someBytes,new String(someBytes, charsets.get(charsets.firstKey())).getBytes(charsets.get(charsets.lastKey()))));
> + assertFalse(bytesEquals("\u4f60\u597d".getBytes(ascii), "\u4f60\u597d".getBytes(utf8)));
> + }
> + }
> +
> + boolean bytesEquals(byte[] bytes1, byte[] bytes2){
> + if (bytes1.length == bytes2.length){
> + for (int i = 0; i < bytes1.length; i++){
> + if (bytes1[i] != bytes2[i]){
> + return false;
> + }
> + }
> + return true;
> + }
> + return false;
> }
> }
>
>
>