25
25
import reactor .core .publisher .Mono ;
26
26
27
27
import javax .annotation .Nullable ;
28
+
29
+ import java .nio .charset .StandardCharsets ;
28
30
import java .util .LinkedHashMap ;
29
31
import java .util .Map ;
30
32
@@ -52,10 +54,10 @@ public boolean process(byte value) {
52
54
53
55
}
54
56
57
+ private static final Class <Map > TYPE = Map .class ;
58
+
55
59
private final ByteBufAllocator byteBufAllocator ;
56
60
57
- private final Class <Map > type = Map .class ;
58
-
59
61
private final int oid ;
60
62
61
63
/**
@@ -73,23 +75,21 @@ public boolean canDecode(int dataType, Format format, Class<?> type) {
73
75
Assert .requireNonNull (format , "format must not be null" );
74
76
Assert .requireNonNull (type , "type must not be null" );
75
77
76
- Assert .requireNonNull (type , "type must not be null" );
77
-
78
- return dataType == this .oid && type .isAssignableFrom (this .type );
78
+ return dataType == this .oid && type .isAssignableFrom (TYPE );
79
79
}
80
80
81
81
@ Override
82
82
public boolean canEncode (Object value ) {
83
83
Assert .requireNonNull (value , "value must not be null" );
84
84
85
- return this . type .isInstance (value );
85
+ return TYPE .isInstance (value );
86
86
}
87
87
88
88
@ Override
89
89
public boolean canEncodeNull (Class <?> type ) {
90
90
Assert .requireNonNull (type , "type must not be null" );
91
91
92
- return this . type .isAssignableFrom (type );
92
+ return TYPE .isAssignableFrom (type );
93
93
}
94
94
95
95
@ Override
@@ -126,10 +126,9 @@ private static Map<String, String> decodeBinaryFormat(ByteBuf buffer) {
126
126
127
127
private static Map <String , String > decodeTextFormat (ByteBuf buffer ) {
128
128
Map <String , String > map = new LinkedHashMap <>();
129
- StringBuilder mutableBuffer = new StringBuilder ();
130
129
131
130
while (buffer .isReadable ()) {
132
- String key = readString (mutableBuffer , buffer );
131
+ String key = readString (buffer );
133
132
if (key == null ) {
134
133
break ;
135
134
}
@@ -140,7 +139,7 @@ private static Map<String, String> decodeTextFormat(ByteBuf buffer) {
140
139
value = null ;
141
140
buffer .skipBytes (4 );// skip 'NULL'.
142
141
} else {
143
- value = readString (mutableBuffer , buffer );
142
+ value = readString (buffer );
144
143
}
145
144
map .put (key , value );
146
145
@@ -163,9 +162,10 @@ private static byte peekByte(ByteBuf buffer) {
163
162
}
164
163
165
164
@ Nullable
166
- private static String readString (StringBuilder mutableBuffer , ByteBuf buffer ) {
167
- mutableBuffer .setLength (0 );
168
- int position = buffer .forEachByte (new IndexOfProcessor ((byte ) '"' ));
165
+ private static String readString (ByteBuf buffer ) {
166
+ final ByteBuf accBuffer = buffer .alloc ().buffer ();
167
+ final int position = buffer .forEachByte (new IndexOfProcessor ((byte ) '"' ));
168
+
169
169
if (position > buffer .writerIndex ()) {
170
170
return null ;
171
171
}
@@ -175,17 +175,18 @@ private static String readString(StringBuilder mutableBuffer, ByteBuf buffer) {
175
175
}
176
176
177
177
while (buffer .isReadable ()) {
178
- char c = (char ) buffer .readByte ();
179
- if (c == '"' ) {
178
+ final byte current = buffer .readByte ();
179
+
180
+ if (current == '"' ) {
180
181
break ;
181
- } else if (c == '\\' ) {
182
- c = (char ) buffer .readByte ();
183
182
}
184
- mutableBuffer .append (c );
183
+
184
+ accBuffer .writeByte (current == '\\' ? buffer .readByte () : current );
185
185
}
186
186
187
- String result = mutableBuffer .toString ();
188
- mutableBuffer .setLength (0 );
187
+ final String result = accBuffer .toString (StandardCharsets .UTF_8 );
188
+
189
+ accBuffer .release ();
189
190
return result ;
190
191
}
191
192
@@ -233,7 +234,7 @@ private EncodedParameter encodeNull(int dataType) {
233
234
234
235
@ Override
235
236
public Class <?> type () {
236
- return this . type ;
237
+ return TYPE ;
237
238
}
238
239
239
240
}
0 commit comments