You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by hi...@apache.org on 2006/08/17 15:13:13 UTC

svn commit: r432226 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/io/BufferedOutputStream.java test/java/tests/api/java/io/BufferedOutputStreamTest.java

Author: hindessm
Date: Thu Aug 17 06:13:12 2006
New Revision: 432226

URL: http://svn.apache.org/viewvc?rev=432226&view=rev
Log:
Applied patch from "[#HARMONY-1213] [classlib][luni] tests.api.java.io.BufferedOutputStreamTest.test_write$BII should not depend on the default buf size".

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedOutputStream.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/BufferedOutputStreamTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedOutputStream.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedOutputStream.java?rev=432226&r1=432225&r2=432226&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedOutputStream.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedOutputStream.java Thu Aug 17 06:13:12 2006
@@ -1,4 +1,4 @@
-/* Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable
+/* Copyright 1998, 2006 The Apache Software Foundation or its licensors, as applicable
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -60,6 +60,8 @@
 	 *            the OutputStream to buffer writes on.
 	 * @param size
 	 *            the size of the buffer in bytes.
+	 * @throws IllegalArgumentException
+	 *             the size is <= 0
 	 * 
 	 */
 	public BufferedOutputStream(OutputStream out, int size) {
@@ -109,49 +111,46 @@
 	 *             BufferedOutputStream.
 	 * @throws NullPointerException
 	 *             If buffer is null.
-	 * @throws IndexOutOfBoundsException
-	 *             If offset or count are outside of bounds.
+	 * @throws ArrayIndexOutOfBoundsException
+	 *             If offset or count is outside of bounds.
 	 */
 	@Override
-    public synchronized void write(byte[] buffer, int offset, int length)
+	public synchronized void write(byte[] buffer, int offset, int length)
 			throws IOException {
-		if (buffer != null) {
-			// avoid int overflow
-			if (0 <= offset && offset <= buffer.length && 0 <= length
-					&& length <= buffer.length - offset) {
-				if (count == 0 && length >= buf.length) {
-					out.write(buffer, offset, length);
-					return;
-				}
-				int available = buf.length - count;
-				if (length < available) {
-                    available = length;
-                }
-				if (available > 0) {
+		if (buffer == null) {
+			throw new NullPointerException(org.apache.harmony.luni.util.Msg
+					.getString("K0047")); //$NON-NLS-1$
+		}
+		if (offset < 0 || offset > buffer.length - length || length < 0) {
+			throw new ArrayIndexOutOfBoundsException(
+					org.apache.harmony.luni.util.Msg.getString("K002f")); //$NON-NLS-1$
+		}
+		if (count == 0 && length >= buf.length) {
+			out.write(buffer, offset, length);
+			return;
+		}
+		int available = buf.length - count;
+		if (length < available) {
+			available = length;
+		}
+		if (available > 0) {
+			System.arraycopy(buffer, offset, buf, count, available);
+			count += available;
+		}
+		if (count == buf.length) {
+			out.write(buf, 0, buf.length);
+			count = 0;
+			if (length > available) {
+				offset += available;
+				available = length - available;
+				if (available >= buf.length) {
+					out.write(buffer, offset, available);
+				} else {
 					System.arraycopy(buffer, offset, buf, count, available);
 					count += available;
 				}
-				if (count == buf.length) {
-					out.write(buf, 0, buf.length);
-					count = 0;
-					if (length > available) {
-						offset += available;
-						available = length - available;
-						if (available >= buf.length) {
-							out.write(buffer, offset, available);
-						} else {
-							System.arraycopy(buffer, offset, buf, count,
-									available);
-							count += available;
-						}
-					}
-				}
-			} else {
-                throw new ArrayIndexOutOfBoundsException(Msg.getString("K002f")); //$NON-NLS-1$
-            }
-		} else {
-            throw new NullPointerException(Msg.getString("K0047")); //$NON-NLS-1$
-        }
+			}
+		}
 	}
 
 	/**

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/BufferedOutputStreamTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/BufferedOutputStreamTest.java?rev=432226&r1=432225&r2=432226&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/BufferedOutputStreamTest.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/BufferedOutputStreamTest.java Thu Aug 17 06:13:12 2006
@@ -1,4 +1,4 @@
-/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+/* Copyright 1998, 2006 The Apache Software Foundation or its licensors, as applicable
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -15,7 +15,10 @@
 
 package tests.api.java.io;
 
+import java.io.BufferedOutputStream;
 import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
 
 public class BufferedOutputStreamTest extends junit.framework.TestCase {
 
@@ -85,7 +88,7 @@
 		// int)
 		try {
 			os = new java.io.BufferedOutputStream(
-					baos = new java.io.ByteArrayOutputStream());
+					baos = new java.io.ByteArrayOutputStream(),512);
 			os.write(fileString.getBytes(), 0, 500);
 			bais = new java.io.ByteArrayInputStream(baos.toByteArray());
 			assertEquals("Bytes written, not buffered", 0, bais.available());
@@ -104,6 +107,146 @@
 			fail("Flush test failed");
 		}
 
+	}
+	
+	/**
+	 * @tests java.io.BufferedOutputStream#write(byte[], int, int)
+	 */
+	public void test_write_$BII_Exception() throws IOException {
+		OutputStream bos = new BufferedOutputStream(new ByteArrayOutputStream());	
+		byte[] nullByteArray = null;
+		byte[] byteArray = new byte[10];
+		
+		try {
+			bos.write(nullByteArray, -1, -1);
+			fail("should throw NullPointerException");
+		} catch (NullPointerException e) {
+			// expected
+		}
+
+		try {
+			bos.write(nullByteArray, -1, 0);
+			fail("should throw NullPointerException");
+		} catch (NullPointerException e) {
+			// expected
+		}
+		
+		try {
+			bos.write(nullByteArray, -1, 1);
+			fail("should throw NullPointerException");
+		} catch (NullPointerException e) {
+			// expected
+		}
+
+		try {
+			bos.write(nullByteArray, 0, -1);
+			fail("should throw NullPointerException");
+		} catch (NullPointerException e) {
+			// expected
+		}
+
+		try {
+			bos.write(nullByteArray, 0, 0);
+			fail("should throw NullPointerException");
+		} catch (NullPointerException e) {
+			// expected
+		}
+		
+		try {
+			bos.write(nullByteArray, 0, 1);
+			fail("should throw NullPointerException");
+		} catch (NullPointerException e) {
+			// expected
+		}
+		
+		try {
+			bos.write(nullByteArray, 1, -1);
+			fail("should throw NullPointerException");
+		} catch (NullPointerException e) {
+			// expected
+		}
+
+		try {
+			bos.write(nullByteArray, 1, 0);
+			fail("should throw NullPointerException");
+		} catch (NullPointerException e) {
+			// expected
+		}
+		
+		try {
+			bos.write(nullByteArray, 1, 1);
+			fail("should throw NullPointerException");
+		} catch (NullPointerException e) {
+			// expected
+		}
+		
+		try {
+			bos.write(byteArray, -1, -1);
+			fail("should throw ArrayIndexOutOfBoundsException");
+		} catch (ArrayIndexOutOfBoundsException e) {
+			// expected
+		}
+
+		try {
+			bos.write(byteArray, -1, 0);
+			fail("should throw ArrayIndexOutOfBoundsException");
+		} catch (ArrayIndexOutOfBoundsException e) {
+			// expected
+		}
+		
+		try {
+			bos.write(byteArray, -1, 1);
+			fail("should throw ArrayIndexOutOfBoundsException");
+		} catch (ArrayIndexOutOfBoundsException e) {
+			// expected
+		}
+
+		try {
+			bos.write(byteArray, 0, -1);
+			fail("should throw ArrayIndexOutOfBoundsException");
+		} catch (ArrayIndexOutOfBoundsException e) {
+			// expected
+		}
+
+		bos.write(byteArray, 0, 0);
+        bos.write(byteArray, 0, 1);
+        bos.write(byteArray, 0, byteArray.length);
+			
+		try {
+			bos.write(byteArray, 1, -1);
+			fail("should throw ArrayIndexOutOfBoundsException");
+		} catch (ArrayIndexOutOfBoundsException e) {
+			// expected
+		}
+
+		bos.write(byteArray, 1, 0);
+		bos.write(byteArray, 1, 1);
+
+
+		bos.write(byteArray, byteArray.length, 0);
+		
+		try {
+			bos.write(byteArray, byteArray.length + 1, 0);
+			fail("should throw ArrayIndexOutOfBoundsException");
+		} catch (ArrayIndexOutOfBoundsException e) {
+			//expected
+		}
+		
+		try {
+			bos.write(byteArray, byteArray.length + 1, 1);
+			fail("should throw ArrayIndexOutOfBoundsException");
+		} catch (ArrayIndexOutOfBoundsException e) {
+			//expected
+		}
+
+		bos.close();
+
+		try {
+			bos.write(byteArray, -1, -1);
+			fail("should throw ArrayIndexOutOfBoundsException");
+		} catch (ArrayIndexOutOfBoundsException e) {
+			//expected
+		}
 	}
 
 	/**