forked from shyiko/mysql-binlog-connector-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request shyiko#34 from ramanenka/optimize-read-array-in-by…
…tearrayinputstream Optimize read operation on ByteArrayInputStream
- Loading branch information
Showing
2 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
src/test/java/com/github/shyiko/mysql/binlog/io/ByteArrayInputStreamTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package com.github.shyiko.mysql.binlog.io; | ||
|
||
import org.testng.annotations.Test; | ||
|
||
import static org.testng.Assert.assertEquals; | ||
|
||
public class ByteArrayInputStreamTest { | ||
@Test | ||
public void testReadToArray() throws Exception { | ||
byte[] buff = new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; | ||
ByteArrayInputStream in = new ByteArrayInputStream(buff); | ||
assertEquals(in.getPosition(), 0); | ||
|
||
byte[] b = new byte[20]; | ||
|
||
int read = in.read(b, 0, 0); | ||
assertEquals(read, 0); | ||
assertEquals(in.getPosition(), 0); | ||
|
||
read = in.read(b, 0, 4); | ||
assertEquals(read, 4); | ||
assertEquals(b[3], 3); | ||
assertEquals(in.getPosition(), 4); | ||
|
||
read = in.read(b, 4, 4); | ||
assertEquals(read, 4); | ||
assertEquals(b[7], 7); | ||
assertEquals(in.getPosition(), 8); | ||
|
||
read = in.read(b, 8, 4); | ||
assertEquals(read, 4); | ||
assertEquals(b[11], 11); | ||
assertEquals(in.getPosition(), 12); | ||
|
||
read = in.read(b, 12, 4); | ||
assertEquals(read, 4); | ||
assertEquals(b[15], 15); | ||
assertEquals(in.getPosition(), 16); | ||
|
||
read = in.read(b, 16, 4); | ||
assertEquals(read, -1); | ||
assertEquals(in.getPosition(), 16); | ||
} | ||
|
||
@Test | ||
public void testReadToArrayWithinBlockBoundaries() throws Exception { | ||
byte[] buff = new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8}; | ||
ByteArrayInputStream in = new ByteArrayInputStream(buff); | ||
byte[] b = new byte[8]; | ||
|
||
in.enterBlock(4); | ||
|
||
int read = in.read(b, 0, 3); | ||
assertEquals(read, 3); | ||
assertEquals(b[2], 2); | ||
|
||
read = in.read(b, 3, 3); | ||
assertEquals(read, 1); | ||
assertEquals(b[3], 3); | ||
|
||
read = in.read(b, 4, 3); | ||
assertEquals(read, -1); | ||
} | ||
|
||
@Test(expectedExceptions = NullPointerException.class) | ||
public void testReadToArrayWithNullBuff() throws Exception { | ||
ByteArrayInputStream in = new ByteArrayInputStream(new byte[]{}); | ||
in.read(null, 0, 4); | ||
} | ||
|
||
@Test(expectedExceptions = IndexOutOfBoundsException.class) | ||
public void testReadToArrayWhenLenExceedsBuffSize() throws Exception { | ||
ByteArrayInputStream in = new ByteArrayInputStream(new byte[]{0, 1, 2}); | ||
byte[] b = new byte[1]; | ||
in.read(b, 0, 4); | ||
} | ||
|
||
@Test(expectedExceptions = IndexOutOfBoundsException.class) | ||
public void testReadToArrayWhenOffsetNegative() throws Exception { | ||
ByteArrayInputStream in = new ByteArrayInputStream(new byte[]{0, 1, 2}); | ||
byte[] b = new byte[1]; | ||
in.read(b, -1, 1); | ||
} | ||
|
||
@Test(expectedExceptions = IndexOutOfBoundsException.class) | ||
public void testReadToArrayWhenLengthNegative() throws Exception { | ||
ByteArrayInputStream in = new ByteArrayInputStream(new byte[]{0, 1, 2}); | ||
byte[] b = new byte[1]; | ||
in.read(b, 0, -1); | ||
} | ||
|
||
@Test | ||
public void testPeekAndReadToArray() throws Exception { | ||
ByteArrayInputStream in = new ByteArrayInputStream(new byte[]{5, 6, 7}); | ||
byte[] b = new byte[3]; | ||
assertEquals(in.peek(), 5); | ||
int read = in.read(b, 0, 3); | ||
assertEquals(read, 3); | ||
assertEquals(b[0], 5); | ||
assertEquals(b[2], 7); | ||
} | ||
} |