@@ -33,6 +33,9 @@ public class Utf8ByteBufCharsetDecoder {
33
33
private final CharsetDecoder decoder = configureReplaceCodingErrorActions (UTF_8 .newDecoder ());
34
34
protected CharBuffer charBuffer = allocateCharBuffer (INITIAL_CHAR_BUFFER_SIZE );
35
35
private ByteBuffer splitCharBuffer = ByteBuffer .allocate (UTF_8_MAX_BYTES_PER_CHAR );
36
+ private int totalSize = 0 ;
37
+ private int totalNioBuffers = 0 ;
38
+ private boolean withoutArray = false ;
36
39
37
40
private static Utf8ByteBufCharsetDecoder pooledDecoder () {
38
41
Utf8ByteBufCharsetDecoder decoder = POOL .get ();
@@ -48,6 +51,14 @@ public static String decodeUtf8(ByteBuf... bufs) {
48
51
return pooledDecoder ().decode (bufs );
49
52
}
50
53
54
+ public static char [] decodeUtf8Chars (ByteBuf buf ) {
55
+ return pooledDecoder ().decodeChars (buf );
56
+ }
57
+
58
+ public static char [] decodeUtf8Chars (ByteBuf ... bufs ) {
59
+ return pooledDecoder ().decodeChars (bufs );
60
+ }
61
+
51
62
private static CharsetDecoder configureReplaceCodingErrorActions (CharsetDecoder decoder ) {
52
63
return decoder .onMalformedInput (CodingErrorAction .REPLACE ).onUnmappableCharacter (CodingErrorAction .REPLACE );
53
64
}
@@ -98,6 +109,9 @@ public void reset() {
98
109
configureReplaceCodingErrorActions (decoder .reset ());
99
110
charBuffer .clear ();
100
111
splitCharBuffer .clear ();
112
+ totalSize = 0 ;
113
+ totalNioBuffers = 0 ;
114
+ withoutArray = false ;
101
115
}
102
116
103
117
private boolean stashContinuationBytes (ByteBuffer nioBuffer , int missingBytes ) {
@@ -176,7 +190,47 @@ public String decode(ByteBuf buf) {
176
190
if (buf .isDirect ()) {
177
191
return buf .toString (UTF_8 );
178
192
}
193
+ decodeHead0 (buf );
194
+ return charBuffer .toString ();
195
+ }
196
+
197
+ public char [] decodeChars (ByteBuf buf ) {
198
+ if (buf .isDirect ()) {
199
+ return buf .toString (UTF_8 ).toCharArray ();
200
+ }
201
+ decodeHead0 (buf );
202
+ return toCharArray (charBuffer );
203
+ }
204
+
205
+ public String decode (ByteBuf ... bufs ) {
206
+ if (bufs .length == 1 ) {
207
+ return decode (bufs [0 ]);
208
+ }
209
+
210
+ inspectByteBufs (bufs );
211
+ if (withoutArray ) {
212
+ return ByteBufUtils .byteBuf2StringDefault (UTF_8 , bufs );
213
+ } else {
214
+ decodeHeap0 (bufs );
215
+ return charBuffer .toString ();
216
+ }
217
+ }
218
+
219
+ public char [] decodeChars (ByteBuf ... bufs ) {
220
+ if (bufs .length == 1 ) {
221
+ return decodeChars (bufs [0 ]);
222
+ }
179
223
224
+ inspectByteBufs (bufs );
225
+ if (withoutArray ) {
226
+ return ByteBufUtils .byteBuf2StringDefault (UTF_8 , bufs ).toCharArray ();
227
+ } else {
228
+ decodeHeap0 (bufs );
229
+ return toCharArray (charBuffer );
230
+ }
231
+ }
232
+
233
+ private void decodeHead0 (ByteBuf buf ) {
180
234
int length = buf .readableBytes ();
181
235
ensureCapacity (length );
182
236
@@ -185,18 +239,29 @@ public String decode(ByteBuf buf) {
185
239
} else {
186
240
decode (buf .nioBuffers ());
187
241
}
188
-
189
- return charBuffer .flip ().toString ();
242
+ charBuffer .flip ();
190
243
}
191
244
192
- public String decode (ByteBuf ... bufs ) {
193
- if (bufs .length == 1 ) {
194
- return decode (bufs [0 ]);
245
+ private void decodeHeap0 (ByteBuf [] bufs ) {
246
+ ByteBuffer [] nioBuffers = new ByteBuffer [totalNioBuffers ];
247
+ int i = 0 ;
248
+ for (ByteBuf buf : bufs ) {
249
+ for (ByteBuffer nioBuffer : buf .nioBuffers ()) {
250
+ nioBuffers [i ++] = nioBuffer ;
251
+ }
195
252
}
253
+ ensureCapacity (totalSize );
254
+ decode (nioBuffers );
255
+ charBuffer .flip ();
256
+ }
196
257
197
- int totalSize = 0 ;
198
- int totalNioBuffers = 0 ;
199
- boolean withoutArray = false ;
258
+ private static char [] toCharArray (CharBuffer charBuffer ) {
259
+ char [] chars = new char [charBuffer .remaining ()];
260
+ charBuffer .get (chars );
261
+ return chars ;
262
+ }
263
+
264
+ private void inspectByteBufs (ByteBuf [] bufs ) {
200
265
for (ByteBuf buf : bufs ) {
201
266
if (!buf .hasArray ()) {
202
267
withoutArray = true ;
@@ -205,23 +270,5 @@ public String decode(ByteBuf... bufs) {
205
270
totalSize += buf .readableBytes ();
206
271
totalNioBuffers += buf .nioBufferCount ();
207
272
}
208
-
209
- if (withoutArray ) {
210
- return ByteBufUtils .byteBuf2StringDefault (UTF_8 , bufs );
211
-
212
- } else {
213
- ByteBuffer [] nioBuffers = new ByteBuffer [totalNioBuffers ];
214
- int i = 0 ;
215
- for (ByteBuf buf : bufs ) {
216
- for (ByteBuffer nioBuffer : buf .nioBuffers ()) {
217
- nioBuffers [i ++] = nioBuffer ;
218
- }
219
- }
220
-
221
- ensureCapacity (totalSize );
222
- decode (nioBuffers );
223
-
224
- return charBuffer .flip ().toString ();
225
- }
226
273
}
227
274
}
0 commit comments