harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From py...@apache.org
Subject svn commit: r430323 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/io/SequenceInputStream.java test/java/tests/api/java/io/SequenceInputStreamTest.java
Date Thu, 10 Aug 2006 09:01:47 GMT
Author: pyang
Date: Thu Aug 10 02:01:46 2006
New Revision: 430323

URL: http://svn.apache.org/viewvc?rev=430323&view=rev
Log:
Patch applied for HARMONY-1115([classlib][luni] java.io.SequenceInputStream 's constructor
SequenceInputStream(InputStream, InputStream) should not throw NullPointerException)

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

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/SequenceInputStream.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/SequenceInputStream.java?rev=430323&r1=430322&r2=430323&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/SequenceInputStream.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/SequenceInputStream.java
Thu Aug 10 02:01:46 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.
@@ -18,8 +18,6 @@
 import java.util.Enumeration;
 import java.util.Vector;
 
-import org.apache.harmony.luni.util.Msg;
-
 /**
  * SequenceInputStream is used for streaming over a sequence of streams
  * concatenated together. Reads are taken from the first stream until it ends,
@@ -48,14 +46,13 @@
 	 *            the second stream to get bytes from
 	 */
 	public SequenceInputStream(InputStream s1, InputStream s2) {
-		if (s1 != null && s2 != null) {
-			Vector<InputStream> inVector = new Vector<InputStream>(1);
-			inVector.addElement(s2);
-			e = inVector.elements();
-			in = s1;
-		} else {
-            throw new NullPointerException();
-        }
+		if (s1 == null) {
+			throw new NullPointerException();
+		}
+		Vector<InputStream> inVector = new Vector<InputStream>(1);
+		inVector.addElement(s2);
+		e = inVector.elements();
+		in = s1;
 	}
 
 	/**
@@ -101,10 +98,6 @@
 	 *             If an error occurs attempting to close this FileInputStream.
 	 */
 	public void close() throws IOException {
-		if (e == null) {
-			throw new IOException(Msg.getString("K00b7")); //$NON-NLS-1$
-		}
-
 		while (in != null) {
 			nextStream();
 		}
@@ -167,8 +160,7 @@
 	 * @return the number of bytes actually read or -1 if end of stream.
 	 * 
 	 * @throws IOException
-	 *             If the stream is already closed or another IOException
-	 *             occurs.
+	 *             If an error occurs while reading the stream
 	 */
 	public int read(byte[] buffer, int offset, int count) throws IOException {
 		if (in == null) {
@@ -178,18 +170,16 @@
 			throw new NullPointerException();
 		}
 		// avoid int overflow
-		if (0 <= offset && offset <= buffer.length && 0 <= count
-				&& count <= buffer.length - offset) {
-			while (in != null) {
-				int result = in.read(buffer, offset, count);
-				if (result >= 0) {
-					return result;
-				}
-				nextStream();
+		if (offset < 0 || offset > buffer.length - count || count < 0) {
+			throw new IndexOutOfBoundsException();
+		}
+		while (in != null) {
+			int result = in.read(buffer, offset, count);
+			if (result >= 0) {
+				return result;
 			}
-			return -1;
-		} else {
-			throw new ArrayIndexOutOfBoundsException();
+			nextStream();
 		}
+		return -1;
 	}
 }

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/SequenceInputStreamTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/SequenceInputStreamTest.java?rev=430323&r1=430322&r2=430323&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/SequenceInputStreamTest.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/SequenceInputStreamTest.java
Thu Aug 10 02:01:46 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.
@@ -38,6 +38,23 @@
 		// java.io.InputStream)
 		// Used in tests
 	}
+	
+	/**
+	 * @tests SequenceInputStream#SequenceInputStream(java.io.InputStream,
+	 *        java.io.InputStream)
+	 */
+	public void test_Constructor_LInputStreamLInputStream_Null() {		
+		try {
+			si = new SequenceInputStream(null , null);
+			fail("should throw NullPointerException");
+		} catch (NullPointerException e) {
+			//expected
+		}
+		
+		//will not throw NullPointerException if the first InputStream is not null
+		InputStream is = new ByteArrayInputStream(s1.getBytes()); 
+		si = new SequenceInputStream(is , null);
+	}
 
 	/**
 	 * @tests java.io.SequenceInputStream#SequenceInputStream(java.util.Enumeration)
@@ -93,20 +110,16 @@
 	/**
 	 * @tests java.io.SequenceInputStream#close()
 	 */
-	public void test_close() {
-		// Test for method void java.io.SequenceInputStream.close()
-		try {
-			si.close();
-		} catch (IOException e) {
-			fail("IOException during close test : " + e.getMessage());
-		}
-
+	public void test_close() throws IOException {
+		si.close();		
+		//will not throw IOException to close a stream which is closed already
+		si.close();
 	}
 
 	/**
 	 * @tests java.io.SequenceInputStream#read()
 	 */
-	public void test_read() {
+	public void test_read() throws IOException {
 		// Test for method int java.io.SequenceInputStream.read()
 		try {
 			si.read();
@@ -114,6 +127,11 @@
 		} catch (IOException e) {
 			fail("IOException during read test: " + e.getMessage());
 		}
+		
+		//returns -1 if the stream is closed , do not throw IOException
+		si.close();
+		int result = si.read();
+		assertEquals(-1 , result);		
 	}
 
 	/**
@@ -144,6 +162,13 @@
 		} catch (NullPointerException e) {
 			// expected
 		}
+		
+        //returns -1 if the stream is closed , do not throw IOException
+		byte[] array = new byte[] { 1 , 2 , 3 ,4 };
+		sis.close();
+		int result = sis.read(array , 0 , 5);
+		assertEquals(-1 , result);	
+		
 	}
 
 	/**



Mime
View raw message