You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by se...@apache.org on 2020/08/10 20:40:40 UTC

[commons-io] branch master updated: IO-683 failure to convert byte to unsigned

This is an automated email from the ASF dual-hosted git repository.

sebb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git


The following commit(s) were added to refs/heads/master by this push:
     new a521b43  IO-683 failure to convert byte to unsigned
a521b43 is described below

commit a521b43e774a92f4c07461536ffc810715c762d8
Author: Sebb <se...@apache.org>
AuthorDate: Mon Aug 10 21:40:29 2020 +0100

    IO-683 failure to convert byte to unsigned
    
    CircularBufferInputStream.read() fails to convert byte to unsigned int
---
 src/changes/changes.xml                            |  3 +++
 .../io/input/buffer/CircularBufferInputStream.java |  2 +-
 .../buffer/CircularBufferInputStreamTest.java      | 22 ++++++++++++++++++++++
 3 files changed, 26 insertions(+), 1 deletion(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index c8ab544..5f340e2 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -128,6 +128,9 @@ The <action> type attribute can be add,update,fix,remove.
       <action dev="ggregory" type="update" due-to="Dependabot">
         Update junit-pioneer from 0.8.0 to 0.9.0 #138.
       </action>
+      <action issue="IO-683" dev="sebb" type="fix">
+        CircularBufferInputStream.read() fails to convert byte to unsigned int
+      </action>
     </release>
     <!-- The release date is the date RC is cut -->
     <release version="2.7" date="2020-05-24" description="Java 8 required.">
diff --git a/src/main/java/org/apache/commons/io/input/buffer/CircularBufferInputStream.java b/src/main/java/org/apache/commons/io/input/buffer/CircularBufferInputStream.java
index 8438a15..f797f3d 100644
--- a/src/main/java/org/apache/commons/io/input/buffer/CircularBufferInputStream.java
+++ b/src/main/java/org/apache/commons/io/input/buffer/CircularBufferInputStream.java
@@ -104,7 +104,7 @@ public class CircularBufferInputStream extends InputStream {
         if (!haveBytes(1)) {
             return -1;
         }
-        return buffer.read();
+        return buffer.read() & 0xFF; // return unsigned byte
     }
 
     @Override
diff --git a/src/test/java/org/apache/commons/io/input/buffer/CircularBufferInputStreamTest.java b/src/test/java/org/apache/commons/io/input/buffer/CircularBufferInputStreamTest.java
index 2022cba..66b13aa 100644
--- a/src/test/java/org/apache/commons/io/input/buffer/CircularBufferInputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/input/buffer/CircularBufferInputStreamTest.java
@@ -16,7 +16,11 @@
  */
 package org.apache.commons.io.input.buffer;
 
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
 import java.io.ByteArrayInputStream;
+import java.io.IOException;
 import java.util.Random;
 
 import org.junit.jupiter.api.Test;
@@ -70,6 +74,24 @@ public class CircularBufferInputStreamTest {
 				throw new IllegalStateException("Unexpected random choice value");
 			}
 		}
+		assertTrue(true, "Test finished OK");
+	}
+
+	@Test
+  public void testIO683() throws IOException {
+		final byte[] buffer = new byte[]{0,1,-2,-2,-1,4};
+		try (
+			final ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
+			final CircularBufferInputStream cbis = new CircularBufferInputStream(bais);
+		){
+			int b;
+			int i = 0;
+			while((b = cbis.read()) != -1) {
+				assertEquals(buffer[i] & 0xFF,b, "byte at index " + i + " should be equal");
+				i++;
+			}
+			assertEquals(buffer.length, i, "Should have read all the bytes");
+		}
 	}
 
 	/**