Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #9006 - WebSocket MessageInputStream.read() returns signed byte #9010

Merged
merged 1 commit into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -941,7 +941,7 @@ else if (_line.length() == 0)
_pos = 0;
}

return _buffer[_pos++];
return _buffer[_pos++] & 0xFF;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public int read() throws IOException
if (len < 0) // EOF
return -1;
if (len > 0) // did read something
return buf[0];
return buf[0] & 0xFF;
// reading nothing (len == 0) tries again
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTimeout;

Expand Down Expand Up @@ -279,4 +280,23 @@ public void testAppendNullPayloadRead() throws IOException
}
});
}

@Test
public void testReadSingleByteIsNotSigned() throws Exception
{
try (MessageInputStream stream = new MessageInputStream())
{
// Byte must be greater than 127.
int theByte = 200;
// Append a single message (simple, short)
Frame frame = new Frame(OpCode.BINARY);
frame.setPayload(new byte[]{(byte)theByte});
frame.setFin(true);
stream.accept(frame, Callback.NOOP);

// Single byte read must not return a signed byte.
int read = stream.read();
assertEquals(theByte, read);
}
}
}