forked from Graylog2/graylog2-server
-
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.
Simplify
ByteBufferUtils#readBytes()
, document side-effects, add te…
…sts (Graylog2#4538) The `ByteBufferUtils#readBytes(ByteBuf, int, int)` method failed for non-array-backed byte buffers whose reader index didn't start at index 0 and failed to mention side effects (e. g. consumption of the given `ByteBuffer`) in the Javadocs. Additionally, the "optimization" in `ByteBufferUtils#readBytes(ByteBuf, int, int)` was unnecessary because the relevant `ByteBuffer` implementations are already using the backing array if available.
- Loading branch information
1 parent
abc7346
commit 77d03b0
Showing
2 changed files
with
60 additions
and
9 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
54 changes: 54 additions & 0 deletions
54
graylog2-server/src/test/java/org/graylog2/shared/utilities/ByteBufferUtilsTest.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,54 @@ | ||
/** | ||
* This file is part of Graylog. | ||
* | ||
* Graylog is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Graylog is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with Graylog. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package org.graylog2.shared.utilities; | ||
|
||
import org.junit.Test; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Arrays; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class ByteBufferUtilsTest { | ||
@Test | ||
public void readBytesFromArrayBackedByteBuffer() { | ||
final byte[] bytes = "FOOBAR".getBytes(StandardCharsets.US_ASCII); | ||
final ByteBuffer buffer1 = ByteBuffer.wrap(bytes); | ||
final ByteBuffer buffer2 = ByteBuffer.wrap(bytes); | ||
final byte[] readBytesComplete = ByteBufferUtils.readBytes(buffer1); | ||
final byte[] readBytesPartial = ByteBufferUtils.readBytes(buffer2, 0, 3); | ||
|
||
assertThat(readBytesComplete).isEqualTo(bytes); | ||
assertThat(readBytesPartial).isEqualTo(Arrays.copyOf(bytes, 3)); | ||
} | ||
|
||
@Test | ||
public void readBytesFromNonArrayBackedByteBuffer() { | ||
final byte[] bytes = "FOOBAR".getBytes(StandardCharsets.US_ASCII); | ||
final ByteBuffer buffer1 = ByteBuffer.allocateDirect(1024); | ||
buffer1.put(bytes).flip(); | ||
final ByteBuffer buffer2 = ByteBuffer.allocateDirect(1024); | ||
buffer2.put(bytes).flip(); | ||
|
||
final byte[] readBytesComplete = ByteBufferUtils.readBytes(buffer1); | ||
final byte[] readBytesPartial = ByteBufferUtils.readBytes(buffer2, 0, 3); | ||
|
||
assertThat(readBytesComplete).isEqualTo(bytes); | ||
assertThat(readBytesPartial).isEqualTo(Arrays.copyOf(bytes, 3)); | ||
} | ||
} |