32
32
import io .undertow .server .HttpServerExchange ;
33
33
34
34
/**
35
+ * Utility class for handling buffer operations and conversions.
36
+ * Provides methods for converting between different buffer types, managing
37
+ * pooled byte buffers, and performing buffer operations safely with size limits.
35
38
*
36
39
* @author Andrea Di Cesare {@literal <andrea@softinstigate.com>}
37
40
*/
38
41
public class BuffersUtils {
39
42
43
+ /** Logger instance for this class. */
40
44
private static final Logger LOGGER = LoggerFactory .getLogger (BuffersUtils .class );
41
45
42
46
/**
43
- * @param srcs
44
- * @return
45
- * @throws java.io.IOException
47
+ * Converts an array of pooled byte buffers to a single ByteBuffer.
48
+ * This method consolidates multiple pooled buffers into one continuous buffer,
49
+ * respecting the maximum content size limit.
50
+ *
51
+ * @param srcs array of pooled byte buffers to convert
52
+ * @return a ByteBuffer containing all the data from the source buffers, or null if srcs is null
53
+ * @throws IOException if the total content exceeds the maximum allowed size
46
54
*/
47
55
public static ByteBuffer toByteBuffer (final PooledByteBuffer [] srcs ) throws IOException {
48
56
if (srcs == null ) {
@@ -72,6 +80,15 @@ public static ByteBuffer toByteBuffer(final PooledByteBuffer[] srcs) throws IOEx
72
80
return dst .flip ();
73
81
}
74
82
83
+ /**
84
+ * Converts an array of pooled byte buffers to a byte array.
85
+ * This method first converts the pooled buffers to a ByteBuffer and then
86
+ * extracts the data as a byte array.
87
+ *
88
+ * @param srcs array of pooled byte buffers to convert
89
+ * @return a byte array containing all the data from the source buffers
90
+ * @throws IOException if the conversion fails or content exceeds size limits
91
+ */
75
92
public static byte [] toByteArray (final PooledByteBuffer [] srcs ) throws IOException {
76
93
var content = toByteBuffer (srcs );
77
94
@@ -82,21 +99,38 @@ public static byte[] toByteArray(final PooledByteBuffer[] srcs) throws IOExcepti
82
99
return ret ;
83
100
}
84
101
102
+ /**
103
+ * Converts an array of pooled byte buffers to a string using the specified charset.
104
+ *
105
+ * @param srcs array of pooled byte buffers to convert
106
+ * @param cs the charset to use for string conversion
107
+ * @return a string representation of the buffer content
108
+ * @throws IOException if the conversion fails or content exceeds size limits
109
+ */
85
110
public static String toString (final PooledByteBuffer [] srcs , Charset cs ) throws IOException {
86
111
return new String (toByteArray (srcs ), cs );
87
112
}
88
113
114
+ /**
115
+ * Converts a byte array to a string using the specified charset.
116
+ *
117
+ * @param src the byte array to convert
118
+ * @param cs the charset to use for string conversion
119
+ * @return a string representation of the byte array content
120
+ * @throws IOException if the conversion fails
121
+ */
89
122
public static String toString (final byte [] src , Charset cs ) throws IOException {
90
123
return new String (src , cs );
91
124
}
92
125
93
126
/**
94
- * transfer the src data to the pooled buffers overwriting the exising data
127
+ * Transfers data from a source ByteBuffer to pooled byte buffers, overwriting existing data.
128
+ * This method allocates new pooled buffers as needed and clears existing ones before copying.
95
129
*
96
- * @param src
97
- * @param dest
98
- * @param exchange
99
- * @return
130
+ * @param src the source ByteBuffer containing data to transfer
131
+ * @param dest array of pooled byte buffers to receive the data
132
+ * @param exchange the HTTP server exchange for accessing the byte buffer pool
133
+ * @return the number of bytes copied
100
134
*/
101
135
public static int transfer (final ByteBuffer src , final PooledByteBuffer [] dest , HttpServerExchange exchange ) {
102
136
var byteBufferPool = exchange .getConnection ().getByteBufferPool ();
@@ -132,6 +166,13 @@ public static int transfer(final ByteBuffer src, final PooledByteBuffer[] dest,
132
166
return copied ;
133
167
}
134
168
169
+ /**
170
+ * Dumps the content of pooled byte buffers to the debug log for debugging purposes.
171
+ * This method logs the hexadecimal representation of each buffer's content.
172
+ *
173
+ * @param msg a message to include in the log output
174
+ * @param data array of pooled byte buffers to dump
175
+ */
135
176
public static void dump (String msg , PooledByteBuffer [] data ) {
136
177
int nbuf = 0 ;
137
178
for (PooledByteBuffer dest : data ) {
@@ -151,12 +192,13 @@ public static void dump(String msg, PooledByteBuffer[] data) {
151
192
}
152
193
153
194
/**
154
- * append the src data to the pooled buffers
195
+ * Appends data from a source ByteBuffer to pooled byte buffers.
196
+ * Unlike transfer(), this method appends to existing buffer content rather than overwriting it.
155
197
*
156
- * @param src
157
- * @param dest
158
- * @param exchange
159
- * @return
198
+ * @param src the source ByteBuffer containing data to append
199
+ * @param dest array of pooled byte buffers to append data to
200
+ * @param exchange the HTTP server exchange for accessing the byte buffer pool
201
+ * @return the number of bytes copied
160
202
*/
161
203
public static int append (final ByteBuffer src , final PooledByteBuffer [] dest , HttpServerExchange exchange ) {
162
204
var byteBufferPool = exchange .getConnection ().getByteBufferPool ();
@@ -192,6 +234,16 @@ public static int append(final ByteBuffer src, final PooledByteBuffer[] dest, Ht
192
234
return copied ;
193
235
}
194
236
237
+ /**
238
+ * Transfers data from source pooled byte buffers to destination pooled byte buffers.
239
+ * This method copies data between two arrays of pooled buffers, allocating destination
240
+ * buffers as needed.
241
+ *
242
+ * @param src array of source pooled byte buffers
243
+ * @param dest array of destination pooled byte buffers
244
+ * @param exchange the HTTP server exchange for accessing the byte buffer pool
245
+ * @return the number of bytes copied
246
+ */
195
247
public static int transfer (final PooledByteBuffer [] src , final PooledByteBuffer [] dest , final HttpServerExchange exchange ) {
196
248
var byteBufferPool = exchange .getConnection ().getByteBufferPool ();
197
249
int copied = 0 ;
0 commit comments