21
21
import java .net .URI ;
22
22
import java .net .URISyntaxException ;
23
23
import java .nio .ByteBuffer ;
24
- import java .util .concurrent .atomic .AtomicInteger ;
25
24
26
25
import javax .net .ssl .SSLSession ;
27
26
34
33
35
34
import org .springframework .core .io .buffer .DataBuffer ;
36
35
import org .springframework .core .io .buffer .DataBufferFactory ;
37
- import org .springframework .core .io .buffer .DataBufferUtils ;
38
- import org .springframework .core .io .buffer .DataBufferWrapper ;
39
- import org .springframework .core .io .buffer .PooledDataBuffer ;
40
36
import org .springframework .http .HttpCookie ;
41
37
import org .springframework .lang .Nullable ;
42
38
import org .springframework .util .Assert ;
@@ -172,7 +168,6 @@ protected void readingPaused() {
172
168
@ Nullable
173
169
protected DataBuffer read () throws IOException {
174
170
PooledByteBuffer pooledByteBuffer = this .byteBufferPool .allocate ();
175
- boolean release = true ;
176
171
try {
177
172
ByteBuffer byteBuffer = pooledByteBuffer .getBuffer ();
178
173
int read = this .channel .read (byteBuffer );
@@ -183,19 +178,17 @@ protected DataBuffer read() throws IOException {
183
178
184
179
if (read > 0 ) {
185
180
byteBuffer .flip ();
186
- DataBuffer dataBuffer = this .bufferFactory .wrap ( byteBuffer );
187
- release = false ;
188
- return new UndertowDataBuffer ( dataBuffer , pooledByteBuffer ) ;
181
+ DataBuffer dataBuffer = this .bufferFactory .allocateBuffer ( read );
182
+ dataBuffer . write ( byteBuffer ) ;
183
+ return dataBuffer ;
189
184
}
190
185
else if (read == -1 ) {
191
186
onAllDataRead ();
192
187
}
193
188
return null ;
194
189
}
195
190
finally {
196
- if (release && pooledByteBuffer .isOpen ()) {
197
- pooledByteBuffer .close ();
198
- }
191
+ pooledByteBuffer .close ();
199
192
}
200
193
}
201
194
@@ -205,59 +198,4 @@ protected void discardData() {
205
198
}
206
199
}
207
200
208
-
209
- private static class UndertowDataBuffer extends DataBufferWrapper implements PooledDataBuffer {
210
-
211
- private final PooledByteBuffer pooledByteBuffer ;
212
-
213
- private final AtomicInteger refCount ;
214
-
215
-
216
- public UndertowDataBuffer (DataBuffer dataBuffer , PooledByteBuffer pooledByteBuffer ) {
217
- super (dataBuffer );
218
- this .pooledByteBuffer = pooledByteBuffer ;
219
- this .refCount = new AtomicInteger (1 );
220
- }
221
-
222
- private UndertowDataBuffer (DataBuffer dataBuffer , PooledByteBuffer pooledByteBuffer ,
223
- AtomicInteger refCount ) {
224
- super (dataBuffer );
225
- this .refCount = refCount ;
226
- this .pooledByteBuffer = pooledByteBuffer ;
227
- }
228
-
229
- @ Override
230
- public boolean isAllocated () {
231
- return this .refCount .get () > 0 ;
232
- }
233
-
234
- @ Override
235
- public PooledDataBuffer retain () {
236
- this .refCount .incrementAndGet ();
237
- DataBufferUtils .retain (dataBuffer ());
238
- return this ;
239
- }
240
-
241
- @ Override
242
- public boolean release () {
243
- int refCount = this .refCount .decrementAndGet ();
244
- if (refCount == 0 ) {
245
- try {
246
- return DataBufferUtils .release (dataBuffer ());
247
- }
248
- finally {
249
- this .pooledByteBuffer .close ();
250
- }
251
- }
252
- return false ;
253
- }
254
-
255
- @ Override
256
- public DataBuffer slice (int index , int length ) {
257
- DataBuffer slice = dataBuffer ().slice (index , length );
258
- return new UndertowDataBuffer (slice , this .pooledByteBuffer , this .refCount );
259
- }
260
-
261
- }
262
-
263
201
}
0 commit comments