failure to convert byte to unsigned
CircularBufferInputStream.read() fails to convert byte to unsigned int
MODIFY
src/changes/changes.xml
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.">

MODIFY
src/main/java/org/apache/commons/io/input/buffer/CircularBufferInputStream.java
src/main/java/org/apache/commons/io/input/buffer/CircularBufferInputStream.java
@@ -104,7 +104,7 @@ public int read() throws IOException {
if (!haveBytes(1)) {
return -1;
}
- return buffer.read();
+ return buffer.read() & 0xFF; // return unsigned byte
}
@Override

MODIFY
src/test/java/org/apache/commons/io/input/buffer/CircularBufferInputStreamTest.java
src/test/java/org/apache/commons/io/input/buffer/CircularBufferInputStreamTest.java
*/
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 void testRandomRead() throws Exception {
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");
+ }
}
/**

IO-683 failure to convert byte to unsigned

CircularBufferInputStream.read() fails to convert byte to unsigned int