harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hinde...@apache.org
Subject svn commit: r432235 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/io/PipedReader.java test/java/tests/api/java/io/PipedWriterTest.java
Date Thu, 17 Aug 2006 13:33:49 GMT
Author: hindessm
Date: Thu Aug 17 06:33:49 2006
New Revision: 432235

URL: http://svn.apache.org/viewvc?rev=432235&view=rev
Log:
Applied patch from "[#HARMONY-1216] [classlib][luni] java.io.PipedWriter.write(char[],int,int)
method should throw IOException when the corresponding thread which is reading data is no
longer alive".

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/PipedReader.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/PipedWriterTest.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=432235&r1=432234&r2=432235&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
Thu Aug 17 06:33:49 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.
@@ -289,37 +289,43 @@
 	 *             occurs.
 	 */
 	void receive(char oneChar) throws IOException {
-		synchronized (lock) {
-			if (data != null) {
-				/**
-				 * Set the last thread to be writing on this PipedWriter. If
-				 * lastWriter dies while someone is waiting to read an
-				 * IOException of "Pipe broken" will be thrown in read()
-				 */
-				lastWriter = Thread.currentThread();
-				try {
-					while (data != null && out == in) {
-						notifyAll();
-						wait(1000);
-						if (lastReader != null && !lastReader.isAlive())
-							throw new IOException(org.apache.harmony.luni.util.Msg
-									.getString("K0076")); //$NON-NLS-1$
-					}
-				} catch (InterruptedException e) {
-					throw new InterruptedIOException();
-				}
-				if (data != null) {
-					if (in == -1)
-						in = 0;
-					data[in++] = oneChar;
-					if (in == data.length)
-						in = 0;
-					return;
-				}
-			}
-			throw new IOException(org.apache.harmony.luni.util.Msg.getString("K0078")); //$NON-NLS-1$
-		}
-	}
+        synchronized (lock) {
+            if (data == null || isClosed) {
+                throw new IOException(org.apache.harmony.luni.util.Msg
+                        .getString("K0078")); //$NON-NLS-1$
+            }
+            if (lastReader != null && !lastReader.isAlive()) {
+                throw new IOException(org.apache.harmony.luni.util.Msg
+                        .getString("K0076")); //$NON-NLS-1$
+            }
+            /**
+             * Set the last thread to be writing on this PipedWriter. If
+             * lastWriter dies while someone is waiting to read an IOException
+             * of "Pipe broken" will be thrown in read()
+             */
+            lastWriter = Thread.currentThread();
+            try {
+                while (data != null && out == in) {
+                    notifyAll();
+                    wait(1000);
+                    if (lastReader != null && !lastReader.isAlive()) {
+                        throw new IOException(org.apache.harmony.luni.util.Msg
+                                .getString("K0076")); //$NON-NLS-1$
+                    }
+                }
+            } catch (InterruptedException e) {
+                throw new InterruptedIOException();
+            }
+            if (data != null) {
+                if (in == -1)
+                    in = 0;
+                data[in++] = oneChar;
+                if (in == data.length)
+                    in = 0;
+                return;
+            }
+        }
+    }
 
 	/**
 	 * Receives a char array and stores it into the PipedReader. This called by
@@ -340,57 +346,64 @@
 	 *             occurs.
 	 */
 	void receive(char[] chars, int offset, int count) throws IOException {
-		synchronized (lock) {
-			if (data != null) {
-				/**
-				 * Set the last thread to be writing on this PipedWriter. If
-				 * lastWriter dies while someone is waiting to read an
-				 * IOException of "Pipe broken" will be thrown in read()
-				 */
-				lastWriter = Thread.currentThread();
-				while (count > 0) {
-					try {
-						while (data != null && out == in) {
-							notifyAll();
-							wait(1000);
-							if (lastReader != null && !lastReader.isAlive())
-								throw new IOException(org.apache.harmony.luni.util.Msg
-										.getString("K0076")); //$NON-NLS-1$
-						}
-					} catch (InterruptedException e) {
-						throw new InterruptedIOException();
-					}
-					if (data == null)
-						break;
-					if (in == -1)
-						in = 0;
-					if (in >= out) {
-						int length = data.length - in;
-						if (count < length)
-							length = count;
-						System.arraycopy(chars, offset, data, in, length);
-						offset += length;
-						count -= length;
-						in += length;
-						if (in == data.length)
-							in = 0;
-					}
-					if (count > 0 && in != out) {
-						int length = out - in;
-						if (count < length)
-							length = count;
-						System.arraycopy(chars, offset, data, in, length);
-						offset += length;
-						count -= length;
-						in += length;
-					}
-				}
-				if (count == 0)
-					return;
-			}
-			throw new IOException(org.apache.harmony.luni.util.Msg.getString("K0078")); //$NON-NLS-1$
-		}
-	}
+        synchronized (lock) {
+            if (data == null || isClosed) {
+                throw new IOException(org.apache.harmony.luni.util.Msg
+                        .getString("K0078")); //$NON-NLS-1$
+            }
+            if (lastReader != null && !lastReader.isAlive()) {
+                throw new IOException(org.apache.harmony.luni.util.Msg
+                        .getString("K0076")); //$NON-NLS-1$
+            }
+            /**
+             * Set the last thread to be writing on this PipedWriter. If
+             * lastWriter dies while someone is waiting to read an IOException
+             * of "Pipe broken" will be thrown in read()
+             */
+            lastWriter = Thread.currentThread();
+            while (count > 0) {
+                try {
+                    while (data != null && out == in) {
+                        notifyAll();
+                        wait(1000);
+                        if (lastReader != null && !lastReader.isAlive()) {
+                            throw new IOException(
+                                    org.apache.harmony.luni.util.Msg
+                                            .getString("K0076")); //$NON-NLS-1$
+                        }
+                    }
+                } catch (InterruptedException e) {
+                    throw new InterruptedIOException();
+                }
+                if (data == null)
+                    break;
+                if (in == -1)
+                    in = 0;
+                if (in >= out) {
+                    int length = data.length - in;
+                    if (count < length)
+                        length = count;
+                    System.arraycopy(chars, offset, data, in, length);
+                    offset += length;
+                    count -= length;
+                    in += length;
+                    if (in == data.length)
+                        in = 0;
+                }
+                if (count > 0 && in != out) {
+                    int length = out - in;
+                    if (count < length)
+                        length = count;
+                    System.arraycopy(chars, offset, data, in, length);
+                    offset += length;
+                    count -= length;
+                    in += length;
+                }
+            }
+            if (count == 0)
+                return;
+        }
+    }
 
 	void done() {
 		synchronized (lock) {

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/PipedWriterTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/PipedWriterTest.java?rev=432235&r1=432234&r2=432235&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/PipedWriterTest.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/PipedWriterTest.java
Thu Aug 17 06:33:49 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.
@@ -236,6 +236,129 @@
         } catch (IndexOutOfBoundsException t) {
             fail("NullPointerException expected");
         } catch (NullPointerException t) {}
+    }
+    
+    /**
+     * @tests java.io.PipedWriter#write(int)
+     */
+    public void test_write_I_MultiThread() throws IOException {
+        final PipedReader pr = new PipedReader();
+        final PipedWriter pw = new PipedWriter();
+        // test if writer recognizes dead reader
+        pr.connect(pw);
+
+        class WriteRunnable implements Runnable {
+            boolean pass = false;
+            boolean readerAlive = true;
+            public void run() {
+                try {
+                    pw.write(1);
+                    while (readerAlive) {
+                    // wait the reader thread dead
+                    }
+                    try {
+                        // should throw exception since reader thread
+                        // is now dead
+                        pw.write(1);
+                    } catch (IOException e) {
+                        pass = true;
+                    }
+                } catch (IOException e) {
+                  //ignore
+                }
+            }
+        }
+        WriteRunnable writeRunnable = new WriteRunnable();
+        Thread writeThread = new Thread(writeRunnable);
+        class ReadRunnable implements Runnable {
+            boolean pass;
+            public void run() {
+                try {
+                    pr.read();
+                    pass = true;
+                } catch (IOException e) {
+                  //ignore
+                }
+            }
+        }
+        ReadRunnable readRunnable = new ReadRunnable();
+        Thread readThread = new Thread(readRunnable);
+        writeThread.start();
+        readThread.start();
+        while (readThread.isAlive()) {
+           //wait the reader thread dead
+        }
+        writeRunnable.readerAlive = false;
+        assertTrue("reader thread failed to read", readRunnable.pass);
+        while (writeThread.isAlive()) {
+           //wait the writer thread dead
+        }
+        assertTrue("writer thread failed to recognize dead reader",
+                writeRunnable.pass);
+    }
+    
+    /**
+     * @tests java.io.PipedWriter#write(char[],int,int)
+     */
+    public void test_write_$CII_MultiThread() throws Exception {
+        final PipedReader pr = new PipedReader();
+        final PipedWriter pw = new PipedWriter();
+
+        // test if writer recognizes dead reader
+        pr.connect(pw);
+
+        class WriteRunnable implements Runnable {
+            boolean pass = false;
+
+            boolean readerAlive = true;
+
+            public void run() {
+                try {
+                    pw.write(1);
+                    while (readerAlive) {
+                    // wait the reader thread dead
+                    }
+                    try {
+                        // should throw exception since reader thread
+                        // is now dead
+                        char[] buf = new char[10];
+                        pw.write(buf, 0, 10);
+                    } catch (IOException e) {
+                        pass = true;
+                    }
+                } catch (IOException e) {
+                  //ignore
+                }
+            }
+        }
+        WriteRunnable writeRunnable = new WriteRunnable();
+        Thread writeThread = new Thread(writeRunnable);
+        class ReadRunnable implements Runnable {
+            boolean pass;
+
+            public void run() {
+                try {
+                    pr.read();
+                    pass = true;
+                } catch (IOException e) {
+                  //ignore
+                }
+            }
+        }
+        ReadRunnable readRunnable = new ReadRunnable();
+        Thread readThread = new Thread(readRunnable);
+        writeThread.start();
+        readThread.start();
+        while (readThread.isAlive()) {
+            //wait the reader thread dead
+        }
+        writeRunnable.readerAlive = false;
+        assertTrue("reader thread failed to read", readRunnable.pass);
+        while (writeThread.isAlive()) {
+            //wait the writer thread dead
+        }
+        assertTrue("writer thread failed to recognize dead reader",
+                writeRunnable.pass);
     }
 
     /**



Mime
View raw message