25
25
import reactor .core .publisher .Mono ;
26
26
27
27
import javax .annotation .Nullable ;
28
-
29
28
import java .nio .charset .StandardCharsets ;
30
29
import java .util .LinkedHashMap ;
31
30
import java .util .Map ;
37
36
final class HStoreCodec implements Codec <Map > {
38
37
39
38
/**
40
- * A {@link ByteProcessor} which finds the first appearance of a specific byte .
39
+ * Aborts on a comma {@code ('"')} .
41
40
*/
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 ) '"' );
56
42
57
43
private static final Class <Map > TYPE = Map .class ;
58
-
44
+
59
45
private final ByteBufAllocator byteBufAllocator ;
60
46
61
47
private final int oid ;
@@ -163,9 +149,8 @@ private static byte peekByte(ByteBuf buffer) {
163
149
164
150
@ Nullable
165
151
private static String readString (ByteBuf buffer ) {
166
- final ByteBuf accBuffer = buffer .alloc ().buffer ();
167
- final int position = buffer .forEachByte (new IndexOfProcessor ((byte ) '"' ));
168
152
153
+ int position = buffer .forEachByte (FIND_QUOTE );
169
154
if (position > buffer .writerIndex ()) {
170
155
return null ;
171
156
}
@@ -174,20 +159,22 @@ private static String readString(ByteBuf buffer) {
174
159
buffer .readerIndex (position + 1 );
175
160
}
176
161
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 ();
183
166
184
- accBuffer .writeByte (current == '\\' ? buffer .readByte () : current );
185
- }
167
+ if (current == '"' ) {
168
+ break ;
169
+ }
186
170
187
- final String result = accBuffer .toString (StandardCharsets .UTF_8 );
171
+ aggregated .writeByte (current == '\\' ? buffer .readByte () : current );
172
+ }
188
173
189
- accBuffer .release ();
190
- return result ;
174
+ return aggregated .toString (StandardCharsets .UTF_8 );
175
+ } finally {
176
+ aggregated .release ();
177
+ }
191
178
}
192
179
193
180
@ Override
0 commit comments