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 2014/04/12 19:05:57 UTC
svn commit: r1586879 - in /commons/proper/compress/trunk/src:
changes/changes.xml main/java/org/apache/commons/compress/utils/IOUtils.java
test/java/org/apache/commons/compress/utils/IOUtilsTest.java
Author: bodewig
Date: Sat Apr 12 17:05:57 2014
New Revision: 1586879
URL: http://svn.apache.org/r1586879
Log:
COMPRESS-277 make sure IOUtils#skip deals with streams that return 0 on skip even though more could be read
Added:
commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/utils/IOUtilsTest.java (with props)
Modified:
commons/proper/compress/trunk/src/changes/changes.xml
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/utils/IOUtils.java
Modified: commons/proper/compress/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/changes/changes.xml?rev=1586879&r1=1586878&r2=1586879&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/changes/changes.xml (original)
+++ commons/proper/compress/trunk/src/changes/changes.xml Sat Apr 12 17:05:57 2014
@@ -55,6 +55,10 @@ The <action> type attribute can be add,u
CompressorStreamFactory can now auto-detect Unix compress
(".Z") streams.
</action>
+ <action type="add" date="2014-04-12" issue="COMPRESS-277">
+ IOUtils#skip might skip fewer bytes than requested even though
+ more could be read from the stream.
+ </action>
</release>
<release version="1.8" date="2014-03-12"
description="Release 1.8">
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=1586879&r1=1586878&r2=1586879&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 Sat Apr 12 17:05:57 2014
@@ -30,6 +30,9 @@ import java.io.OutputStream;
*/
public final class IOUtils {
+ private static final int COPY_BUF_SIZE = 8024;
+ private static final int SKIP_BUF_SIZE = 4096;
+
/** Private constructor to prevent instantiation of this utility class. */
private IOUtils(){
}
@@ -46,7 +49,7 @@ public final class IOUtils {
* if an error occurs
*/
public static long copy(final InputStream input, final OutputStream output) throws IOException {
- return copy(input, output, 8024);
+ return copy(input, output, COPY_BUF_SIZE);
}
/**
@@ -76,6 +79,10 @@ public final class IOUtils {
* Skips the given number of bytes by repeatedly invoking skip on
* the given input stream if necessary.
*
+ * <p>In a case where the stream's skip() method returns 0 before
+ * the requested number of bytes has been skip this implementation
+ * will fall back to using the read() method.</p>
+ *
* <p>This method will only skip less than the requested number of
* bytes if the end of the input stream has been reached.</p>
*
@@ -93,6 +100,18 @@ public final class IOUtils {
}
numToSkip -= skipped;
}
+
+ if (numToSkip > 0) {
+ byte[] skipBuf = new byte[SKIP_BUF_SIZE];
+ while (numToSkip > 0) {
+ int read = readFully(input, skipBuf, 0,
+ (int) Math.min(numToSkip, SKIP_BUF_SIZE));
+ if (read < 1) {
+ break;
+ }
+ numToSkip -= read;
+ }
+ }
return available - numToSkip;
}
Added: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/utils/IOUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/utils/IOUtilsTest.java?rev=1586879&view=auto
==============================================================================
--- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/utils/IOUtilsTest.java (added)
+++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/utils/IOUtilsTest.java Sat Apr 12 17:05:57 2014
@@ -0,0 +1,84 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.compress.utils;
+
+import java.io.ByteArrayInputStream;
+import java.io.FilterInputStream;
+import java.io.InputStream;
+import java.io.IOException;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class IOUtilsTest {
+
+ private interface StreamWrapper {
+ InputStream wrap(InputStream toWrap);
+ }
+
+ @Test
+ public void skipUsingSkip() throws Exception {
+ skip(new StreamWrapper() {
+ public InputStream wrap(InputStream toWrap) {
+ return toWrap;
+ }
+ });
+ }
+
+ @Test
+ public void skipUsingRead() throws Exception {
+ skip(new StreamWrapper() {
+ public InputStream wrap(InputStream toWrap) {
+ return new FilterInputStream(toWrap) {
+ public long skip(long s) {
+ return 0;
+ }
+ };
+ }
+ });
+ }
+
+ @Test
+ public void skipUsingSkipAndRead() throws Exception {
+ skip(new StreamWrapper() {
+ public InputStream wrap(final InputStream toWrap) {
+ return new FilterInputStream(toWrap) {
+ boolean skipped;
+ public long skip(long s) throws IOException {
+ if (!skipped) {
+ toWrap.skip(5);
+ skipped = true;
+ return 5;
+ }
+ return 0;
+ }
+ };
+ }
+ });
+ }
+
+ private void skip(StreamWrapper wrapper) throws Exception {
+ ByteArrayInputStream in = new ByteArrayInputStream(new byte[] {
+ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
+ });
+ InputStream sut = wrapper.wrap(in);
+ Assert.assertEquals(10, IOUtils.skip(sut, 10));
+ Assert.assertEquals(11, sut.read());
+ }
+
+}
Propchange: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/utils/IOUtilsTest.java
------------------------------------------------------------------------------
svn:eol-style = native