16
16
17
17
package org .springframework .http .codec .multipart ;
18
18
19
- import java .io .IOException ;
20
- import java .nio .channels .AsynchronousFileChannel ;
21
- import java .nio .channels .Channel ;
22
19
import java .nio .charset .Charset ;
23
20
import java .nio .charset .StandardCharsets ;
24
- import java .nio .file .OpenOption ;
25
21
import java .nio .file .Path ;
26
- import java .nio .file .StandardOpenOption ;
27
22
import java .util .Collections ;
28
23
import java .util .List ;
29
24
import java .util .Map ;
@@ -100,10 +95,6 @@ public Flux<Part> read(ResolvableType elementType, ReactiveHttpInputMessage mess
100
95
return Flux .error (new CodecException ("No multipart boundary found in Content-Type: \" " +
101
96
message .getHeaders ().getContentType () + "\" " ));
102
97
}
103
- if (logger .isTraceEnabled ()) {
104
- logger .trace ("Boundary: " + toString (boundary ));
105
- }
106
-
107
98
byte [] boundaryNeedle = concat (BOUNDARY_PREFIX , boundary );
108
99
Flux <DataBuffer > body = skipUntilFirstBoundary (message .getBody (), boundary );
109
100
@@ -148,8 +139,10 @@ private static Flux<DataBuffer> skipUntilFirstBoundary(Flux<DataBuffer> dataBuff
148
139
DataBuffer slice = dataBuffer .retainedSlice (endIdx + 1 , length );
149
140
DataBufferUtils .release (dataBuffer );
150
141
if (logger .isTraceEnabled ()) {
151
- logger .trace ("Found first boundary at " + endIdx + " in " + toString (dataBuffer ));
152
- }
142
+ logger .trace (
143
+ "Found last byte of first boundary (" + toString (boundary )
144
+ + ") at " + endIdx );
145
+ }
153
146
return Mono .just (slice );
154
147
}
155
148
else {
@@ -188,14 +181,14 @@ private static Part toPart(DataBuffer dataBuffer) {
188
181
}
189
182
}
190
183
191
- if (logger .isTraceEnabled ()) {
192
- logger .trace ("Part data: " + toString (dataBuffer ));
193
- }
194
184
int endIdx = HEADER_MATCHER .match (dataBuffer );
195
185
196
186
HttpHeaders headers ;
197
187
DataBuffer body ;
198
188
if (endIdx > 0 ) {
189
+ if (logger .isTraceEnabled ()) {
190
+ logger .trace ("Found last byte of part header at " + endIdx );
191
+ }
199
192
readPosition = dataBuffer .readPosition ();
200
193
int headersLength = endIdx + 1 - (readPosition + HEADER_BODY_SEPARATOR .length );
201
194
DataBuffer headersBuffer = dataBuffer .retainedSlice (readPosition , headersLength );
@@ -204,6 +197,9 @@ private static Part toPart(DataBuffer dataBuffer) {
204
197
headers = toHeaders (headersBuffer );
205
198
}
206
199
else {
200
+ if (logger .isTraceEnabled ()) {
201
+ logger .trace ("No header found" );
202
+ }
207
203
headers = new HttpHeaders ();
208
204
body = DataBufferUtils .retain (dataBuffer );
209
205
}
@@ -252,16 +248,6 @@ private static HttpHeaders toHeaders(DataBuffer dataBuffer) {
252
248
return result ;
253
249
}
254
250
255
-
256
- private static String toString (DataBuffer dataBuffer ) {
257
- byte [] bytes = new byte [dataBuffer .readableByteCount ()];
258
- int j = 0 ;
259
- for (int i = dataBuffer .readPosition (); i < dataBuffer .writePosition (); i ++) {
260
- bytes [j ++] = dataBuffer .getByte (i );
261
- }
262
- return toString (bytes );
263
- }
264
-
265
251
private static String toString (byte [] bytes ) {
266
252
StringBuilder builder = new StringBuilder ();
267
253
for (byte b : bytes ) {
@@ -368,10 +354,6 @@ public String value() {
368
354
369
355
private static class DefaultFilePart extends DefaultPart implements FilePart {
370
356
371
- private static final OpenOption [] FILE_CHANNEL_OPTIONS =
372
- {StandardOpenOption .CREATE , StandardOpenOption .TRUNCATE_EXISTING , StandardOpenOption .WRITE };
373
-
374
-
375
357
public DefaultFilePart (HttpHeaders headers , DataBuffer body ) {
376
358
super (headers , body );
377
359
}
@@ -385,23 +367,9 @@ public String filename() {
385
367
386
368
@ Override
387
369
public Mono <Void > transferTo (Path dest ) {
388
- return Mono .using (() -> AsynchronousFileChannel .open (dest , FILE_CHANNEL_OPTIONS ),
389
- this ::writeBody , this ::close );
390
- }
391
-
392
- private Mono <Void > writeBody (AsynchronousFileChannel channel ) {
393
- return DataBufferUtils .write (content (), channel )
394
- .map (DataBufferUtils ::release )
395
- .then ();
370
+ return DataBufferUtils .write (content (), dest );
396
371
}
397
372
398
- private void close (Channel channel ) {
399
- try {
400
- channel .close ();
401
- }
402
- catch (IOException ignore ) {
403
- }
404
- }
405
373
}
406
374
407
375
}
0 commit comments