harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hinde...@apache.org
Subject svn commit: r432229 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/io/BufferedReader.java test/java/tests/api/java/io/BufferedReaderTest.java
Date Thu, 17 Aug 2006 13:21:56 GMT
Author: hindessm
Date: Thu Aug 17 06:21:55 2006
New Revision: 432229

URL: http://svn.apache.org/viewvc?rev=432229&view=rev
Log:
Applied patch from "[#HARMONY-1215] [classlib][luni] java.io.BufferedReader#read(char[], int
, int) method's exception order is different from RI's".

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

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedReader.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedReader.java?rev=432229&r1=432228&r2=432229&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedReader.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedReader.java
Thu Aug 17 06:21:55 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.
@@ -74,6 +74,8 @@
 	 *            the Reader to buffer reads on.
 	 * @param size
 	 *            the size of buffer to allocate.
+	 * @throws IllegalArgumentException 
+	 *            if the size is <= 0         
 	 */
 
 	public BufferedReader(Reader in, int size) {
@@ -160,6 +162,8 @@
 	 * 
 	 * @throws IOException
 	 *             If an error occurs attempting mark this BufferedReader.
+	 * @throws IllegalArgumentException
+	 *             If readlimit is < 0      
 	 */
 
 	@Override
@@ -245,62 +249,58 @@
 	@Override
     public int read(char[] buffer, int offset, int length) throws IOException {
 		synchronized (lock) {
-			if (isOpen()) {
-				// check for null
-				int bufLen = buffer.length;
-                if(offset < 0 || length < 0 || (long)offset + (long)length > bufLen){
-                    throw new ArrayIndexOutOfBoundsException();
-                }
-				if (length == 0) {
-                    return 0;
-                }
-                int required;
-                if (pos < count) {
-                    /* There are bytes available in the buffer. */
-                    int copylength = count - pos >= length ? length : count
-                            - pos;
-                    System.arraycopy(buf, pos, buffer, offset, copylength);
-                    pos += copylength;
-                    if (copylength == length || !in.ready()) {
-                        return copylength;
-                    }
-                    offset += copylength;
-                    required = length - copylength;
-                } else {
-                    required = length;
-                }
+			if (!isOpen()) {
+				throw new IOException(Msg.getString("K005b")); //$NON-NLS-1$
+			}
+            if (offset < 0 || offset > buffer.length - length || length < 0) {
+				throw new IndexOutOfBoundsException();
+			}
+			if (length == 0) {
+				return 0;
+			}
+			int required;
+			if (pos < count) {
+				/* There are bytes available in the buffer. */
+				int copylength = count - pos >= length ? length : count - pos;
+				System.arraycopy(buf, pos, buffer, offset, copylength);
+				pos += copylength;
+				if (copylength == length || !in.ready()) {
+					return copylength;
+				}
+				offset += copylength;
+				required = length - copylength;
+			} else {
+				required = length;
+			}
 
-                while (true) {
-                    int read;
-                    /*
-                     * If we're not marked and the required size is greater than
-                     * the buffer, simply read the bytes directly bypassing the
-                     * buffer.
-                     */
-                    if (markpos == -1 && required >= buf.length) {
-                        read = in.read(buffer, offset, required);
-                        if (read == -1) {
-                            return required == length ? -1 : length - required;
-                        }
-                    } else {
-                        if (fillbuf() == -1) {
-                            return required == length ? -1 : length - required;
-                        }
-                        read = count - pos >= required ? required : count - pos;
-                        System.arraycopy(buf, pos, buffer, offset, read);
-                        pos += read;
-                    }
-                    required -= read;
-                    if (required == 0) {
-                        return length;
-                    }
-                    if (!in.ready()) {
-                        return length - required;
-                    }
-                    offset += read;
-                }
+			while (true) {
+				int read;
+				/*
+				 * If we're not marked and the required size is greater than the
+				 * buffer, simply read the bytes directly bypassing the buffer.
+				 */
+				if (markpos == -1 && required >= buf.length) {
+					read = in.read(buffer, offset, required);
+					if (read == -1) {
+						return required == length ? -1 : length - required;
+					}
+				} else {
+					if (fillbuf() == -1) {
+						return required == length ? -1 : length - required;
+					}
+					read = count - pos >= required ? required : count - pos;
+					System.arraycopy(buf, pos, buffer, offset, read);
+					pos += read;
+				}
+				required -= read;
+				if (required == 0) {
+					return length;
+				}
+				if (!in.ready()) {
+					return length - required;
+				}
+				offset += read;
 			}
-			throw new IOException(Msg.getString("K005b")); //$NON-NLS-1$
 		}
 	}
 
@@ -434,6 +434,8 @@
 	 * @throws IOException
 	 *             If the BufferedReader is already closed or some other IO
 	 *             error occurs.
+	 * @throws  IllegalArgumentException
+	 *              If amount is negative
 	 */
 
 	@Override

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/BufferedReaderTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/BufferedReaderTest.java?rev=432229&r1=432228&r2=432229&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/BufferedReaderTest.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/BufferedReaderTest.java
Thu Aug 17 06:21:55 2006
@@ -273,6 +273,104 @@
 	}
 
 	/**
+	 * @tests java.io.BufferedReader#read(char[], int, int)
+	 */
+	public void test_read_$CII_Exception() throws IOException {
+		br = new BufferedReader(new Support_StringReader(testString));
+		char[] nullCharArray = null;
+		char[] charArray = testString.toCharArray();
+		
+		try {
+			br.read(nullCharArray, -1, -1);
+			fail("should throw IndexOutOfBoundsException");
+		} catch (IndexOutOfBoundsException e) {
+			// expected
+		}
+		
+		try {
+			br.read(nullCharArray, -1, 0);
+			fail("should throw IndexOutOfBoundsException");
+		} catch (IndexOutOfBoundsException e) {
+			// expected
+		}
+
+		try {
+			br.read(nullCharArray, 0, -1);
+			fail("should throw NullPointerException");
+		} catch (NullPointerException e) {
+			// expected
+		}
+
+		try {
+			br.read(nullCharArray, 0, 0);
+			fail("should throw NullPointerException");
+		} catch (NullPointerException e) {
+			// expected
+		}
+		
+		try {
+			br.read(nullCharArray, 0, 1);
+			fail("should throw NullPointerException");
+		} catch (NullPointerException e) {
+			// expected
+		}
+		
+		try {
+			br.read(charArray, -1, -1);
+			fail("should throw IndexOutOfBoundsException");
+		} catch (IndexOutOfBoundsException e) {
+			// expected
+		}
+
+		try {
+			br.read(charArray, -1, 0);
+			fail("should throw IndexOutOfBoundsException");
+		} catch (IndexOutOfBoundsException e) {
+			// expected
+		}
+
+		br.read(charArray, 0, 0);
+        br.read(charArray, 0, charArray.length);
+        br.read(charArray, charArray.length, 0);
+		
+		try {
+			br.read(charArray, charArray.length + 1, 0);
+			fail("should throw IndexOutOfBoundsException");
+		} catch (IndexOutOfBoundsException e) {
+			//expected
+		}
+		
+		try {
+			br.read(charArray, charArray.length + 1, 1);
+			fail("should throw IndexOutOfBoundsException");
+		} catch (IndexOutOfBoundsException e) {
+			//expected
+		}
+
+		br.close();
+
+		try {
+			br.read(nullCharArray, -1, -1);
+			fail("should throw IOException");
+		} catch (IOException e) {
+			// expected
+		}
+
+		try {
+			br.read(charArray, -1, 0);
+			fail("should throw IOException");
+		} catch (IOException e) {
+			// expected
+		}
+
+		try {
+			br.read(charArray, 0, -1);
+			fail("should throw IOException");
+		} catch (IOException e) {
+			// expected
+		}
+	}
+	/**
 	 * @tests java.io.BufferedReader#readLine()
 	 */
 	public void test_readLine() {



Mime
View raw message