Skip to content

Commit 269371d

Browse files
authored
Refactor ByteBufCombiner#composite method to NettyBufferUtils#composite (#126)
Motivation: Improve reusability by moving the `composite` method from the `ByteBufCombiner` class to the `NettyBufferUtils` class. Modifications: The `composite` method, previously residing in the `ByteBufCombiner` class, has been extracted and refactored into `composite` method within the `NettyBufferUtils` class. Result: Enhance code organization.
1 parent dd4568c commit 269371d

File tree

4 files changed

+53
-80
lines changed

4 files changed

+53
-80
lines changed

src/main/java/io/asyncer/r2dbc/mysql/internal/util/NettyBufferUtils.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,62 @@
1717
package io.asyncer.r2dbc.mysql.internal.util;
1818

1919
import io.asyncer.r2dbc.mysql.message.FieldValue;
20+
import io.netty.buffer.ByteBuf;
21+
import io.netty.buffer.CompositeByteBuf;
2022
import io.netty.util.ReferenceCountUtil;
2123
import io.netty.util.ReferenceCounted;
2224

2325
import java.util.List;
2426

27+
2528
/**
2629
* An internal utility considers the use of safe release buffers (array or {@link List}). It uses standard
2730
* netty {@link ReferenceCountUtil#safeRelease} to suppress release errors.
2831
*/
2932
public final class NettyBufferUtils {
3033

34+
/**
35+
* Combine {@link ByteBuf}s through composite buffer.
36+
* <p>
37+
* This method would release all {@link ByteBuf}s when any exception throws.
38+
*
39+
* @param parts The {@link ByteBuf}s want to be wrap, it can not be empty, and it will be cleared.
40+
* @return A {@link ByteBuf} holds the all bytes of given {@code parts}, it may be a read-only buffer.
41+
*/
42+
public static ByteBuf composite(final List<ByteBuf> parts) {
43+
final int size = parts.size();
44+
45+
switch (size) {
46+
case 0:
47+
throw new IllegalStateException("No buffer available");
48+
case 1:
49+
try {
50+
return parts.get(0);
51+
} finally {
52+
parts.clear();
53+
}
54+
default:
55+
CompositeByteBuf composite = null;
56+
57+
try {
58+
composite = parts.get(0).alloc().compositeBuffer(size);
59+
// Auto-releasing failed parts
60+
return composite.addComponents(true, parts);
61+
} catch (Throwable e) {
62+
if (composite == null) {
63+
// Alloc failed, release parts.
64+
releaseAll(parts);
65+
} else {
66+
// Also release success parts.
67+
composite.release();
68+
}
69+
throw e;
70+
} finally {
71+
parts.clear();
72+
}
73+
}
74+
}
75+
3176
public static void releaseAll(ReferenceCounted[] parts) {
3277
for (ReferenceCounted counted : parts) {
3378
if (counted != null) {

src/main/java/io/asyncer/r2dbc/mysql/message/server/ByteBufCombiner.java

Lines changed: 0 additions & 73 deletions
This file was deleted.

src/main/java/io/asyncer/r2dbc/mysql/message/server/FieldReader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ static FieldReader of(List<ByteBuf> buffers) {
7777

7878
if (totalSize <= Integer.MAX_VALUE) {
7979
// The buffers will be cleared by ByteBufCombiner.composite().
80-
ByteBuf combined = ByteBufCombiner.composite(buffers);
80+
ByteBuf combined = NettyBufferUtils.composite(buffers);
8181
try {
8282
return new NormalFieldReader(combined);
8383
} catch (Throwable e) {

src/main/java/io/asyncer/r2dbc/mysql/message/server/ServerMessageDecoder.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,12 @@ private static ServerMessage decodeMessage(List<ByteBuf> buffers, int envelopeId
9090
ConnectionContext context, DecodeContext decodeContext) {
9191
if (decodeContext instanceof ResultDecodeContext) {
9292
return decodeResult(buffers, context, (ResultDecodeContext) decodeContext);
93-
} else if (decodeContext instanceof FetchDecodeContext) {
93+
}
94+
if (decodeContext instanceof FetchDecodeContext) {
9495
return decodeFetch(buffers, context);
9596
}
9697

97-
ByteBuf combined = ByteBufCombiner.composite(buffers);
98+
ByteBuf combined = NettyBufferUtils.composite(buffers);
9899

99100
try {
100101
if (decodeContext instanceof CommandDecodeContext) {
@@ -159,7 +160,7 @@ private static ServerMessage decodeResult(List<ByteBuf> buffers, ConnectionConte
159160
}
160161

161162
if (decodeContext.isInMetadata()) {
162-
ByteBuf combined = ByteBufCombiner.composite(buffers);
163+
ByteBuf combined = NettyBufferUtils.composite(buffers);
163164
try {
164165
return decodeInMetadata(combined, header, context, decodeContext);
165166
} finally {
@@ -309,7 +310,7 @@ private static ErrorMessage decodeCheckError(List<ByteBuf> buffers, short header
309310
// 0xFF is not header of var integer,
310311
// not header of text result null (0xFB) and
311312
// not header of column metadata (0x03 + "def")
312-
ByteBuf combined = ByteBufCombiner.composite(buffers);
313+
ByteBuf combined = NettyBufferUtils.composite(buffers);
313314
try {
314315
return ErrorMessage.decode(combined);
315316
} finally {
@@ -329,15 +330,15 @@ private static ServerMessage decodeRow(List<ByteBuf> buffers, ByteBuf firstBuf,
329330
int byteSize = firstBuf.readableBytes();
330331

331332
if (OkMessage.isValidSize(byteSize)) {
332-
ByteBuf combined = ByteBufCombiner.composite(buffers);
333+
ByteBuf combined = NettyBufferUtils.composite(buffers);
333334

334335
try {
335336
return OkMessage.decode(combined, context);
336337
} finally {
337338
combined.release();
338339
}
339340
} else if (EofMessage.isValidSize(byteSize)) {
340-
ByteBuf combined = ByteBufCombiner.composite(buffers);
341+
ByteBuf combined = NettyBufferUtils.composite(buffers);
341342

342343
try {
343344
return EofMessage.decode(combined);

0 commit comments

Comments
 (0)