]> source.dussan.org Git - jgit.git/commitdiff
IO#readFully: provide overload that fills the full array 89/197589/14
authorAnna Papitto <annapapitto@google.com>
Fri, 2 Dec 2022 23:56:56 +0000 (15:56 -0800)
committerAnna Papitto <annapapitto@google.com>
Mon, 19 Dec 2022 18:26:41 +0000 (10:26 -0800)
IO#readFully is often called with the intent to fill the destination
array from beginning to end. The redundant arguments for where to start
and stop filling are opportunities for bugs if specified incorrectly or
if not changed to match a changed array length.

Provide a overloaded method for filling the full destination array.

Change-Id: I964f18f4a061189cce1ca00ff0258669277ff499
Signed-off-by: Anna Papitto <annapapitto@google.com>
org.eclipse.jgit.test/tst/org/eclipse/jgit/util/io/TimeoutInputStreamTest.java
org.eclipse.jgit/src/org/eclipse/jgit/util/IO.java

index 648416925ce4f0b2d8f03ffd5d4d07ce4b383c4f..76bda6a35b380ad0ecc396a22cefe1ad9933f90b 100644 (file)
@@ -91,7 +91,7 @@ public class TimeoutInputStreamTest {
                final byte[] exp = new byte[] { 'a', 'b', 'c' };
                final byte[] act = new byte[exp.length];
                out.write(exp);
-               IO.readFully(is, act, 0, act.length);
+               IO.readFully(is, act);
                assertArrayEquals(exp, act);
        }
 
@@ -110,7 +110,7 @@ public class TimeoutInputStreamTest {
        public void testTimeout_readBuffer_Timeout() throws IOException {
                beginRead();
                try {
-                       IO.readFully(is, new byte[512], 0, 512);
+                       IO.readFully(is, new byte[512]);
                        fail("incorrectly read bytes");
                } catch (InterruptedIOException e) {
                        // expected
index 6d5694e435f11a5213be8f7386521ac1432772f6..80877bbdc67690534882c656ac8d348d4cb0046e 100644 (file)
@@ -206,6 +206,25 @@ public class IO {
                }
        }
 
+       /**
+        * Read from input until the entire byte array filled, or throw an exception
+        * if stream ends first.
+        *
+        * @param fd
+        *            input stream to read the data from.
+        * @param dst
+        *            buffer that must be fully populated
+        * @throws EOFException
+        *             the stream ended before dst was fully populated.
+        * @throws java.io.IOException
+        *             there was an error reading from the stream.
+        * @since 6.5
+        */
+       public static void readFully(InputStream fd, byte[] dst)
+                       throws IOException {
+               readFully(fd, dst, 0, dst.length);
+       }
+
        /**
         * Read as much of the array as possible from a channel.
         *