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/10 11:06:15 UTC

svn commit: r430325 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/io/OutputStreamWriter.java test/java/tests/api/java/io/OutputStreamWriterTest.java

Author: pyang
Date: Thu Aug 10 02:06:14 2006
New Revision: 430325

URL: http://svn.apache.org/viewvc?rev=430325&view=rev
Log:
Patch applied for HARMONY-1121(classlib][luni] java.io.OutputStreamWriter methods write(char[],int,int),write(int) write(String,int,int) throw wrong exceptions)

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

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/OutputStreamWriter.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/OutputStreamWriter.java?rev=430325&r1=430324&r2=430325&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/OutputStreamWriter.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/OutputStreamWriter.java Thu Aug 10 02:06:14 2006
@@ -66,7 +66,7 @@
 
 	/**
 	 * Constructs a new OutputStreamWriter using <code>out</code> as the
-	 * OutputStream to write converted characters to and <code>end</code> as
+	 * OutputStream to write converted characters to and <code>enc</code> as
 	 * the character encoding. If the encoding cannot be found, an
 	 * UnsupportedEncodingException error is thrown.
 	 * 
@@ -216,34 +216,34 @@
 	 *            maximum number of characters to write
 	 * 
 	 * @throws IOException
-	 *             If this OuputStreamWriter has already been closed or some
+	 *             If this OutputStreamWriter has already been closed or some
 	 *             other IOException occurs.
 	 * @throws IndexOutOfBoundsException
-	 *             If offset or count are outside of bounds.
+	 *             If offset or count is outside of bounds.
 	 */
 	public void write(char[] buf, int offset, int count) throws IOException {
-		if (offset < 0 || count < 0 || offset > buf.length - count) {
-			throw new IndexOutOfBoundsException();
+		synchronized (lock) {
+			checkStatus();
+			if (offset < 0 || offset > buf.length - count || count < 0) {
+				throw new IndexOutOfBoundsException();
+			}
+			CharBuffer chars = CharBuffer.wrap(buf, offset, count);
+			convert(chars);
 		}
-		CharBuffer chars = CharBuffer.wrap(buf, offset, count);
-		convert(chars);
 	}
 
 	private void convert(CharBuffer chars) throws IOException {
-		synchronized (lock) {
-			checkStatus();
-            CoderResult result = encoder.encode(chars, bytes, true);
-			while (true) {
-				if (result.isError()) {
-					throw new IOException(result.toString());
-				} else if (result.isOverflow()) {
-					//flush the output buffer
-					flush();
-					result = encoder.encode(chars, bytes, true);
-                    continue;
-				}
-				break;
+		CoderResult result = encoder.encode(chars, bytes, true);
+		while (true) {
+			if (result.isError()) {
+				throw new IOException(result.toString());
+			} else if (result.isOverflow()) {
+				//flush the output buffer
+				flush();
+				result = encoder.encode(chars, bytes, true);
+				continue;
 			}
+			break;
 		}
 	}
 
@@ -257,7 +257,7 @@
 	 *            the character to write
 	 * 
 	 * @throws IOException
-	 *             If this OuputStreamWriter has already been closed or some
+	 *             If this OutputStreamWriter has already been closed or some
 	 *             other IOException occurs.
 	 */
 	public void write(int oneChar) throws IOException {
@@ -283,7 +283,7 @@
 	 *            maximum number of characters to write
 	 * 
 	 * @throws IOException
-	 *             If this OuputStreamWriter has already been closed or some
+	 *             If this OutputStreamWriter has already been closed or some
 	 *             other IOException occurs.
 	 * @throws IndexOutOfBoundsException    
 	 *             If count is negative    
@@ -291,15 +291,18 @@
 	 *             If offset is negative or offset + count is outside of bounds
 	 */
 	public void write(String str, int offset, int count) throws IOException {
-		// avoid int overflow
-		if (count < 0) {
-			throw new IndexOutOfBoundsException();
-		}
-		if (offset > str.length() - count || offset < 0) {
-			throw new StringIndexOutOfBoundsException();
+		synchronized (lock) {
+			// avoid int overflow
+			if (count < 0) {
+				throw new IndexOutOfBoundsException();
+			}
+			if (offset > str.length() - count || offset < 0) {
+				throw new StringIndexOutOfBoundsException();
+			}
+			checkStatus();
+			CharBuffer chars = CharBuffer.wrap(str, offset, count + offset);
+			convert(chars);
 		}
-		CharBuffer chars = CharBuffer.wrap(str, offset, count + offset);
-		convert(chars);
 	}
 }
 

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/OutputStreamWriterTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/OutputStreamWriterTest.java?rev=430325&r1=430324&r2=430325&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/OutputStreamWriterTest.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/OutputStreamWriterTest.java Thu Aug 10 02:06:14 2006
@@ -106,6 +106,23 @@
 	 */
 	public void testWritecharArrayintint() throws IOException {
 		char[] chars = source.toCharArray();
+		
+		//throws IndexOutOfBoundsException if offset is negative
+		try {
+			writer.write((char[]) null, -1, -1);
+			fail("should throw IndexOutOfBoundsException");
+		} catch (IndexOutOfBoundsException e) {
+			//expected
+		}
+		
+		//throws NullPointerException though count is negative 
+		try {
+			writer.write((char[]) null, 1, -1);
+			fail("should throw NullPointerException");
+		} catch (NullPointerException e) {
+			//expected
+		}
+	
 		try {
 			writer.write((char[]) null, 1, 1);
 			fail();
@@ -137,6 +154,15 @@
 		writer.write(chars, 0, chars.length);
 		writer.flush();
 		assertEquals("hi" + source, out.toString("utf-8"));
+			
+		writer.close();
+        //after the stream is closed ,should throw IOException first
+		try {
+			writer.write((char[]) null, -1, -1);
+			fail("should throw IOException");
+		} catch (IOException e) {
+			//expected
+		}
 
 	}
 
@@ -163,6 +189,17 @@
 		writer.flush();
 		str = new String(out.toByteArray(), "utf-8");
 		assertEquals("\u0001\u0002\uffff\uedcb", str);
+		
+		writer.close();
+		 //after the stream is closed ,should throw IOException
+		try {
+			writer.write(1);
+            fail("should throw IOException");
+		} catch (IOException e) {
+			//expected
+		}
+		
+		
 	}
 
 	/*
@@ -217,6 +254,37 @@
 		writer.write(source, 0, source.length());
 		writer.flush();
 		assertEquals("bc" + source, out.toString("utf-8"));
+		
+		writer.close();
+        //throws IndexOutOfBoundsException first if count is negative
+		try {
+			writer.write((String) null, 0, -1);
+			fail("should throw IndexOutOfBoundsException");
+		} catch (IndexOutOfBoundsException e) {
+			//expected
+		}
+		
+		try {
+			writer.write((String) null, -1, 0);
+			fail("should throw NullPointerException");
+		} catch (NullPointerException e) {
+			//expected
+		}
+		
+		try {
+			writer.write("abc", -1, 0);
+			fail("should throw StringIndexOutOfBoundsException");
+		} catch (StringIndexOutOfBoundsException e) {
+			//expected
+		}
+		
+		//throws IOException
+		try {
+			writer.write("abc", 0, 1);
+			fail("should throw IOException");
+		} catch (IOException e) {
+			//expected
+		}
 
 	}