Skip to content

Commit

Permalink
GEODE-7864: Override the default implementation of write(byte[],int,i…
Browse files Browse the repository at this point in the history
…nt) (apache#4871)

* Prevent overheads of writing one byte at a time.
  • Loading branch information
nabarunnag authored May 28, 2020
1 parent dd990e8 commit bd2d118
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ public void write(int b) throws IOException {
}
}

@Override
public void write(byte[] b, int off, int len) throws IOException {
super.write(b, off, len);
OutputStream os = this.branch;
if (os != null) {
os.write(b, off, len);
}
}

@Override
public void flush() throws IOException {
super.flush();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2163,6 +2163,11 @@ public static void writeSerializableObject(Object o, DataOutput out) throws IOEx
public void write(int b) throws IOException {
out2.write(b);
}

@Override
public void write(byte[] b, int off, int len) throws IOException {
out2.write(b, off, len);
}
};
}
boolean wasDoNotCopy = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ public void write(int b) throws IOException {
}
}

@Override
public void write(byte[] b, int off, int len) throws IOException {
Set<OutputStream> outputStreams = this.streams;
for (OutputStream out : outputStreams) {
out.write(b, off, len);
}
}

/**
* Flushes this output stream and forces any buffered output bytes to be written out to the
* stream.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,8 @@ public void testNewCompositeOutputStreamWithOneStream() throws IOException {
compositeOutputStream.close();

InOrder inOrder = inOrder(mockStreamOne);
inOrder.verify(mockStreamOne, times(1)).write(2);
inOrder.verify(mockStreamOne, times(1)).write(3);
inOrder.verify(mockStreamOne, times(1)).write(4);
inOrder.verify(mockStreamOne, times(1)).write(0);
inOrder.verify(mockStreamOne, times(1)).write(1);
inOrder.verify(mockStreamOne, times(1)).write(new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, 2, 3);
inOrder.verify(mockStreamOne, times(1)).write(new byte[] {0, 1}, 0, 2);
inOrder.verify(mockStreamOne, times(1)).write(9);
inOrder.verify(mockStreamOne, times(2)).flush();
inOrder.verify(mockStreamOne, times(1)).close();
Expand All @@ -101,11 +98,9 @@ public void testNewCompositeOutputStreamWithTwoStreams() throws IOException {
cos.close();

InOrder inOrderStreams = inOrder(streamOne, streamTwo);
inOrderStreams.verify(streamOne, times(1)).write(2);
inOrderStreams.verify(streamOne, times(1)).write(3);
inOrderStreams.verify(streamOne, times(1)).write(4);
inOrderStreams.verify(streamOne, times(1)).write(0);
inOrderStreams.verify(streamOne, times(1)).write(1);
inOrderStreams.verify(streamOne, times(1)).write(new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, 2,
3);
inOrderStreams.verify(streamOne, times(1)).write(new byte[] {0, 1}, 0, 2);
inOrderStreams.verify(streamOne, times(1)).write(9);
inOrderStreams.verify(streamOne, times(2)).flush();
inOrderStreams.verify(streamOne, times(1)).close();
Expand All @@ -128,11 +123,9 @@ public void testAddOutputStreamWithTwoStreams() throws IOException {
cos.close();

InOrder inOrderStreams = inOrder(streamOne, streamTwo, streamThree);
inOrderStreams.verify(streamOne, times(1)).write(2);
inOrderStreams.verify(streamOne, times(1)).write(3);
inOrderStreams.verify(streamOne, times(1)).write(4);
inOrderStreams.verify(streamOne, times(1)).write(0);
inOrderStreams.verify(streamOne, times(1)).write(1);
inOrderStreams.verify(streamOne, times(1)).write(new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, 2,
3);
inOrderStreams.verify(streamOne, times(1)).write(new byte[] {0, 1}, 0, 2);
inOrderStreams.verify(streamOne, times(1)).write(9);
inOrderStreams.verify(streamOne, times(2)).flush();
inOrderStreams.verify(streamOne, times(1)).close();
Expand All @@ -154,11 +147,9 @@ public void testAddOutputStreamWithOneStream() throws IOException {
cos.close();

InOrder inOrderStreams = inOrder(streamOne, streamTwo);
inOrderStreams.verify(streamOne, times(1)).write(2);
inOrderStreams.verify(streamOne, times(1)).write(3);
inOrderStreams.verify(streamOne, times(1)).write(4);
inOrderStreams.verify(streamOne, times(1)).write(0);
inOrderStreams.verify(streamOne, times(1)).write(1);
inOrderStreams.verify(streamOne, times(1)).write(new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, 2,
3);
inOrderStreams.verify(streamOne, times(1)).write(new byte[] {0, 1}, 0, 2);
inOrderStreams.verify(streamOne, times(1)).write(9);
inOrderStreams.verify(streamOne, times(2)).flush();
inOrderStreams.verify(streamOne, times(1)).close();
Expand All @@ -180,11 +171,9 @@ public void testAddOneOutputStreamWhenEmpty() throws IOException {
cos.close();

InOrder inOrderStreams = inOrder(streamOne);
inOrderStreams.verify(streamOne, times(1)).write(2);
inOrderStreams.verify(streamOne, times(1)).write(3);
inOrderStreams.verify(streamOne, times(1)).write(4);
inOrderStreams.verify(streamOne, times(1)).write(0);
inOrderStreams.verify(streamOne, times(1)).write(1);
inOrderStreams.verify(streamOne, times(1)).write(new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, 2,
3);
inOrderStreams.verify(streamOne, times(1)).write(new byte[] {0, 1}, 0, 2);
inOrderStreams.verify(streamOne, times(1)).write(9);
inOrderStreams.verify(streamOne, times(2)).flush();
inOrderStreams.verify(streamOne, times(1)).close();
Expand All @@ -208,11 +197,9 @@ public void testAddTwoOutputStreamsWhenEmpty() throws IOException {
cos.close();

InOrder inOrderStreams = inOrder(streamOne, streamTwo);
inOrderStreams.verify(streamOne, times(1)).write(2);
inOrderStreams.verify(streamOne, times(1)).write(3);
inOrderStreams.verify(streamOne, times(1)).write(4);
inOrderStreams.verify(streamOne, times(1)).write(0);
inOrderStreams.verify(streamOne, times(1)).write(1);
inOrderStreams.verify(streamOne, times(1)).write(new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, 2,
3);
inOrderStreams.verify(streamOne, times(1)).write(new byte[] {0, 1}, 0, 2);
inOrderStreams.verify(streamOne, times(1)).write(9);
inOrderStreams.verify(streamOne, times(2)).flush();
inOrderStreams.verify(streamOne, times(1)).close();
Expand All @@ -236,11 +223,9 @@ public void testRemoveOutputStreamWithTwoStreams() throws IOException {

verifyNoMoreInteractions(streamTwo);
InOrder inOrderStreams = inOrder(streamOne);
inOrderStreams.verify(streamOne, times(1)).write(2);
inOrderStreams.verify(streamOne, times(1)).write(3);
inOrderStreams.verify(streamOne, times(1)).write(4);
inOrderStreams.verify(streamOne, times(1)).write(0);
inOrderStreams.verify(streamOne, times(1)).write(1);
inOrderStreams.verify(streamOne, times(1)).write(new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, 2,
3);
inOrderStreams.verify(streamOne, times(1)).write(new byte[] {0, 1}, 0, 2);
inOrderStreams.verify(streamOne, times(1)).write(9);
inOrderStreams.verify(streamOne, times(2)).flush();
inOrderStreams.verify(streamOne, times(1)).close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ public void write(final int byteData) throws IOException {
outputStream.write(byteData);
byteCount.incrementAndGet();
}

@Override
public void write(byte[] b, int off, int len) throws IOException {
outputStream.write(b, off, len);
byteCount.addAndGet(len);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@ public void write(final int byteData) throws IOException {
outputStream.write(byteData);
byteCount.incrementAndGet();
}

@Override
public void write(byte[] b, int off, int len) throws IOException {
outputStream.write(b, off, len);
byteCount.addAndGet(len);
}
}

}

0 comments on commit bd2d118

Please sign in to comment.