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/21 05:40:09 UTC

svn commit: r433137 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/io/PushbackReader.java test/java/tests/api/java/io/PushbackReaderTest.java

Author: pyang
Date: Sun Aug 20 20:40:07 2006
New Revision: 433137

URL: http://svn.apache.org/viewvc?rev=433137&view=rev
Log:
Patch applied for HARMONY-1224 ([classlib][luni] The exception throwing order problems: java.io.PushbackReader#unread(char[], int , int) and read(char[], int , int))

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

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/PushbackReader.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/PushbackReader.java?rev=433137&r1=433136&r2=433137&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/PushbackReader.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/PushbackReader.java Sun Aug 20 20:40:07 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.
@@ -61,6 +61,8 @@
      * @param size
      *            the size of the pushback buffer (<code>size>=0</code>) in
      *            characters.
+     * @throws IllegalArgumentException
+     *             if size <= 0
      */
     public PushbackReader(Reader in, int size) {
         super(in);
@@ -141,7 +143,7 @@
                  */
                 return in.read();
             }
-            throw new IOException();
+            throw new IOException(Msg.getString("K0059")); //$NON-NLS-1$
         }
     }
 
@@ -166,42 +168,41 @@
      */
     @Override
     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 (buf != null) {
-                    int copiedChars = 0, copyLength = 0, newOffset = offset;
-                    /* Are there pushback chars available? */
-                    if (pos < buf.length) {
-                        copyLength = (buf.length - pos >= count) ? count
-                                : buf.length - pos;
-                        System.arraycopy(buf, pos, buffer, newOffset,
-                                copyLength);
-                        newOffset += copyLength;
-                        copiedChars += copyLength;
-                        /* Use up the chars in the local buffer */
-                        pos += copyLength;
-                    }
-                    /* Have we copied enough? */
-                    if (copyLength == count) {
-                        return count;
-                    }
-                    int inCopied = in.read(buffer, newOffset, count
-                            - copiedChars);
-                    if (inCopied > 0) {
-                        return inCopied + copiedChars;
-                    }
-                    if (copiedChars == 0) {
-                        return inCopied;
-                    }
-                    return copiedChars;
-                }
-                throw new IOException();
-            }
-        }
-        throw new ArrayIndexOutOfBoundsException();
-    }
+		synchronized (lock) {
+			if (null == buf) {
+				throw new IOException(Msg.getString("K0059")); //$NON-NLS-1$
+			}
+			// avoid int overflow
+			if (offset < 0 || count < 0 || offset > buffer.length - count) {
+				throw new IndexOutOfBoundsException();
+			}
+			int copiedChars = 0;
+			int copyLength = 0;
+			int newOffset = offset;
+			/* Are there pushback chars available? */
+			if (pos < buf.length) {
+				copyLength = (buf.length - pos >= count) ? count : buf.length
+						- pos;
+				System.arraycopy(buf, pos, buffer, newOffset, copyLength);
+				newOffset += copyLength;
+				copiedChars += copyLength;
+				/* Use up the chars in the local buffer */
+				pos += copyLength;
+			}
+			/* Have we copied enough? */
+			if (copyLength == count) {
+				return count;
+			}
+			int inCopied = in.read(buffer, newOffset, count - copiedChars);
+			if (inCopied > 0) {
+				return inCopied + copiedChars;
+			}
+			if (copiedChars == 0) {
+				return inCopied;
+			}
+			return copiedChars;
+		}
+	}
 
     /**
      * Answers a <code>boolean</code> indicating whether or not this
@@ -276,22 +277,26 @@
      *             If the pushback buffer becomes, or is, full.
      */
     public void unread(char[] buffer, int offset, int count) throws IOException {
-        if (count > pos) {
-            // Pushback buffer full
-            throw new IOException(Msg.getString("K007e")); //$NON-NLS-1$
-        }
-        // avoid int overflow
-        if (0 <= offset && offset <= buffer.length && 0 <= count
-                && count <= buffer.length - offset) {
-            synchronized (lock) {
-                for (int i = offset + count - 1; i >= offset; i--) {
-                    unread(buffer[i]);
-                }
-            }
-        } else {
-            throw new ArrayIndexOutOfBoundsException();
-        }
-    }
+		synchronized (lock) {
+			if (buf == null) {
+				throw new IOException(Msg.getString("K0059")); //$NON-NLS-1$
+			}
+			if (count > pos) {
+				// Pushback buffer full
+				throw new IOException(Msg.getString("K007e")); //$NON-NLS-1$
+			}
+			if (buffer == null) {
+				throw new NullPointerException();
+			}
+			// avoid int overflow
+			if (offset < 0 || count < 0 || offset > buffer.length - count) {
+				throw new ArrayIndexOutOfBoundsException();
+			}
+			for (int i = offset + count - 1; i >= offset; i--) {
+				unread(buffer[i]);
+			}
+		}
+	}
 
     /**
      * Push back one <code>char</code>. Takes the char <code>oneChar</code>
@@ -313,7 +318,7 @@
                     throw new IOException(Msg.getString("K007e")); //$NON-NLS-1$
                 }
             } else {
-                throw new IOException();
+            	throw new IOException(Msg.getString("K0059")); //$NON-NLS-1$
             }
         }
     }
@@ -358,7 +363,7 @@
                 }
                 return inSkipped + availableFromBuffer;
             }
-            throw new IOException();
+            throw new IOException(Msg.getString("K0059")); //$NON-NLS-1$
         }
     }
 }

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/PushbackReaderTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/PushbackReaderTest.java?rev=433137&r1=433136&r2=433137&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/PushbackReaderTest.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/PushbackReaderTest.java Sun Aug 20 20:40:07 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.
@@ -121,6 +121,71 @@
 			fail("IOException during read test : " + e.getMessage());
 		}
 	}
+	
+	/**
+	 * @tests java.io.PushbackReader#read(char[], int, int)
+	 */
+	public void test_read_$CII_Exception() throws IOException {
+		pbr = new PushbackReader(new StringReader(pbString), 10);
+		
+		char[] nullCharArray = null;
+		char[] charArray = new char[10];
+		
+		try {
+			pbr.read(nullCharArray, -1, -1);
+			fail("should throw IndexOutOfBoundsException");
+		} catch (IndexOutOfBoundsException e) {
+			// expected
+		}
+		
+		try {
+			pbr.read(nullCharArray, 1, 0);
+			fail("should throw NullPointerException");
+		} catch (NullPointerException e) {
+			// expected
+		}
+		
+		try {
+			pbr.read(charArray, -1, -1);
+			fail("should throw IndexOutOfBoundsException");
+		} catch (IndexOutOfBoundsException e) {
+			// expected
+		}
+
+		pbr.read(charArray, 0, 0);
+        pbr.read(charArray, 0, charArray.length);
+		pbr.read(charArray, charArray.length, 0);
+		
+		try {
+			pbr.read(charArray, charArray.length + 1, 0);
+			fail("should throw IndexOutOfBoundsException");
+		} catch (IndexOutOfBoundsException e) {
+			//expected
+		}
+		
+		try {
+			pbr.read(charArray, charArray.length + 1, 1);
+			fail("should throw IndexOutOfBoundsException");
+		} catch (IndexOutOfBoundsException e) {
+			//expected
+		}
+
+		pbr.close();
+
+		try {
+			pbr.read(nullCharArray, -1, -1);
+			fail("should throw IOException");
+		} catch (IOException e) {
+			// expected
+		}
+
+		try {
+			pbr.read(charArray, -1, -1);
+			fail("should throw IOException");
+		} catch (IOException e) {
+			// expected
+		}
+	}
 
 	/**
 	 * @tests java.io.PushbackReader#ready()
@@ -250,6 +315,52 @@
 		}
 	}
 
+	/**
+	 * @tests java.io.PushbackReader#unread(char[], int, int)
+	 */
+	public void test_unread_$CII_NullPointerException() throws IOException {
+		//a pushback reader with one character buffer
+		pbr = new PushbackReader(new StringReader(pbString));
+		
+		try {
+			pbr.unread(null, 0, 1);
+			fail("should throw NullPointerException");
+		} catch (NullPointerException e) {
+			// expected
+		}
+	}
+	
+	/**
+	 * @tests java.io.PushbackReader#unread(char[], int, int)
+	 */
+	public void test_unread_$CII_Exception_InsufficientBuffer() throws IOException {
+		//a pushback reader with one character buffer
+		pbr = new PushbackReader(new StringReader(pbString));
+		
+		//if count > buffer's size , should throw IOException
+		try {
+			pbr.unread(new char[pbString.length()], 0, 2);
+			fail("should throw IOException");
+		} catch (IOException e) {
+			// expected
+		}
+	}
+	
+	/**
+	 * @tests java.io.PushbackReader#unread(char[], int, int)
+	 */
+	public void test_unread_$CII_ArrayIndexOutOfBoundsException() throws IOException {
+		//a pushback reader with one character buffer
+		pbr = new PushbackReader(new StringReader(pbString));
+		
+		try {
+			pbr.unread(new char[pbString.length()], -1 , -1);
+			fail("should throw ArrayIndexOutOfBoundsException");
+		} catch (ArrayIndexOutOfBoundsException e) {
+			// expected
+		}
+	}
+	
 	/**
 	 * @tests java.io.PushbackReader#unread(int)
 	 */