You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by py...@apache.org on 2006/08/16 08:34:42 UTC
svn commit: r431843 - in
/incubator/harmony/enhanced/classlib/trunk/modules/luni/src:
main/java/java/io/BufferedWriter.java
test/java/tests/api/java/io/BufferedWriterTest.java
Author: pyang
Date: Tue Aug 15 23:34:41 2006
New Revision: 431843
URL: http://svn.apache.org/viewvc?rev=431843&view=rev
Log:
Patch applied for HARMONY-1193 ([classlib][luni] java.io.BufferedWriter.write(char[],int,int) and write(String,int,int) throws different exceptions with RI)
Modified:
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedWriter.java
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/BufferedWriterTest.java
Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedWriter.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedWriter.java?rev=431843&r1=431842&r2=431843&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedWriter.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedWriter.java Tue Aug 15 23:34:41 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.
@@ -156,49 +156,46 @@
* @throws IOException
* If this Writer has already been closed or some other
* IOException occurs.
- * @throws ArrayIndexOutOfBoundsException
+ * @throws IndexOutOfBoundsException
* If offset or count are outside of bounds.
*/
@Override
public void write(char[] cbuf, int offset, int count) throws IOException {
- // avoid int overflow
- if (0 <= offset && offset <= cbuf.length && 0 <= count
- && count <= cbuf.length - offset) {
- synchronized (lock) {
- if (isOpen()) {
- if (pos == 0 && count >= this.buf.length) {
- out.write(cbuf, offset, count);
+ synchronized (lock) {
+ if (!isOpen()) {
+ throw new IOException(org.apache.harmony.luni.util.Msg
+ .getString("K005d"));
+ }
+ if (offset < 0 || offset > cbuf.length - count || count < 0) {
+ throw new IndexOutOfBoundsException();
+ }
+ if (pos == 0 && count >= this.buf.length) {
+ out.write(cbuf, offset, count);
+ return;
+ }
+ int available = this.buf.length - pos;
+ if (count < available)
+ available = count;
+ if (available > 0) {
+ System.arraycopy(cbuf, offset, this.buf, pos, available);
+ pos += available;
+ }
+ if (pos == this.buf.length) {
+ out.write(this.buf, 0, this.buf.length);
+ pos = 0;
+ if (count > available) {
+ offset += available;
+ available = count - available;
+ if (available >= this.buf.length) {
+ out.write(cbuf, offset, available);
return;
}
- int available = this.buf.length - pos;
- if (count < available) {
- available = count;
- }
- if (available > 0) {
- System.arraycopy(cbuf, offset, this.buf, pos, available);
- pos += available;
- }
- if (pos == this.buf.length) {
- out.write(this.buf, 0, this.buf.length);
- pos = 0;
- if (count > available) {
- offset += available;
- available = count - available;
- if (available >= this.buf.length) {
- out.write(cbuf, offset, available);
- return;
- }
- System.arraycopy(cbuf, offset, this.buf, pos, available);
- pos += available;
- }
- }
- } else {
- throw new IOException(Msg.getString("K005d")); //$NON-NLS-1$
- }
- }
- } else {
- throw new ArrayIndexOutOfBoundsException();
+
+ System.arraycopy(cbuf, offset, this.buf, pos, available);
+ pos += available;
+ }
+ }
}
}
@@ -248,48 +245,46 @@
@Override
public void write(String str, int offset, int count) throws IOException {
- // avoid int overflow
- if (0 <= offset && offset <= str.length() && 0 <= count
- && count <= str.length() - offset) {
- synchronized (lock) {
- if (isOpen()) {
- if (pos == 0 && count >= buf.length) {
+ synchronized (lock) {
+ if (!isOpen()) {
+ throw new IOException(org.apache.harmony.luni.util.Msg
+ .getString("K005d")); //$NON-NLS-1$
+ }
+ if (count <= 0) {
+ return;
+ }
+ if (offset > str.length() - count || offset < 0 ) {
+ throw new StringIndexOutOfBoundsException();
+ }
+ if (pos == 0 && count >= buf.length) {
+ char[] chars = new char[count];
+ str.getChars(offset, offset + count, chars, 0);
+ out.write(chars, 0, count);
+ return;
+ }
+ int available = buf.length - pos;
+ if (count < available)
+ available = count;
+ if (available > 0) {
+ str.getChars(offset, offset + available, buf, pos);
+ pos += available;
+ }
+ if (pos == buf.length) {
+ out.write(this.buf, 0, this.buf.length);
+ pos = 0;
+ if (count > available) {
+ offset += available;
+ available = count - available;
+ if (available >= buf.length) {
char[] chars = new char[count];
- str.getChars(offset, offset + count, chars, 0);
- out.write(chars, 0, count);
+ str.getChars(offset, offset + available, chars, 0);
+ out.write(chars, 0, available);
return;
}
- int available = buf.length - pos;
- if (count < available) {
- available = count;
- }
- if (available > 0) {
- str.getChars(offset, offset + available, buf, pos);
- pos += available;
- }
- if (pos == buf.length) {
- out.write(this.buf, 0, this.buf.length);
- pos = 0;
- if (count > available) {
- offset += available;
- available = count - available;
- if (available >= buf.length) {
- char[] chars = new char[count];
- str.getChars(offset, offset + available, chars,
- 0);
- out.write(chars, 0, available);
- return;
- }
- str.getChars(offset, offset + available, buf, pos);
- pos += available;
- }
- }
- } else {
- throw new IOException(Msg.getString("K005d")); //$NON-NLS-1$
- }
+ str.getChars(offset, offset + available, buf, pos);
+ pos += available;
+ }
}
- } else {
- throw new StringIndexOutOfBoundsException();
}
}
}
Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/BufferedWriterTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/BufferedWriterTest.java?rev=431843&r1=431842&r2=431843&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/BufferedWriterTest.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/BufferedWriterTest.java Tue Aug 15 23:34:41 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.
@@ -102,7 +102,8 @@
public void test_write$CII() {
// Test for method void java.io.BufferedWriter.write(char [], int, int)
try {
- bw.write(testString, 500, 1000);
+ char[] testCharArray = testString.toCharArray();
+ bw.write(testCharArray, 500, 1000);
bw.flush();
assertTrue("Incorrect string written", sw.toString().equals(
testString.substring(500, 1500)));
@@ -110,6 +111,64 @@
fail("Exception during write test");
}
}
+
+ /**
+ * @tests java.io.BufferedWriter#write(char[], int, int)
+ */
+ public void test_write_$CII_Exception() throws IOException {
+ BufferedWriter bWriter = new BufferedWriter(sw);
+ char[] nullCharArray = null;
+
+ try {
+ bWriter.write(nullCharArray, -1, -1);
+ fail("should throw IndexOutOfBoundsException");
+ } catch (IndexOutOfBoundsException e) {
+ // expected
+ }
+
+ try {
+ bWriter.write(nullCharArray, -1, 0);
+ fail("should throw IndexOutOfBoundsException");
+ } catch (IndexOutOfBoundsException e) {
+ // expected
+ }
+
+ try {
+ bWriter.write(nullCharArray, 0, -1);
+ fail("should throw NullPointerException");
+ } catch (NullPointerException e) {
+ // expected
+ }
+
+ try {
+ bWriter.write(nullCharArray, 0, 0);
+ fail("should throw NullPointerException");
+ } catch (NullPointerException e) {
+ // expected
+ }
+
+ char[] testCharArray = testString.toCharArray();
+
+ bWriter.write(testCharArray, 0, 0);
+
+ bWriter.write(testCharArray, testCharArray.length, 0);
+
+ try {
+ bWriter.write(testCharArray, testCharArray.length + 1, 0);
+ fail("should throw IndexOutOfBoundsException");
+ } catch (IndexOutOfBoundsException e) {
+ //expected
+ }
+
+ bWriter.close();
+
+ try {
+ bWriter.write(nullCharArray, -1, -1);
+ fail("should throw IOException");
+ } catch (IOException e) {
+ // expected
+ }
+ }
/**
* @tests java.io.BufferedWriter#write(int)
@@ -139,6 +198,59 @@
testString));
} catch (Exception e) {
fail("Exception during write test");
+ }
+ }
+
+ /**
+ * @tests java.io.BufferedWriter#write(java.lang.String, int, int)
+ */
+ public void test_write_LStringII_Exception() throws IOException {
+ BufferedWriter bWriter = new BufferedWriter(sw);
+
+ bWriter.write((String) null , -1, -1);
+ bWriter.write((String) null , -1, 0);
+ bWriter.write((String) null , 0 , -1);
+ bWriter.write((String) null , 0 , 0);
+
+ try {
+ bWriter.write((String) null , -1, 1);
+ fail("should throw NullPointerException");
+ } catch (NullPointerException e) {
+ // expected
+ }
+
+ bWriter.write(testString, 0, 0);
+ bWriter.write(testString, testString.length(), 0);
+ bWriter.write(testString, testString.length() + 1, 0);
+
+ try {
+ bWriter.write(testString, testString.length() + 1, 1);
+ fail("should throw StringIndexOutOfBoundsException");
+ } catch (StringIndexOutOfBoundsException e) {
+ // expected
+ }
+
+ bWriter.close();
+
+ try {
+ bWriter.write((String) null , -1, -1);
+ fail("should throw IOException");
+ } catch (IOException e) {
+ // expected
+ }
+
+ try {
+ bWriter.write((String) null , -1, 1);
+ fail("should throw IOException");
+ } catch (IOException e) {
+ // expected
+ }
+
+ try {
+ bWriter.write(testString , -1, -1);
+ fail("should throw IOException");
+ } catch (IOException e) {
+ // expected
}
}