You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by bo...@apache.org on 2013/10/04 15:17:32 UTC
svn commit: r1529157 - in
/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress:
archivers/cpio/CpioArchiveInputStream.java
archivers/dump/TapeInputStream.java
archivers/zip/ZipArchiveInputStream.java utils/IOUtils.java
Author: bodewig
Date: Fri Oct 4 13:17:32 2013
New Revision: 1529157
URL: http://svn.apache.org/r1529157
Log:
one readFully method to rule them all
Modified:
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStream.java
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/TapeInputStream.java
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/utils/IOUtils.java
Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStream.java?rev=1529157&r1=1529156&r2=1529157&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStream.java (original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStream.java Fri Oct 4 13:17:32 2013
@@ -28,6 +28,7 @@ import org.apache.commons.compress.archi
import org.apache.commons.compress.archivers.zip.ZipEncodingHelper;
import org.apache.commons.compress.utils.ArchiveUtils;
import org.apache.commons.compress.utils.CharsetNames;
+import org.apache.commons.compress.utils.IOUtils;
/**
* CPIOArchiveInputStream is a stream for reading cpio streams. All formats of
@@ -330,19 +331,12 @@ public class CpioArchiveInputStream exte
private final int readFully(final byte[] b, final int off, final int len)
throws IOException {
- if (len < 0) {
- throw new IndexOutOfBoundsException();
- }
- int n = 0;
- while (n < len) {
- int count = this.in.read(b, off + n, len - n);
- count(count);
- if (count < 0) {
- throw new EOFException();
- }
- n += count;
+ int count = IOUtils.readFully(in, b, off, len);
+ count(count);
+ if (count < len) {
+ throw new EOFException();
}
- return n;
+ return count;
}
private long readBinaryLong(final int length, final boolean swapHalfWord)
Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/TapeInputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/TapeInputStream.java?rev=1529157&r1=1529156&r2=1529157&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/TapeInputStream.java (original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/TapeInputStream.java Fri Oct 4 13:17:32 2013
@@ -26,6 +26,7 @@ import java.util.Arrays;
import java.util.zip.DataFormatException;
import java.util.zip.Inflater;
+import org.apache.commons.compress.utils.IOUtils;
/**
* Filter stream that mimics a physical tape drive capable of compressing
@@ -333,16 +334,9 @@ class TapeInputStream extends FilterInpu
*/
private boolean readFully(byte[] b, int off, int len)
throws IOException {
- int count = 0;
-
- while (count < len) {
- int n = in.read(b, off + count, len - count);
-
- if (n == -1) {
- throw new ShortFileException();
- }
-
- count += n;
+ int count = IOUtils.readFully(in, b, off, len);
+ if (count < len) {
+ throw new ShortFileException();
}
return true;
Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java?rev=1529157&r1=1529156&r2=1529157&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java (original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java Fri Oct 4 13:17:32 2013
@@ -32,6 +32,7 @@ import java.util.zip.ZipException;
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.ArchiveInputStream;
+import org.apache.commons.compress.utils.IOUtils;
import static org.apache.commons.compress.archivers.zip.ZipConstants.DWORD;
import static org.apache.commons.compress.archivers.zip.ZipConstants.SHORT;
@@ -674,13 +675,10 @@ public class ZipArchiveInputStream exten
}
private void readFully(byte[] b) throws IOException {
- int count = 0, x = 0;
- while (count != b.length) {
- count += x = in.read(b, count, b.length - count);
- if (x == -1) {
- throw new EOFException();
- }
- count(x);
+ int count = IOUtils.readFully(in, b);
+ count(count);
+ if (count < b.length) {
+ throw new EOFException();
}
}
Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/utils/IOUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/utils/IOUtils.java?rev=1529157&r1=1529156&r2=1529157&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/utils/IOUtils.java (original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/utils/IOUtils.java Fri Oct 4 13:17:32 2013
@@ -77,7 +77,7 @@ public final class IOUtils {
*
* <p>This method will only skip less than the requested number of
* bytes if the end of the input stream has been reached.</p>
-
+ *
* @param input stream to skip bytes in
* @param numToSkip the number of bytes to skip
* @return the number of bytes actually skipped
@@ -95,6 +95,51 @@ public final class IOUtils {
return (available - numToSkip);
}
+ /**
+ * Reads as much from input as possible to fill the given array.
+ *
+ * <p>This method may invoke read repeatedly to fill the array and
+ * only read less bytes than the length of the array if the end of
+ * the stream has been reached.</p>
+ *
+ * @param input stream to read from
+ * @param b buffer to fill
+ * @retun the number of bytes actually read
+ */
+ public static int readFully(InputStream input, byte[] b) throws IOException {
+ return readFully(input, b, 0, b.length);
+ }
+
+ /**
+ * Reads as much from input as possible to fill the given array
+ * with the given amount of bytes.
+ *
+ * <p>This method may invoke read repeatedly to read the bytes and
+ * only read less bytes than the requested length if the end of
+ * the stream has been reached.</p>
+ *
+ * @param input stream to read from
+ * @param b buffer to fill
+ * @param offset offset into the buffer to start filling at
+ * @param amount of bytes to read
+ * @retun the number of bytes actually read
+ */
+ public static int readFully(InputStream input, byte[] b, int offset, int len)
+ throws IOException {
+ if (len < 0 || offset < 0 || len + offset > b.length) {
+ throw new IndexOutOfBoundsException();
+ }
+ int count = 0, x = 0;
+ while (count != len) {
+ x = input.read(b, offset + count, len - count);
+ if (x == -1) {
+ break;
+ }
+ count += x;
+ }
+ return count;
+ }
+
// toByteArray(InputStream) copied from:
// commons/proper/io/trunk/src/main/java/org/apache/commons/io/IOUtils.java?revision=1428941
// January 8th, 2013