Skip to content

Commit 868dbde

Browse files
committed
Ensure Java 8 ByteBufferAPI usage.
[resolves #690]
1 parent fe87bb2 commit 868dbde

File tree

9 files changed

+12
-22
lines changed

9 files changed

+12
-22
lines changed

src/main/java/io/r2dbc/postgresql/codec/Json.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import io.netty.buffer.ByteBufUtil;
2222
import io.netty.buffer.Unpooled;
2323
import io.r2dbc.postgresql.util.Assert;
24+
import io.r2dbc.postgresql.util.ByteBufferUtils;
2425
import org.jspecify.annotations.Nullable;
2526

2627
import java.io.ByteArrayInputStream;
@@ -372,9 +373,7 @@ void assertNotReleased() {
372373
public <T> T mapBuffer(Function<ByteBuffer, ? extends T> mappingFunction) {
373374
assertNotReleased();
374375

375-
ByteBuffer buffer = ByteBuffer.allocate(this.buffer.readableBytes());
376-
this.buffer.readBytes(buffer);
377-
buffer.flip();
376+
ByteBuffer buffer = ByteBufferUtils.toByteBuffer(this.buffer);
378377
release();
379378

380379
return mappingFunction.apply(buffer);

src/main/java/io/r2dbc/postgresql/codec/JsonByteBufferCodec.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import io.r2dbc.postgresql.client.EncodedParameter;
2222
import io.r2dbc.postgresql.message.Format;
2323
import io.r2dbc.postgresql.util.Assert;
24+
import io.r2dbc.postgresql.util.ByteBufferUtils;
2425

2526
import java.nio.ByteBuffer;
2627

@@ -37,12 +38,7 @@ final class JsonByteBufferCodec extends AbstractJsonCodec<ByteBuffer> {
3738

3839
@Override
3940
ByteBuffer doDecode(ByteBuf buffer, PostgresTypeIdentifier dataType, Format format, Class<? extends ByteBuffer> type) {
40-
41-
ByteBuffer result = ByteBuffer.allocate(buffer.readableBytes());
42-
buffer.readBytes(result);
43-
result.flip();
44-
45-
return result;
41+
return ByteBufferUtils.toByteBuffer(buffer);
4642
}
4743

4844
@Override

src/main/java/io/r2dbc/postgresql/message/frontend/GSSResponse.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ public final class GSSResponse implements FrontendMessage {
4646
*/
4747
public GSSResponse(ByteBuffer data) {
4848
Assert.requireNonNull(data, "data must not be null");
49-
data.flip();
5049
this.data = data;
5150
}
5251

src/main/java/io/r2dbc/postgresql/message/frontend/SASLInitialResponse.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
*/
3939
public final class SASLInitialResponse implements FrontendMessage {
4040

41-
private final ByteBuffer initialResponse;
41+
private final @Nullable ByteBuffer initialResponse;
4242

4343
private final String name;
4444

@@ -50,12 +50,7 @@ public final class SASLInitialResponse implements FrontendMessage {
5050
* @throws IllegalArgumentException if {@code name} is {@code null}
5151
*/
5252
public SASLInitialResponse(@Nullable ByteBuffer initialResponse, String name) {
53-
if (initialResponse == null) {
54-
this.initialResponse = null;
55-
} else {
56-
initialResponse.flip();
57-
this.initialResponse = initialResponse;
58-
}
53+
this.initialResponse = initialResponse;
5954
this.name = Assert.requireNonNull(name, "name must not be null");
6055
}
6156

src/main/java/io/r2dbc/postgresql/message/frontend/SASLResponse.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ public final class SASLResponse implements FrontendMessage {
4646
*/
4747
public SASLResponse(ByteBuffer data) {
4848
Assert.requireNonNull(data, "data must not be null");
49-
data.flip();
5049
this.data = data;
5150
}
5251

src/main/java/io/r2dbc/postgresql/util/ByteBufferUtils.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import io.netty.buffer.ByteBuf;
2020

21+
import java.nio.Buffer;
2122
import java.nio.ByteBuffer;
2223
import java.nio.charset.StandardCharsets;
2324

@@ -54,6 +55,7 @@ public static ByteBuffer encode(CharSequence s) {
5455

5556
ByteBuffer buffer = StandardCharsets.UTF_8.encode(s.toString());
5657
buffer.position(buffer.limit());
58+
((Buffer) buffer).flip();
5759
return buffer;
5860
}
5961

@@ -68,7 +70,7 @@ public static ByteBuffer toByteBuffer(ByteBuf source) {
6870

6971
ByteBuffer out = ByteBuffer.allocate(source.readableBytes());
7072
source.readBytes(out);
71-
out.flip();
73+
((Buffer) out).flip();
7274
return out;
7375
}
7476

src/test/java/io/r2dbc/postgresql/message/frontend/GSSResponseUnitTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ void constructorNoData() {
3636

3737
@Test
3838
void encode() {
39-
assertThat(new GSSResponse(ByteBuffer.allocate(4).putInt(100))).encoded()
39+
assertThat(new GSSResponse(ByteBuffer.allocate(4).putInt(100).flip())).encoded()
4040
.isDeferred()
4141
.isEncodedAs(buffer -> buffer
4242
.writeByte('p')

src/test/java/io/r2dbc/postgresql/message/frontend/SASLInitialResponseUnitTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ void constructorNoName() {
3737

3838
@Test
3939
void encode() {
40-
assertThat(new SASLInitialResponse(ByteBuffer.allocate(4).putInt(100), "test-name")).encoded()
40+
assertThat(new SASLInitialResponse(ByteBuffer.allocate(4).putInt(100).flip(), "test-name")).encoded()
4141
.isDeferred()
4242
.isEncodedAs(buffer -> {
4343
buffer

src/test/java/io/r2dbc/postgresql/message/frontend/SASLResponseUnitTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ void constructorNoData() {
3636

3737
@Test
3838
void encode() {
39-
assertThat(new SASLResponse(ByteBuffer.allocate(4).putInt(100))).encoded()
39+
assertThat(new SASLResponse(ByteBuffer.allocate(4).putInt(100).flip())).encoded()
4040
.isDeferred()
4141
.isEncodedAs(buffer -> buffer
4242
.writeByte('p')

0 commit comments

Comments
 (0)