24
24
import java .net .URISyntaxException ;
25
25
import java .nio .ByteBuffer ;
26
26
import java .nio .charset .Charset ;
27
+ import java .util .concurrent .atomic .AtomicInteger ;
27
28
import java .util .function .IntPredicate ;
28
29
import javax .net .ssl .SSLSession ;
29
30
@@ -211,31 +212,45 @@ private static class UndertowDataBuffer implements PooledDataBuffer {
211
212
212
213
private final PooledByteBuffer pooledByteBuffer ;
213
214
215
+ private final AtomicInteger refCount ;
216
+
214
217
public UndertowDataBuffer (DataBuffer dataBuffer , PooledByteBuffer pooledByteBuffer ) {
215
218
this .dataBuffer = dataBuffer ;
216
219
this .pooledByteBuffer = pooledByteBuffer ;
220
+ this .refCount = new AtomicInteger (1 );
221
+ }
222
+
223
+ private UndertowDataBuffer (DataBuffer dataBuffer , PooledByteBuffer pooledByteBuffer ,
224
+ AtomicInteger refCount ) {
225
+ this .refCount = refCount ;
226
+ this .dataBuffer = dataBuffer ;
227
+ this .pooledByteBuffer = pooledByteBuffer ;
217
228
}
218
229
219
230
@ Override
220
231
public boolean isAllocated () {
221
- return this .pooledByteBuffer . isOpen () ;
232
+ return this .refCount . get () > 0 ;
222
233
}
223
234
224
235
@ Override
225
236
public PooledDataBuffer retain () {
237
+ this .refCount .incrementAndGet ();
238
+ DataBufferUtils .retain (this .dataBuffer );
226
239
return this ;
227
240
}
228
241
229
242
@ Override
230
243
public boolean release () {
231
- boolean result ;
232
- try {
233
- result = DataBufferUtils .release (this .dataBuffer );
234
- }
235
- finally {
236
- this .pooledByteBuffer .close ();
244
+ int refCount = this .refCount .decrementAndGet ();
245
+ if (refCount == 0 ) {
246
+ try {
247
+ return DataBufferUtils .release (this .dataBuffer );
248
+ }
249
+ finally {
250
+ this .pooledByteBuffer .close ();
251
+ }
237
252
}
238
- return result && this . pooledByteBuffer . isOpen () ;
253
+ return false ;
239
254
}
240
255
241
256
@ Override
@@ -280,7 +295,8 @@ public int writePosition() {
280
295
281
296
@ Override
282
297
public DataBuffer writePosition (int writePosition ) {
283
- return this .dataBuffer .writePosition (writePosition );
298
+ this .dataBuffer .writePosition (writePosition );
299
+ return this ;
284
300
}
285
301
286
302
@ Override
@@ -290,12 +306,14 @@ public int capacity() {
290
306
291
307
@ Override
292
308
public DataBuffer capacity (int newCapacity ) {
293
- return this .dataBuffer .capacity (newCapacity );
309
+ this .dataBuffer .capacity (newCapacity );
310
+ return this ;
294
311
}
295
312
296
313
@ Override
297
314
public DataBuffer ensureCapacity (int capacity ) {
298
- return this .dataBuffer .ensureCapacity (capacity );
315
+ this .dataBuffer .ensureCapacity (capacity );
316
+ return this ;
299
317
}
300
318
301
319
@ Override
@@ -310,47 +328,56 @@ public byte read() {
310
328
311
329
@ Override
312
330
public DataBuffer read (byte [] destination ) {
313
- return this .dataBuffer .read (destination );
331
+ this .dataBuffer .read (destination );
332
+ return this ;
314
333
}
315
334
316
335
@ Override
317
336
public DataBuffer read (byte [] destination , int offset , int length ) {
318
- return this .dataBuffer .read (destination , offset , length );
337
+ this .dataBuffer .read (destination , offset , length );
338
+ return this ;
319
339
}
320
340
321
341
@ Override
322
342
public DataBuffer write (byte b ) {
323
- return this .dataBuffer .write (b );
343
+ this .dataBuffer .write (b );
344
+ return this ;
324
345
}
325
346
326
347
@ Override
327
348
public DataBuffer write (byte [] source ) {
328
- return this .dataBuffer .write (source );
349
+ this .dataBuffer .write (source );
350
+ return this ;
329
351
}
330
352
331
353
@ Override
332
354
public DataBuffer write (byte [] source , int offset , int length ) {
333
- return this .dataBuffer .write (source , offset , length );
355
+ this .dataBuffer .write (source , offset , length );
356
+ return this ;
334
357
}
335
358
336
359
@ Override
337
360
public DataBuffer write (DataBuffer ... buffers ) {
338
- return this .dataBuffer .write (buffers );
361
+ this .dataBuffer .write (buffers );
362
+ return this ;
339
363
}
340
364
341
365
@ Override
342
366
public DataBuffer write (ByteBuffer ... byteBuffers ) {
343
- return this .dataBuffer .write (byteBuffers );
367
+ this .dataBuffer .write (byteBuffers );
368
+ return this ;
344
369
}
345
370
346
371
@ Override
347
372
public DataBuffer write (CharSequence charSequence , Charset charset ) {
348
- return this .dataBuffer .write (charSequence , charset );
373
+ this .dataBuffer .write (charSequence , charset );
374
+ return this ;
349
375
}
350
376
351
377
@ Override
352
378
public DataBuffer slice (int index , int length ) {
353
- return this .dataBuffer .slice (index , length );
379
+ DataBuffer slice = this .dataBuffer .slice (index , length );
380
+ return new UndertowDataBuffer (slice , this .pooledByteBuffer , this .refCount );
354
381
}
355
382
356
383
@ Override
0 commit comments