Consistent use of NIO.2 for file read/write interactions
Issue: SPR-15748
This commit is contained in:
@@ -17,7 +17,6 @@
|
||||
package org.springframework.http.codec.multipart;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.channels.Channels;
|
||||
@@ -25,6 +24,7 @@ import java.nio.channels.FileChannel;
|
||||
import java.nio.channels.ReadableByteChannel;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -187,9 +187,9 @@ public class SynchronossPartHttpMessageReader implements HttpMessageReader<Part>
|
||||
}
|
||||
|
||||
private Part createPart(StreamStorage storage, HttpHeaders httpHeaders) {
|
||||
String fileName = MultipartUtils.getFileName(httpHeaders);
|
||||
if (fileName != null) {
|
||||
return new SynchronossFilePart(httpHeaders, storage, fileName, this.bufferFactory);
|
||||
String filename = MultipartUtils.getFileName(httpHeaders);
|
||||
if (filename != null) {
|
||||
return new SynchronossFilePart(httpHeaders, storage, this.bufferFactory, filename);
|
||||
}
|
||||
else if (MultipartUtils.isFormField(httpHeaders, this.context)) {
|
||||
String value = MultipartUtils.readFormParameterValue(storage, httpHeaders);
|
||||
@@ -200,13 +200,6 @@ public class SynchronossPartHttpMessageReader implements HttpMessageReader<Part>
|
||||
}
|
||||
}
|
||||
|
||||
private Part createPart(HttpHeaders httpHeaders, StreamStorage storage) {
|
||||
String fileName = MultipartUtils.getFileName(httpHeaders);
|
||||
return fileName != null ?
|
||||
new SynchronossFilePart(httpHeaders, storage, fileName, this.bufferFactory) :
|
||||
new DefaultSynchronossPart(httpHeaders, storage, this.bufferFactory);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(String message, Throwable cause) {
|
||||
if (this.terminated.getAndIncrement() == 0) {
|
||||
@@ -284,15 +277,18 @@ public class SynchronossPartHttpMessageReader implements HttpMessageReader<Part>
|
||||
|
||||
private static class SynchronossFilePart extends DefaultSynchronossPart implements FilePart {
|
||||
|
||||
public SynchronossFilePart(HttpHeaders headers, StreamStorage storage,
|
||||
String fileName, DataBufferFactory factory) {
|
||||
private final String filename;
|
||||
|
||||
public SynchronossFilePart(
|
||||
HttpHeaders headers, StreamStorage storage, DataBufferFactory factory, String filename) {
|
||||
|
||||
super(headers, storage, factory);
|
||||
this.filename = filename;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String filename() {
|
||||
return MultipartUtils.getFileName(headers());
|
||||
return this.filename;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -301,8 +297,7 @@ public class SynchronossPartHttpMessageReader implements HttpMessageReader<Part>
|
||||
FileChannel output = null;
|
||||
try {
|
||||
input = Channels.newChannel(getStorage().getInputStream());
|
||||
output = new FileOutputStream(destination).getChannel();
|
||||
|
||||
output = FileChannel.open(destination.toPath(), StandardOpenOption.WRITE);
|
||||
long size = (input instanceof FileChannel ? ((FileChannel) input).size() : Long.MAX_VALUE);
|
||||
long totalWritten = 0;
|
||||
while (totalWritten < size) {
|
||||
|
||||
@@ -17,10 +17,10 @@
|
||||
package org.springframework.http.server.reactive;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -85,7 +85,7 @@ public class UndertowServerHttpResponse extends AbstractListenerServerHttpRespon
|
||||
return doCommit(() -> {
|
||||
FileChannel source = null;
|
||||
try {
|
||||
source = new FileInputStream(file).getChannel();
|
||||
source = FileChannel.open(file.toPath(), StandardOpenOption.READ);
|
||||
StreamSinkChannel destination = getUndertowExchange().getResponseChannel();
|
||||
Channels.transferBlocking(destination, source, position, count);
|
||||
return Mono.empty();
|
||||
|
||||
@@ -17,12 +17,12 @@
|
||||
package org.springframework.web.multipart.support;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.Serializable;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
@@ -317,7 +317,7 @@ public class StandardMultipartHttpServletRequest extends AbstractMultipartHttpSe
|
||||
// At least we offloaded the file from memory storage; it'll get deleted
|
||||
// from the temp dir eventually in any case. And for our user's purposes,
|
||||
// we can manually copy it to the requested location as a fallback.
|
||||
FileCopyUtils.copy(this.part.getInputStream(), new FileOutputStream(dest));
|
||||
FileCopyUtils.copy(this.part.getInputStream(), Files.newOutputStream(dest.toPath()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user