Skip to content

Commit 21e7fe8

Browse files
committed
Polishing.
Delay buffer allocation until there is something to read. Guard code with try/finally to ensure aggregation buffer release. [pgjdbc#407][closes pgjdbc#408] Signed-off-by: Mark Paluch <[email protected]>
1 parent 65aa28c commit 21e7fe8

File tree

2 files changed

+18
-32
lines changed

2 files changed

+18
-32
lines changed

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

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import reactor.core.publisher.Mono;
2626

2727
import javax.annotation.Nullable;
28-
2928
import java.nio.charset.StandardCharsets;
3029
import java.util.LinkedHashMap;
3130
import java.util.Map;
@@ -37,25 +36,12 @@
3736
final class HStoreCodec implements Codec<Map> {
3837

3938
/**
40-
* A {@link ByteProcessor} which finds the first appearance of a specific byte.
39+
* Aborts on a comma {@code ('"')}.
4140
*/
42-
static class IndexOfProcessor implements ByteProcessor {
43-
44-
private final byte byteToFind;
45-
46-
public IndexOfProcessor(byte byteToFind) {
47-
this.byteToFind = byteToFind;
48-
}
49-
50-
@Override
51-
public boolean process(byte value) {
52-
return value != this.byteToFind;
53-
}
54-
55-
}
41+
static final ByteProcessor FIND_QUOTE = new ByteProcessor.IndexOfProcessor((byte) '"');
5642

5743
private static final Class<Map> TYPE = Map.class;
58-
44+
5945
private final ByteBufAllocator byteBufAllocator;
6046

6147
private final int oid;
@@ -163,9 +149,8 @@ private static byte peekByte(ByteBuf buffer) {
163149

164150
@Nullable
165151
private static String readString(ByteBuf buffer) {
166-
final ByteBuf accBuffer = buffer.alloc().buffer();
167-
final int position = buffer.forEachByte(new IndexOfProcessor((byte) '"'));
168152

153+
int position = buffer.forEachByte(FIND_QUOTE);
169154
if (position > buffer.writerIndex()) {
170155
return null;
171156
}
@@ -174,20 +159,22 @@ private static String readString(ByteBuf buffer) {
174159
buffer.readerIndex(position + 1);
175160
}
176161

177-
while (buffer.isReadable()) {
178-
final byte current = buffer.readByte();
179-
180-
if (current == '"') {
181-
break;
182-
}
162+
ByteBuf aggregated = buffer.alloc().buffer();
163+
try {
164+
while (buffer.isReadable()) {
165+
byte current = buffer.readByte();
183166

184-
accBuffer.writeByte(current == '\\' ? buffer.readByte() : current);
185-
}
167+
if (current == '"') {
168+
break;
169+
}
186170

187-
final String result = accBuffer.toString(StandardCharsets.UTF_8);
171+
aggregated.writeByte(current == '\\' ? buffer.readByte() : current);
172+
}
188173

189-
accBuffer.release();
190-
return result;
174+
return aggregated.toString(StandardCharsets.UTF_8);
175+
} finally {
176+
aggregated.release();
177+
}
191178
}
192179

193180
@Override

src/test/java/io/r2dbc/postgresql/codec/HStoreCodecUnitTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import io.r2dbc.postgresql.client.EncodedParameter;
2121
import io.r2dbc.postgresql.client.ParameterAssert;
2222
import io.r2dbc.postgresql.message.Format;
23-
import org.assertj.core.api.Assertions;
2423
import org.junit.jupiter.api.Test;
2524

2625
import java.nio.charset.Charset;
@@ -62,7 +61,7 @@ void decodeAsText() {
6261
expect.put("d", "Zoë");
6362
expect.put("é", "120°");
6463

65-
Assertions.assertThat(res).isEqualTo(expect);
64+
assertThat(res).isEqualTo(expect);
6665
}
6766

6867
@Test

0 commit comments

Comments
 (0)