Skip to content

Commit fe884fc

Browse files
committed
Fixed reading unsigned byte
1 parent 01b974c commit fe884fc

File tree

3 files changed

+36
-7
lines changed

3 files changed

+36
-7
lines changed

driver/src/main/java/org/neo4j/driver/internal/connector/socket/BufferingChunkedInput.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ int remainingChunkSize()
9696
return remainingChunkSize;
9797
}
9898

99+
99100
/**
100101
* Internal state machine used for reading data from the channel into the buffer.
101102
*/
@@ -126,7 +127,7 @@ else if ( ctx.buffer.remaining() >= 2 )
126127
{
127128
//only 1 byte in buffer, read that and continue
128129
//to read header
129-
byte partialChunkSize = ctx.buffer.get();
130+
int partialChunkSize = getUnsignedByteFromBuffer( ctx.buffer );
130131
ctx.remainingChunkSize = partialChunkSize << 8;
131132
return IN_HEADER.readChunkSize( ctx );
132133
}
@@ -391,6 +392,12 @@ public byte peekByte() throws IOException
391392
return buffer.get( buffer.position() );
392393
}
393394

395+
static int getUnsignedByteFromBuffer( ByteBuffer buffer )
396+
{
397+
return buffer.get() & 0xFF;
398+
}
399+
400+
394401
private boolean hasMoreDataUnreadInCurrentChunk()
395402
{
396403
return remainingChunkSize > 0;

driver/src/test/java/org/neo4j/driver/internal/connector/socket/BufferingChunkedInputTest.java

+23-1
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,35 @@ public void shouldReadChunkWithSplitHeaderForBigMessages() throws IOException
104104
assertThat( input.readByte(), equalTo( (byte) 1 ) );
105105
assertThat( input.remainingChunkSize(), equalTo( packetSize - 1 ) );
106106

107-
for ( int i = 1; i < 384; i++ )
107+
for ( int i = 1; i < packetSize; i++ )
108108
{
109109
assertThat( input.readByte(), equalTo( (byte) 1 ) );
110110
}
111111
assertThat( input.remainingChunkSize(), equalTo( 0 ) );
112112
}
113113

114+
@Test
115+
public void shouldReadChunkWithSplitHeaderForBigMessagesWhenInternalBufferHasOneByte() throws IOException
116+
{
117+
// Given
118+
int packetSize = 32780;
119+
BufferingChunkedInput input =
120+
new BufferingChunkedInput( packets( packet( -128 ), packet( 12 ), fillPacket( packetSize, 1 ) ), 1);
121+
122+
// Then
123+
assertThat( input.readByte(), equalTo( (byte) 1 ) );
124+
assertThat( input.remainingChunkSize(), equalTo( packetSize - 1 ) );
125+
}
126+
127+
@Test
128+
public void shouldReadUnsignedByteFromBuffer() throws IOException
129+
{
130+
ByteBuffer buffer = ByteBuffer.allocate( 1 );
131+
buffer.put( (byte) -1 );
132+
buffer.flip();
133+
assertThat(BufferingChunkedInput.getUnsignedByteFromBuffer( buffer ), equalTo( 255 ));
134+
}
135+
114136
@Test
115137
public void shouldReadOneByteInOneChunkWhenBustingBuffer() throws IOException
116138
{

driver/src/test/java/org/neo4j/driver/internal/connector/socket/ChunkedInputTest.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
*/
1919
package org.neo4j.driver.internal.connector.socket;
2020

21+
import org.junit.Rule;
22+
import org.junit.Test;
23+
import org.junit.rules.ExpectedException;
24+
import org.mockito.Matchers;
25+
2126
import java.io.ByteArrayInputStream;
2227
import java.io.IOException;
2328
import java.nio.ByteBuffer;
@@ -26,11 +31,6 @@
2631
import java.nio.channels.ReadableByteChannel;
2732
import java.util.Arrays;
2833

29-
import org.junit.Rule;
30-
import org.junit.Test;
31-
import org.junit.rules.ExpectedException;
32-
import org.mockito.Matchers;
33-
3434
import org.neo4j.driver.v1.exceptions.ClientException;
3535
import org.neo4j.driver.v1.util.RecordingByteChannel;
3636

0 commit comments

Comments
 (0)