Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle NPE #2906

Merged
merged 2 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/main/java/io/lettuce/core/codec/ByteArrayCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,11 @@ public ByteBuffer encodeValue(byte[] value) {
}

private static byte[] getBytes(ByteBuffer buffer) {
if (buffer == null) {
return EMPTY;
}

int remaining = buffer.remaining();

if (remaining == 0) {
return EMPTY;
}
Expand Down
35 changes: 35 additions & 0 deletions src/test/java/io/lettuce/core/codec/ByteArrayCodecUnitTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.lettuce.core.codec;

import org.junit.jupiter.api.Test;

import java.nio.ByteBuffer;

import static org.assertj.core.api.Assertions.assertThat;

class ByteArrayCodecUnitTests {

@Test
void testDecodeValue_withNonEmptyByteBuffer() {
final ByteArrayCodec byteArrayCodec = ByteArrayCodec.INSTANCE;
final byte[] expectedBytes = { 1, 2, 3, 4, 5 };
final ByteBuffer buffer = ByteBuffer.wrap(expectedBytes);
final byte[] result = byteArrayCodec.decodeValue(buffer);
assertThat(result).isEqualTo(expectedBytes);
}

@Test
void testDecodeValue_withEmptyByteBuffer() {
final ByteArrayCodec byteArrayCodec = ByteArrayCodec.INSTANCE;
final ByteBuffer buffer = ByteBuffer.allocate(0);
final byte[] result = byteArrayCodec.decodeValue(buffer);
assertThat(result).isEmpty();
}

@Test
void testDecodeValue_withNullByteBuffer() {
final ByteArrayCodec byteArrayCodec = ByteArrayCodec.INSTANCE;
final byte[] result = byteArrayCodec.decodeValue(null);
assertThat(result).isEmpty();
}

}
Loading