harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r371906 - in /incubator/harmony/enhanced/classlib/trunk/modules/nio/src: main/java/java/nio/CharBuffer.java main/java/java/nio/CharSequenceAdapter.java test/java/org/apache/harmony/tests/java/nio/CharBufferTest.java
Date Tue, 24 Jan 2006 12:44:13 GMT
Author: tellison
Date: Tue Jan 24 04:44:05 2006
New Revision: 371906

URL: http://svn.apache.org/viewcvs?rev=371906&view=rev
Log:
Fix for HARMONY-36
 - Implementation of java.lang.Appendable and java.lang.Readable
 - Update declaration of NIO CharBuffer and CharSequenceAdapter
 - Update NIO tests

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/CharBuffer.java
    incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/CharSequenceAdapter.java
    incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/CharBufferTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/CharBuffer.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/CharBuffer.java?rev=371906&r1=371905&r2=371906&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/CharBuffer.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/CharBuffer.java
Tue Jan 24 04:44:05 2006
@@ -15,6 +15,8 @@
 
 package java.nio;
 
+import java.io.IOException;
+
 
 
 /**
@@ -35,7 +37,7 @@
  * 
  */
 public abstract class CharBuffer extends Buffer implements Comparable,
-		CharSequence {
+		CharSequence, Appendable, Readable {
 
 	/**
 	 * Creates a char buffer based on a new allocated char array.
@@ -741,4 +743,52 @@
 		}
 		return strbuf.toString();
 	}
+    
+    /**
+     * @see Appendable#append(char)
+     */
+    public Appendable append(char c){
+        return put(c);
+    }
+
+    /**
+     * @see Appendable#append(CharSequence)
+     */
+    public Appendable append(CharSequence csq){
+        if (csq != null) {
+            return put(csq.toString());
+        }
+        return put("null"); //$NON-NLS-1$
+    }
+
+    /**
+     * @see Appendable#append(CharSequence, int, int)
+     */
+    public Appendable append(CharSequence csq, int start, int end){
+        if (csq == null) {
+            csq = "null"; //$NON-NLS-1$
+        }
+        CharSequence cs = csq.subSequence(start, end);
+        if (cs.length() > 0) {
+            return put(cs.toString());
+        }
+        return this;
+    }
+
+    /**
+     * @see Readable#read(CharBuffer)
+     */
+    public int read(CharBuffer target) throws IOException {
+        if(target == this){
+            throw new IllegalArgumentException();
+        }
+        if (remaining() == 0) {
+            return target.remaining()==0?0:-1;
+        }
+        int result = Math.min(target.remaining(), remaining());
+        char[] chars = new char[result];
+        get(chars);
+        target.put(chars);
+        return result;
+    }    
 }

Modified: incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/CharSequenceAdapter.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/CharSequenceAdapter.java?rev=371906&r1=371905&r2=371906&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/CharSequenceAdapter.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/CharSequenceAdapter.java
Tue Jan 24 04:44:05 2006
@@ -102,6 +102,14 @@
 		throw new ReadOnlyBufferException();
 	}
 
+    public final CharBuffer put(char[] src, int off, int len) {
+        throw new ReadOnlyBufferException();
+    }
+
+    public CharBuffer put(String src, int start, int end) {
+        throw new ReadOnlyBufferException();
+    }  
+
 	public CharBuffer slice() {
 		return new CharSequenceAdapter(sequence.subSequence(position, limit));
 	}

Modified: incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/CharBufferTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/CharBufferTest.java?rev=371906&r1=371905&r2=371906&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/CharBufferTest.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/CharBufferTest.java
Tue Jan 24 04:44:05 2006
@@ -1040,4 +1040,189 @@
 		slice.limit(10).position(1);
 		testCharBufferInstance(slice.slice());
 	}
+
+	public void testAppendSelf() throws Exception{
+        CharBuffer cb = CharBuffer.allocate(10);
+        CharBuffer cb2 = cb.duplicate();
+        cb.append(cb);
+        assertEquals(10, cb.position());
+        cb.clear();
+        assertEquals(cb2, cb);
+        
+        cb.put("abc");
+        cb2 = cb.duplicate();
+        cb.append(cb);
+        assertEquals(10, cb.position());
+        cb.clear();
+        cb2.clear();
+        assertEquals(cb2, cb);
+        
+        cb.put("edfg");
+        cb.clear();
+        cb2 = cb.duplicate();
+        cb.append(cb);
+        assertEquals(10, cb.position());
+        cb.clear();
+        cb2.clear();
+        assertEquals(cb, cb2);
+    }
+    
+	public void testAppendOverFlow() throws IOException {
+		CharBuffer cb = CharBuffer.allocate(1);
+		CharSequence cs = "String";
+		cb.put('A');
+		try {
+			cb.append('C');
+			fail("should throw BufferOverflowException.");
+		} catch (BufferOverflowException ex) {
+			// expected;
+		}
+		try {
+			cb.append(cs);
+			fail("should throw BufferOverflowException.");
+		} catch (BufferOverflowException ex) {
+			// expected;
+		}
+		try {
+			cb.append(cs, 1, 2);
+			fail("should throw BufferOverflowException.");
+		} catch (BufferOverflowException ex) {
+			// expected;
+		}
+	}
+
+	public void testReadOnlyMap() throws IOException {
+		CharBuffer cb = CharBuffer.wrap("ABCDE").asReadOnlyBuffer();
+		CharSequence cs = "String";
+		try {
+			cb.append('A');
+			fail("should throw ReadOnlyBufferException.");
+		} catch (ReadOnlyBufferException ex) {
+			// expected;
+		}
+		try {
+			cb.append(cs);
+			fail("should throw ReadOnlyBufferException.");
+		} catch (ReadOnlyBufferException ex) {
+			// expected;
+		}
+		try {
+			cb.append(cs, 1, 2);
+			fail("should throw ReadOnlyBufferException.");
+		} catch (ReadOnlyBufferException ex) {
+			// expected;
+		}
+		cb.append(cs, 1, 1);
+	}
+
+	public void testAppendCNormal() throws IOException {
+		CharBuffer cb = CharBuffer.allocate(2);
+		cb.put('A');
+		assertSame(cb, cb.append('B'));
+		assertEquals('B', cb.get(1));
+	}
+
+	public void testAppendCharSequenceNormal() throws IOException {
+		CharBuffer cb = CharBuffer.allocate(10);
+		cb.put('A');
+		assertSame(cb, cb.append("String"));
+		assertEquals("AString", cb.flip().toString());
+		cb.append(null);
+		assertEquals("null", cb.flip().toString());
+	}
+
+	public void testAppendCharSequenceIINormal() throws IOException {
+		CharBuffer cb = CharBuffer.allocate(10);
+		cb.put('A');
+		assertSame(cb, cb.append("String", 1, 3));
+		assertEquals("Atr", cb.flip().toString());
+
+		cb.append(null, 0, 1);
+		assertEquals("n", cb.flip().toString());
+	}
+
+	public void testAppendCharSequenceII_IllegalArgument() throws IOException {
+		CharBuffer cb = CharBuffer.allocate(10);
+		cb.append("String", 0, 0);
+		cb.append("String", 2, 2);
+		try {
+			cb.append("String", -1, 1);
+			fail("should throw IndexOutOfBoundsException.");
+		} catch (IndexOutOfBoundsException ex) {
+			// expected;
+		}
+		try {
+			cb.append("String", -1, -1);
+			fail("should throw IndexOutOfBoundsException.");
+		} catch (IndexOutOfBoundsException ex) {
+			// expected;
+		}
+		try {
+			cb.append("String", 3, 2);
+			fail("should throw IndexOutOfBoundsException.");
+		} catch (IndexOutOfBoundsException ex) {
+			// expected;
+		}
+		try {
+			cb.append("String", 3, 0);
+			fail("should throw IndexOutOfBoundsException.");
+		} catch (IndexOutOfBoundsException ex) {
+			// expected;
+		}
+		try {
+			cb.append("String", 3, 110);
+			fail("should throw IndexOutOfBoundsException.");
+		} catch (IndexOutOfBoundsException ex) {
+			// expected;
+		}
+	}
+
+	public void testReadCharBuffer() throws IOException {
+		// happy path
+		CharBuffer source = CharBuffer.wrap("String");
+		CharBuffer target = CharBuffer.allocate(10);
+		assertEquals(6, source.read(target));
+		assertEquals("String", target.flip().toString());
+		// return -1 when nothing to read
+		assertEquals(-1, source.read(target));
+		// npe
+		try {
+			assertEquals(-1, source.read(null));
+			fail("should throw NPE.");
+		} catch (NullPointerException ex) {
+			// expected;
+		}
+
+	}
+
+	public void testReadReadOnly() throws IOException {
+		CharBuffer source = CharBuffer.wrap("String");
+		CharBuffer target = CharBuffer.allocate(10).asReadOnlyBuffer();
+		try {
+			source.read(target);
+			fail("should throw ReadOnlyBufferException.");
+		} catch (ReadOnlyBufferException ex) {
+			// expected;
+		}
+		// if target has no remaining, needn't to check the isReadOnly
+		target.flip();
+		assertEquals(0, source.read(target));
+	}
+
+	public void testReadOverflow() throws IOException {
+		CharBuffer source = CharBuffer.wrap("String");
+		CharBuffer target = CharBuffer.allocate(1);
+		assertEquals(1, source.read(target));
+		assertEquals("S", target.flip().toString());
+		assertEquals(1, source.position());
+	}
+    
+    public void testReadSelf() throws Exception{
+        CharBuffer source = CharBuffer.wrap("abuffer");
+        try {
+            source.read(source);
+            fail("should throw IAE.");
+        } catch (IllegalArgumentException e) {
+        }
+    }
 }



Mime
View raw message