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/22 06:20:55 UTC

svn commit: r433501 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/io/PipedReader.java main/java/java/io/PipedWriter.java test/java/tests/api/java/io/PipedReaderTest.java

Author: pyang
Date: Mon Aug 21 21:20:53 2006
New Revision: 433501

URL: http://svn.apache.org/viewvc?rev=433501&view=rev
Log:
Patch applied for HARMONY-1238 ([classlib][luni] java.io.PipedReader.read(char[],int,int) method throws different exception with RI)

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/PipedReader.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/PipedWriter.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/PipedReaderTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/PipedReader.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/PipedReader.java?rev=433501&r1=433500&r2=433501&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/PipedReader.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/PipedReader.java Mon Aug 21 21:20:53 2006
@@ -113,13 +113,10 @@
 	/**
 	 * Establish the connection to the PipedWriter.
 	 * 
-	 * @param src
-	 *            the source PipedWriter
-	 * 
 	 * @throws IOException
 	 *             If this Reader is already connected.
 	 */
-	void establishConnection(PipedWriter src) throws IOException {
+	void establishConnection() throws IOException {
 		synchronized (lock) {
 			if (data == null)
 				throw new IOException(org.apache.harmony.luni.util.Msg.getString("K0078")); //$NON-NLS-1$
@@ -174,84 +171,83 @@
 	 *             occurs.
 	 */
 	public int read(char[] buffer, int offset, int count) throws IOException {
-		// avoid int overflow
-		if (0 <= offset && offset <= buffer.length && 0 <= count
-				&& count <= buffer.length - offset) {
-			synchronized (lock) {
-				if (isConnected && data != null) {
-					if (count == 0)
-						return 0;
-					/**
-					 * Set the last thread to be reading on this PipedReader. If
-					 * lastReader dies while someone is waiting to write an
-					 * IOException of "Pipe broken" will be thrown in receive()
-					 */
-					lastReader = Thread.currentThread();
-					try {
-						boolean first = true;
-						while (in == -1) {
-							// Are we at end of stream?
-							if (isClosed)
-								return -1;
-							if (!first && lastWriter != null
-									&& !lastWriter.isAlive())
-								throw new IOException(org.apache.harmony.luni.util.Msg
-										.getString("K0076")); //$NON-NLS-1$
-							first = false;
-							// Notify callers of receive()
-							notifyAll();
-							lock.wait(1000);
-						}
-					} catch (InterruptedException e) {
-						throw new InterruptedIOException();
-					}
-
-					int copyLength = 0;
-					/* Copy chars from out to end of buffer first */
-					if (out >= in) {
-						copyLength = count > data.length - out ? data.length
-								- out : count;
-						System.arraycopy(data, out, buffer, offset, copyLength);
-						out += copyLength;
-						if (out == data.length)
-							out = 0;
-						if (out == in) {
-							// empty buffer
-							in = -1;
-							out = 0;
-						}
-					}
-
-					/*
-					 * Did the read fully succeed in the previous copy or is the
-					 * buffer empty?
-					 */
-					if (copyLength == count || in == -1)
-						return copyLength;
-
-					int charsCopied = copyLength;
-					/* Copy bytes from 0 to the number of available bytes */
-					copyLength = in - out > count - copyLength ? count
-							- copyLength : in - out;
-					System.arraycopy(data, out, buffer, offset + charsCopied,
-							copyLength);
-					out += copyLength;
-					if (out == in) {
-						// empty buffer
-						in = -1;
-						out = 0;
-					}
-					return charsCopied + copyLength;
-				}
-				if (!isConnected) {
-					throw new IOException(org.apache.harmony.luni.util.Msg
-							.getString("K007b")); //$NON-NLS-1$
-				}
-				throw new IOException(org.apache.harmony.luni.util.Msg.getString("K0078")); //$NON-NLS-1$
-			}
-		}
-		throw new IndexOutOfBoundsException();
-	}
+        synchronized (lock) {
+            if (!isConnected) {
+                throw new IOException(org.apache.harmony.luni.util.Msg
+                        .getString("K007b")); //$NON-NLS-1$
+            }
+            if (data == null) {
+                throw new IOException(org.apache.harmony.luni.util.Msg
+                        .getString("K0078")); //$NON-NLS-1$
+            }
+            // avoid int overflow
+            if (offset < 0 || count > buffer.length - offset || count < 0) {
+                throw new IndexOutOfBoundsException();
+            }
+            if (count == 0)
+                return 0;
+            /**
+             * Set the last thread to be reading on this PipedReader. If
+             * lastReader dies while someone is waiting to write an IOException
+             * of "Pipe broken" will be thrown in receive()
+             */
+            lastReader = Thread.currentThread();
+            try {
+                boolean first = true;
+                while (in == -1) {
+                    // Are we at end of stream?
+                    if (isClosed)
+                        return -1;
+                    if (!first && lastWriter != null && !lastWriter.isAlive())
+                        throw new IOException(org.apache.harmony.luni.util.Msg
+                                .getString("K0076")); //$NON-NLS-1$
+                    first = false;
+                    // Notify callers of receive()
+                    notifyAll();
+                    lock.wait(1000);
+                }
+            } catch (InterruptedException e) {
+                throw new InterruptedIOException();
+            }
+
+            int copyLength = 0;
+            /* Copy chars from out to end of buffer first */
+            if (out >= in) {
+                copyLength = count > data.length - out ? data.length - out
+                        : count;
+                System.arraycopy(data, out, buffer, offset, copyLength);
+                out += copyLength;
+                if (out == data.length)
+                    out = 0;
+                if (out == in) {
+                    // empty buffer
+                    in = -1;
+                    out = 0;
+                }
+            }
+
+            /*
+             * Did the read fully succeed in the previous copy or is the buffer
+             * empty?
+             */
+            if (copyLength == count || in == -1)
+                return copyLength;
+
+            int charsCopied = copyLength;
+            /* Copy bytes from 0 to the number of available bytes */
+            copyLength = in - out > count - copyLength ? count - copyLength
+                    : in - out;
+            System.arraycopy(data, out, buffer, offset + charsCopied,
+                    copyLength);
+            out += copyLength;
+            if (out == in) {
+                // empty buffer
+                in = -1;
+                out = 0;
+            }
+            return charsCopied + copyLength;
+        }
+    }
 
 	/**
 	 * Answer a boolean indicating whether or not this Reader is ready to be
@@ -290,7 +286,7 @@
 	 */
 	void receive(char oneChar) throws IOException {
         synchronized (lock) {
-            if (data == null || isClosed) {
+            if (data == null) {
                 throw new IOException(org.apache.harmony.luni.util.Msg
                         .getString("K0078")); //$NON-NLS-1$
             }
@@ -347,7 +343,7 @@
 	 */
 	void receive(char[] chars, int offset, int count) throws IOException {
         synchronized (lock) {
-            if (data == null || isClosed) {
+            if (data == null) {
                 throw new IOException(org.apache.harmony.luni.util.Msg
                         .getString("K0078")); //$NON-NLS-1$
             }

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/PipedWriter.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/PipedWriter.java?rev=433501&r1=433500&r2=433501&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/PipedWriter.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/PipedWriter.java Mon Aug 21 21:20:53 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.
@@ -88,7 +88,7 @@
 		synchronized (lock) {
 			if (this.dest == null) {
 				if (!closed) {
-					stream.establishConnection(this);
+					stream.establishConnection();
 					this.dest = stream;
 				} else
 					throw new IOException(org.apache.harmony.luni.util.Msg

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/PipedReaderTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/PipedReaderTest.java?rev=433501&r1=433500&r2=433501&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/PipedReaderTest.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/PipedReaderTest.java Mon Aug 21 21:20:53 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.
@@ -202,6 +202,153 @@
         } catch (ArrayIndexOutOfBoundsException t) {
             fail("IndexOutOfBoundsException expected");
         } catch (IndexOutOfBoundsException t) {
+        }
+    }
+    
+    /**
+     * @tests java.io.PipedReader#read(char[], int, int)
+     */
+    public void test_read_$CII_IOException() throws IOException {
+        PipedWriter pw = new PipedWriter();
+        PipedReader pr = new PipedReader(pw);
+        char[] buf = null;
+        pr.close();
+        try {
+            pr.read(buf, 0, 10);
+            fail("Should throws IOException"); //$NON-NLS-1$
+        } catch (IOException e) {
+            // expected
+        } finally {
+            pw = null;
+            pr = null;
+        }
+        
+        pr = new PipedReader();
+        buf = null;
+        pr.close();
+        try {
+            pr.read(buf, 0, 10);
+            fail("Should throws IOException"); //$NON-NLS-1$
+        } catch (IOException e) {
+            // expected
+        } finally {
+            pr = null;
+        }
+        
+        pw = new PipedWriter();
+        pr = new PipedReader(pw);
+        buf = new char[10];
+        pr.close();
+        try {
+            pr.read(buf, -1, 0);
+            fail("Should throws IOException"); //$NON-NLS-1$
+        } catch (IOException e) {
+            // expected
+        } finally {
+            pw = null;
+            pr = null;
+        }
+        
+        pw = new PipedWriter();
+        pr = new PipedReader(pw);
+        buf = new char[10];
+        pr.close();
+        try {
+            pr.read(buf, 0, -1);
+            fail("Should throws IOException"); //$NON-NLS-1$
+        } catch (IOException e) {
+            // expected
+        } finally {
+            pw = null;
+            pr = null;
+        }
+        
+        pw = new PipedWriter();
+        pr = new PipedReader(pw);
+        buf = new char[10];
+        pr.close();
+        try {
+            pr.read(buf, 1, 10);
+            fail("Should throws IOException"); //$NON-NLS-1$
+        } catch (IOException e) {
+            // expected
+        } finally {
+            pw = null;
+            pr = null;
+        }
+        
+        pw = new PipedWriter();
+        pr = new PipedReader(pw);
+        pr.close();
+        try {
+            pr.read(new char[0], -1, -1);
+            fail("should throw IOException"); //$NON-NLS-1$
+        } catch (IOException e) {
+            // expected
+        } finally {
+            pw = null;
+            pr = null;
+        }
+        
+        pw = new PipedWriter();
+        pr = new PipedReader(pw);
+        pr.close();
+        try {
+            pr.read(null, 0, 1);
+            fail("should throw IOException"); //$NON-NLS-1$
+        } catch (IOException e) {
+            // expected
+        } finally {
+            pw = null;
+            pr = null;
+        }
+        
+        pw = new PipedWriter();
+        pr = new PipedReader(pw);
+        try {
+            pr.read(null, -1, 1);
+            fail("should throw IndexOutOfBoundsException"); //$NON-NLS-1$
+        } catch (IndexOutOfBoundsException e) {
+            // expected
+        } finally {
+            pw = null;
+            pr = null;
+        }
+        
+        pw = new PipedWriter();
+        pr = new PipedReader(pw);
+        try {
+            pr.read(null, 0, -1);
+            fail("should throw NullPointerException"); //$NON-NLS-1$
+        } catch (NullPointerException e) {
+            // expected
+        } finally {
+            pw = null;
+            pr = null;
+        }
+        
+        pw = new PipedWriter();
+        pr = new PipedReader(pw);
+        try {
+            pr.read(new char[10], 11, 0);
+            fail("should throw IndexOutOfBoundsException"); //$NON-NLS-1$
+        } catch (IndexOutOfBoundsException e) {
+            // expected
+        } finally {
+            pw = null;
+            pr = null;
+        }
+        
+        pw = new PipedWriter();
+        pr = new PipedReader(pw);
+        try {
+            pr.read(null, 1, 0);
+            fail("should throw NullPointerException"); //$NON-NLS-1$
+        } catch (NullPointerException e) {
+            // expected
+        } finally {
+            pw = null;
+            pr = null;
         }
     }