harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r381989 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/io/ test/java/org/apache/harmony/tests/java/io/
Date Wed, 01 Mar 2006 11:25:11 GMT
Author: tellison
Date: Wed Mar  1 03:24:55 2006
New Revision: 381989

URL: http://svn.apache.org/viewcvs?rev=381989&view=rev
Log:
- Read at end of stream should not invalidate mark
- Setting large mark value causes out of memory error

Added:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/io/BufferedInputStreamTest.java
Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedInputStream.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedReader.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/io/AllTests.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/io/BufferedReaderTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedInputStream.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedInputStream.java?rev=381989&r1=381988&r2=381989&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedInputStream.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedInputStream.java
Wed Mar  1 03:24:55 2006
@@ -117,15 +117,20 @@
 		if (markpos == -1 || (pos - markpos >= marklimit)) {
 			/* Mark position not set or exceeded readlimit */
 			int result = in.read(buf);
-			markpos = -1;
-			pos = 0;
-			count = result == -1 ? 0 : result;
+			if (result > 0) {
+				markpos = -1;
+				pos = 0;
+				count = result == -1 ? 0 : result;
+			}
 			return result;
 		}
-		if (marklimit > buf.length) {
+		if (markpos == 0 && marklimit > buf.length) {
 			/* Increase buffer size to accomodate the readlimit */
-			byte[] newbuf = new byte[marklimit];
-			System.arraycopy(buf, markpos, newbuf, 0, buf.length - markpos);
+			int newLength = buf.length * 2;
+			if (newLength > marklimit)
+				newLength = marklimit;
+			byte[] newbuf = new byte[newLength];
+			System.arraycopy(buf, 0, newbuf, 0, buf.length);
 			buf = newbuf;
 		} else if (markpos > 0) {
 			System.arraycopy(buf, markpos, buf, 0, buf.length - markpos);

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedReader.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedReader.java?rev=381989&r1=381988&r2=381989&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedReader.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedReader.java
Wed Mar  1 03:24:55 2006
@@ -105,19 +105,25 @@
 		if (markpos == -1 || (pos - markpos >= marklimit)) {
 			/* Mark position not set or exceeded readlimit */
 			int result = in.read(buf, 0, buf.length);
-			markpos = -1;
-			pos = 0;
-			count = result == -1 ? 0 : result;
+			if (result > 0) {
+				markpos = -1;
+				pos = 0;
+				count = result == -1 ? 0 : result;
+			}
 			return result;
 		}
-		if (marklimit > buf.length) {
+		if (markpos == 0 && marklimit > buf.length) {
 			/* Increase buffer size to accomodate the readlimit */
-			char[] newbuf = new char[marklimit];
-			System.arraycopy(buf, markpos, newbuf, 0, buf.length - markpos);
+			int newLength = buf.length * 2;
+			if (newLength > marklimit)
+				newLength = marklimit;
+			char[] newbuf = new char[newLength];
+			System.arraycopy(buf, 0, newbuf, 0, buf.length);
 			buf = newbuf;
 		} else if (markpos > 0) {
 			System.arraycopy(buf, markpos, buf, 0, buf.length - markpos);
 		}
+
 		/* Set the new position and mark position */
 		pos -= markpos;
 		count = markpos = 0;

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/io/AllTests.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/io/AllTests.java?rev=381989&r1=381988&r2=381989&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/io/AllTests.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/io/AllTests.java
Wed Mar  1 03:24:55 2006
@@ -30,6 +30,7 @@
 		//$JUnit-BEGIN$
 		suite.addTestSuite(WriterTest.class);
 		suite.addTestSuite(FilePermissionTest.class);
+		suite.addTestSuite(BufferedInputStreamTest.class);
 		suite.addTestSuite(FileTest.class);
 		suite.addTestSuite(BufferedReaderTest.class);
 		suite.addTestSuite(PushBackInputStreamTest.class);

Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/io/BufferedInputStreamTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/io/BufferedInputStreamTest.java?rev=381989&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/io/BufferedInputStreamTest.java
(added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/io/BufferedInputStreamTest.java
Wed Mar  1 03:24:55 2006
@@ -0,0 +1,60 @@
+/* Copyright 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.
+ * You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.harmony.tests.java.io;
+
+import java.io.BufferedInputStream;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+
+import junit.framework.TestCase;
+
+public class BufferedInputStreamTest extends TestCase {
+
+	/**
+	 * @tests java.io.BufferedInputStream#mark(int)
+	 */
+	public void test_markI() throws IOException {
+		BufferedInputStream buf = new BufferedInputStream(
+				new ByteArrayInputStream(new byte[] { 0, 1, 2, 3, 4 }), 2);
+		buf.mark(3);
+		byte[] bytes = new byte[3];
+		int result = buf.read(bytes);
+		assertEquals(3, result);
+		assertEquals("Assert 0:", 0, bytes[0]);
+		assertEquals("Assert 1:", 1, bytes[1]);
+		assertEquals("Assert 2:", 2, bytes[2]);
+		assertEquals("Assert 3:", 3, buf.read());
+
+		buf = new BufferedInputStream(
+				new ByteArrayInputStream(new byte[] { 0, 1, 2, 3, 4 }), 2);
+		buf.mark(3);
+		bytes = new byte[4];
+		result = buf.read(bytes);
+		assertEquals(4, result);
+		assertEquals("Assert 4:", 0, bytes[0]);
+		assertEquals("Assert 5:", 1, bytes[1]);
+		assertEquals("Assert 6:", 2, bytes[2]);
+		assertEquals("Assert 7:", 3, bytes[3]);
+		assertEquals("Assert 8:", 4, buf.read());
+		assertEquals("Assert 9:", -1, buf.read());
+
+		buf = new BufferedInputStream(
+				new ByteArrayInputStream(new byte[] { 0, 1, 2, 3, 4 }), 2);
+		buf.mark(Integer.MAX_VALUE);
+		buf.read();
+		buf.close();
+	}
+}

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/io/BufferedReaderTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/io/BufferedReaderTest.java?rev=381989&r1=381988&r2=381989&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/io/BufferedReaderTest.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/io/BufferedReaderTest.java
Wed Mar  1 03:24:55 2006
@@ -18,6 +18,7 @@
 import java.io.BufferedReader;
 import java.io.CharArrayReader;
 import java.io.IOException;
+import java.io.StringReader;
 
 import junit.framework.TestCase;
 
@@ -57,4 +58,37 @@
 			// expected
 		}
 	}
+	
+	/**
+	 * @tests java.io.BufferedReader#mark(int)
+	 */
+	public void test_markI() throws IOException {
+		BufferedReader buf = new BufferedReader(new StringReader("01234"), 2);
+		buf.mark(3);
+		char[] chars = new char[3];
+		int result = buf.read(chars);
+		assertEquals(3, result);
+		assertEquals("Assert 0:", '0', chars[0]);
+		assertEquals("Assert 1:", '1', chars[1]);
+		assertEquals("Assert 2:", '2', chars[2]);
+		assertEquals("Assert 3:", '3', buf.read());
+
+		buf = new BufferedReader(new StringReader("01234"), 2);
+		buf.mark(3);
+		chars = new char[4];
+		result = buf.read(chars);
+		assertEquals("Assert 4:", 4, result);
+		assertEquals("Assert 5:", '0', chars[0]);
+		assertEquals("Assert 6:", '1', chars[1]);
+		assertEquals("Assert 7:", '2', chars[2]);
+		assertEquals("Assert 8:", '3', chars[3]);
+		assertEquals("Assert 9:", '4', buf.read());
+		assertEquals("Assert 10:", -1, buf.read());
+
+		BufferedReader reader = new BufferedReader(new StringReader("01234"));
+		reader.mark(Integer.MAX_VALUE);
+		reader.read();
+		reader.close();
+	}
+
 }



Mime
View raw message