Skip to content

Commit 2f7db83

Browse files
committed
Read only the expected byte instead the reported by remote
1 parent 6936334 commit 2f7db83

File tree

4 files changed

+11
-19
lines changed

4 files changed

+11
-19
lines changed

src/main/java/org/monora/coolsocket/core/config/Config.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ public final class Config
1313
/**
1414
*
1515
*/
16-
public static final int DATA_EXCHANGE_BYTE_SIZE = 8096;
16+
public static final int DATA_EXCHANGE_BYTE_SIZE = 8192;
1717
}

src/main/java/org/monora/coolsocket/core/session/ActiveConnection.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ public int read(Description description) throws IOException
434434
}
435435

436436
description.byteBuffer.clear();
437-
int length = (int) Math.min(description.byteBuffer.remaining(), description.nextAvailable);
437+
int length = (int) Math.min(description.byteBuffer.remaining(), Math.min(description.nextAvailable, description.available()));
438438
description.byteBuffer.limit(length);
439439
length = getReadableByteChannel().read(description.byteBuffer);
440440
description.byteBuffer.flip();
@@ -457,7 +457,7 @@ public int read(Description description) throws IOException
457457
*/
458458
public Description readBegin() throws IOException
459459
{
460-
return readBegin(new byte[8096]);
460+
return readBegin(new byte[8192]);
461461
}
462462

463463
/**
@@ -785,7 +785,7 @@ public synchronized void write(Description description, byte[] bytes, int offset
785785

786786
if (lengthActual < length)
787787
throw new SizeLimitExceededException("The length requested exceeds the data size reported to the remote. " +
788-
"The requested size has been written to the remote, but this is an error that should handled.",
788+
"The expected size has been written to the remote, but this is an error that should handled.",
789789
description.totalLength, description.consumedLength - lengthActual + length);
790790
}
791791

@@ -799,7 +799,7 @@ public synchronized void write(Description description, byte[] bytes, int offset
799799
public synchronized void write(Description description, InputStream inputStream) throws IOException
800800
{
801801
int len;
802-
byte[] buffer = new byte[8096];
802+
byte[] buffer = new byte[8192];
803803
while ((len = inputStream.read(buffer)) != -1) {
804804
write(description, buffer, 0, len);
805805
}
@@ -835,7 +835,7 @@ public synchronized Description writeBegin(long flags) throws IOException
835835
*/
836836
public synchronized Description writeBegin(long flags, long totalLength) throws IOException
837837
{
838-
ByteBuffer byteBuffer = ByteBuffer.allocate(8096);
838+
ByteBuffer byteBuffer = ByteBuffer.allocate(8192);
839839
int operationId = (int) (Integer.MAX_VALUE * Math.random());
840840
Description description = new Description(flags, operationId, totalLength, byteBuffer);
841841
byteBuffer.putLong(flags)
@@ -973,7 +973,7 @@ public Description(Flags flags, int operationId, long totalLength, ByteBuffer by
973973
if (byteBuffer == null)
974974
throw new NullPointerException("Buffer cannot be null.");
975975

976-
if (byteBuffer.capacity() < 8096)
976+
if (byteBuffer.capacity() < 8192)
977977
throw new BufferUnderflowException();
978978

979979
if (totalLength < 0)

src/test/java/org/monora/coolsocket/core/CommunicationTest.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class CommunicationTest
1515
public void speedTest() throws IOException, InterruptedException
1616
{
1717
final int repeat = 100000;
18-
final byte[] data = new byte[8096];
18+
final byte[] data = new byte[8192];
1919

2020
Arrays.fill(data, (byte) 2);
2121

@@ -43,10 +43,7 @@ public void onConnected(ActiveConnection activeConnection)
4343
long startTime = System.nanoTime();
4444
ActiveConnection.Description description = activeConnection.readBegin();
4545
do {
46-
int len = activeConnection.read(description);
47-
if (len > 0) {
48-
49-
}
46+
activeConnection.read(description);
5047
} while (description.hasAvailable());
5148
System.out.println("It took: " + ((System.nanoTime() - startTime) / 1e9));
5249
} finally {

src/test/java/org/monora/coolsocket/core/DataTransactionTest.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public void onConnected(ActiveConnection activeConnection)
7878
{
7979
try {
8080
ActiveConnection.Description description = activeConnection.writeBegin(0);
81-
activeConnection.write(description, new byte[8096]);
81+
activeConnection.write(description, new byte[8192]);
8282
activeConnection.writeEnd(description);
8383
} catch (IOException ignored) {
8484

@@ -102,7 +102,7 @@ public void onConnected(ActiveConnection activeConnection)
102102
@Test
103103
public void internalCacheLimitTest() throws InterruptedException, IOException
104104
{
105-
final int size = 8096;
105+
final int size = 8192;
106106

107107
CoolSocket coolSocket = new CoolSocket(PORT)
108108
{
@@ -292,17 +292,12 @@ public void onConnected(ActiveConnection activeConnection)
292292
try {
293293
byte[] base = message.substring(0, message.length() - 1).getBytes();
294294
byte[] end = message.substring(message.length() - 1).getBytes();
295-
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(base.length);
296-
byteArrayOutputStream.write(base);
297-
byteArrayOutputStream.write(end);
298-
299295

300296
ActiveConnection.Description description = activeConnection.writeBegin(0, bytes.length);
301297
activeConnection.write(description, base);
302298
activeConnection.write(description, end);
303299
activeConnection.writeEnd(description);
304300
} catch (SizeLimitExceededException ignored) {
305-
ignored.printStackTrace();
306301
} catch (IOException e) {
307302
e.printStackTrace();
308303
}

0 commit comments

Comments
 (0)