Skip to content

Commit e1e1b79

Browse files
committed
Enrich Utf8ByteBufCharsetDecoder to support decoding into char array
1 parent 693e2d2 commit e1e1b79

File tree

1 file changed

+73
-26
lines changed

1 file changed

+73
-26
lines changed

netty-utils/src/main/java/org/asynchttpclient/netty/util/Utf8ByteBufCharsetDecoder.java

Lines changed: 73 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ public class Utf8ByteBufCharsetDecoder {
3333
private final CharsetDecoder decoder = configureReplaceCodingErrorActions(UTF_8.newDecoder());
3434
protected CharBuffer charBuffer = allocateCharBuffer(INITIAL_CHAR_BUFFER_SIZE);
3535
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;
3639

3740
private static Utf8ByteBufCharsetDecoder pooledDecoder() {
3841
Utf8ByteBufCharsetDecoder decoder = POOL.get();
@@ -48,6 +51,14 @@ public static String decodeUtf8(ByteBuf... bufs) {
4851
return pooledDecoder().decode(bufs);
4952
}
5053

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+
5162
private static CharsetDecoder configureReplaceCodingErrorActions(CharsetDecoder decoder) {
5263
return decoder.onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE);
5364
}
@@ -98,6 +109,9 @@ public void reset() {
98109
configureReplaceCodingErrorActions(decoder.reset());
99110
charBuffer.clear();
100111
splitCharBuffer.clear();
112+
totalSize = 0;
113+
totalNioBuffers = 0;
114+
withoutArray = false;
101115
}
102116

103117
private boolean stashContinuationBytes(ByteBuffer nioBuffer, int missingBytes) {
@@ -176,7 +190,47 @@ public String decode(ByteBuf buf) {
176190
if (buf.isDirect()) {
177191
return buf.toString(UTF_8);
178192
}
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+
}
179223

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) {
180234
int length = buf.readableBytes();
181235
ensureCapacity(length);
182236

@@ -185,18 +239,29 @@ public String decode(ByteBuf buf) {
185239
} else {
186240
decode(buf.nioBuffers());
187241
}
188-
189-
return charBuffer.flip().toString();
242+
charBuffer.flip();
190243
}
191244

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+
}
195252
}
253+
ensureCapacity(totalSize);
254+
decode(nioBuffers);
255+
charBuffer.flip();
256+
}
196257

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) {
200265
for (ByteBuf buf : bufs) {
201266
if (!buf.hasArray()) {
202267
withoutArray = true;
@@ -205,23 +270,5 @@ public String decode(ByteBuf... bufs) {
205270
totalSize += buf.readableBytes();
206271
totalNioBuffers += buf.nioBufferCount();
207272
}
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-
}
226273
}
227274
}

0 commit comments

Comments
 (0)