Skip to content

Commit

Permalink
was not handling the case when -1 was in binary data. had to deal wit…
Browse files Browse the repository at this point in the history
…h 2s-compliment
  • Loading branch information
jmazzitelli committed Aug 10, 2015
1 parent 45ef308 commit 35d6c1c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void setOnCloseAction(Runnable action) {

public int read() throws IOException {
if (unreadInMemoryDataExists()) {
return (int) inMemoryData[inMemoryDataPointer++];
return (inMemoryData[inMemoryDataPointer++] & 0xff);
} else {
return streamData.read();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.hawkular.bus.common;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
Expand All @@ -27,6 +28,46 @@
@SuppressWarnings("resource")
public class BinaryDataTest {

@Test
public void testBinaryWithNegativeOneValues() throws Exception {
ByteArrayOutputStream boas = new ByteArrayOutputStream();
boas.write(-1);
boas.write('a');
boas.write('b');
boas.write('c');
boas.write(-1);
boas.write(-1);
boas.write(-1);
boas.write('x');
boas.write('y');
boas.write('z');
boas.write(-1);
boas.close();

final int totalDataSize = 11; // the number of bytes we wrote into our test string

ByteArrayInputStream emptyIn = new ByteArrayInputStream(new byte[0]);
BinaryData bd = new BinaryData(boas.toByteArray(), emptyIn);
Assert.assertEquals(totalDataSize, bd.available());

byte[] readArray = new byte[totalDataSize];
Assert.assertEquals(totalDataSize, bd.read(readArray));
int i = 0;
Assert.assertEquals(-1, readArray[i++]);
Assert.assertEquals('a', readArray[i++]);
Assert.assertEquals('b', readArray[i++]);
Assert.assertEquals('c', readArray[i++]);
Assert.assertEquals(-1, readArray[i++]);
Assert.assertEquals(-1, readArray[i++]);
Assert.assertEquals(-1, readArray[i++]);
Assert.assertEquals('x', readArray[i++]);
Assert.assertEquals('y', readArray[i++]);
Assert.assertEquals('z', readArray[i++]);
Assert.assertEquals(-1, readArray[i++]);

return;
}

@Test
public void testEmptyBinaryData() throws Exception {
BinaryData binaryData;
Expand Down

0 comments on commit 35d6c1c

Please sign in to comment.