Index: src/test/java/tests/api/java/io/DataInputStreamTest.java =================================================================== --- src/test/java/tests/api/java/io/DataInputStreamTest.java (revision 443538) +++ src/test/java/tests/api/java/io/DataInputStreamTest.java (working copy) @@ -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. @@ -201,7 +201,242 @@ fail("IOException during readFully test : " + e.getMessage()); } } + + /** + * @tests java.io.DataInputStream#readFully(byte[], int, int) + */ + public void test_readFully$BII_Exception() throws IOException { + DataInputStream is = new DataInputStream(new ByteArrayInputStream(new byte[fileString.length()])); + byte[] byteArray = new byte[fileString.length()]; + + try { + is.readFully(byteArray, -1, -1); + fail("should throw IndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + try { + is.readFully(byteArray, 0, -1); + fail("should throw IndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + try { + is.readFully(byteArray, 1, -1); + fail("should throw IndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + is.readFully(byteArray, -1, 0); + is.readFully(byteArray, 0, 0); + is.readFully(byteArray, 1, 0); + + try { + is.readFully(byteArray, -1, 1); + fail("should throw IndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + is.readFully(byteArray, 0, 1); + is.readFully(byteArray, 1, 1); + try { + is.readFully(byteArray, 0, Integer.MAX_VALUE); + fail("should throw IndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + } + + /** + * @tests java.io.DataInputStream#readFully(byte[], int, int) + */ + public void test_readFully$BII_NullArray() throws IOException { + DataInputStream is = new DataInputStream(new ByteArrayInputStream(new byte[fileString.length()])); + + byte[] nullByteArray = null; + + try { + is.readFully(nullByteArray, -1, -1); + fail("should throw IndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + try { + is.readFully(nullByteArray, 0, -1); + fail("should throw IndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + try { + is.readFully(nullByteArray, 1, -1); + fail("should throw IndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + is.readFully(nullByteArray, -1, 0); + is.readFully(nullByteArray, 0, 0); + is.readFully(nullByteArray, 1, 0); + + try { + is.readFully(nullByteArray, -1, 1); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + + try { + is.readFully(nullByteArray, 0, 1); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + + try { + is.readFully(nullByteArray, 1, 1); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + + try { + is.readFully(nullByteArray, 0, Integer.MAX_VALUE); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + } + + /** + * @tests java.io.DataInputStream#readFully(byte[], int, int) + */ + public void test_readFully$BII_NullStream() throws IOException { + DataInputStream is = new DataInputStream(null); + byte[] byteArray = new byte[fileString.length()]; + + try { + is.readFully(byteArray, -1, -1); + fail("should throw IndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + try { + is.readFully(byteArray, 0, -1); + fail("should throw IndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + try { + is.readFully(byteArray, 1, -1); + fail("should throw IndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + is.readFully(byteArray, -1, 0); + is.readFully(byteArray, 0, 0); + is.readFully(byteArray, 1, 0); + + try { + is.readFully(byteArray, -1, 1); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + + try { + is.readFully(byteArray, 0, 1); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + + try { + is.readFully(byteArray, 1, 1); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + + try { + is.readFully(byteArray, 0, Integer.MAX_VALUE); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + } + + /** + * @tests java.io.DataInputStream#readFully(byte[], int, int) + */ + public void test_readFully$BII_NullStream_NullArray() throws IOException { + DataInputStream is = new DataInputStream(null); + byte[] nullByteArray = null; + + try { + is.readFully(nullByteArray, -1, -1); + fail("should throw IndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + try { + is.readFully(nullByteArray, 0, -1); + fail("should throw IndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + try { + is.readFully(nullByteArray, 1, -1); + fail("should throw IndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + is.readFully(nullByteArray, -1, 0); + is.readFully(nullByteArray, 0, 0); + is.readFully(nullByteArray, 1, 0); + + try { + is.readFully(nullByteArray, -1, 1); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + + try { + is.readFully(nullByteArray, 0, 1); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + + try { + is.readFully(nullByteArray, 1, 1); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + + try { + is.readFully(nullByteArray, 0, Integer.MAX_VALUE); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + + } + /** * @tests java.io.DataInputStream#readInt() */ @@ -385,8 +620,11 @@ protected void tearDown() { try { os.close(); - dis.close(); } catch (Exception e) { } + try { + dis.close(); + } catch (Exception e) { + } } } Index: src/main/java/java/io/DataInputStream.java =================================================================== --- src/main/java/java/io/DataInputStream.java (revision 443538) +++ src/main/java/java/io/DataInputStream.java (working copy) @@ -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. @@ -189,33 +189,52 @@ readFully(buffer, 0, buffer.length); } - /* - * (non-Javadoc) - * - * @see java.io.DataInput#readFully(byte[], int, int) - */ - public final void readFully(byte[] buffer, int offset, int length) - throws IOException { - if (buffer != null) { - // avoid int overflow - if (0 <= offset && offset <= buffer.length && 0 <= length - && length <= buffer.length - offset) { - while (length > 0) { - int result = in.read(buffer, offset, length); - if (result >= 0) { - offset += result; - length -= result; - } else { - throw new EOFException(); - } - } - } else { - throw new IndexOutOfBoundsException(); - } - } else { + /** + * Reads bytes from this stream and stores them in the byte array + * buffer starting at the position offset. + * This method blocks until count bytes have been read. + * + * @param buffer + * the byte array into which the data is read + * @param offset + * the offset the operation start at + * @param count + * the maximum number of bytes to read + * + * @throws IOException + * if a problem occurs while reading from this stream + * @throws EOFException + * if reaches the end of the stream before enough bytes have + * been read + * @see java.io.DataInput#readFully(byte[], int, int) + */ + public final void readFully(byte[] buffer, int offset, int length) + throws IOException { + if (length < 0) { + throw new IndexOutOfBoundsException(); + } + if (length == 0) { + return; + } + if (in == null) { + throw new NullPointerException(Msg.getString("KA00b")); //$NON-NLS-1$ + } + if (buffer == null) { throw new NullPointerException(Msg.getString("K0047")); //$NON-NLS-1$ } - } + if (offset < 0 || offset > buffer.length - length) { + throw new IndexOutOfBoundsException(); + } + while (length > 0) { + int result = in.read(buffer, offset, length); + if (result >= 0) { + offset += result; + length -= result; + } else { + throw new EOFException(); + } + } + } /** * Reads a 32-bit integer value from this stream.