mirror of
https://github.com/spring-projects/spring-framework.git
synced 2026-09-26 00:39:01 +00:00
Polishing in PartGenerator
This commit is contained in:
+24
-23
@@ -151,6 +151,7 @@ final class PartGenerator implements MultipartParser.PartListener {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Represents the internal state of the {@link PartGenerator} for creating a single {@link Part}.
|
||||
* {@link State} instances are stateful, and created when a new
|
||||
@@ -179,6 +180,7 @@ final class PartGenerator implements MultipartParser.PartListener {
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* The initial state of the creator. Throws an exception for {@link #onBody(DataBuffer, boolean)}.
|
||||
*/
|
||||
@@ -338,13 +340,34 @@ final class PartGenerator implements MultipartParser.PartListener {
|
||||
|
||||
private long byteCount;
|
||||
|
||||
|
||||
public FileState(HttpHeaders headers, Path folder) {
|
||||
this.headers = headers;
|
||||
this.file = createFile(folder);
|
||||
this.outputStream = createOutputStream(this.file);
|
||||
}
|
||||
|
||||
private Path createFile(Path directory) {
|
||||
try {
|
||||
Path tempFile = Files.createTempFile(directory, null, ".multipart");
|
||||
if (logger.isTraceEnabled()) {
|
||||
logger.trace("Storing multipart data in file " + tempFile);
|
||||
}
|
||||
return tempFile;
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new UncheckedIOException("Could not create temp file in " + directory, ex);
|
||||
}
|
||||
}
|
||||
|
||||
private OutputStream createOutputStream(Path file) {
|
||||
try {
|
||||
return Files.newOutputStream(file, StandardOpenOption.WRITE);
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new UncheckedIOException("Could not write to temp file " + file, ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBody(DataBuffer dataBuffer, boolean last) {
|
||||
this.byteCount += dataBuffer.readableByteCount();
|
||||
@@ -375,28 +398,6 @@ final class PartGenerator implements MultipartParser.PartListener {
|
||||
}
|
||||
}
|
||||
|
||||
private Path createFile(Path directory) {
|
||||
try {
|
||||
Path tempFile = Files.createTempFile(directory, null, ".multipart");
|
||||
if (logger.isTraceEnabled()) {
|
||||
logger.trace("Storing multipart data in file " + tempFile);
|
||||
}
|
||||
return tempFile;
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new UncheckedIOException("Could not create temp file in " + directory, ex);
|
||||
}
|
||||
}
|
||||
|
||||
private OutputStream createOutputStream(Path file) {
|
||||
try {
|
||||
return Files.newOutputStream(file, StandardOpenOption.WRITE);
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new UncheckedIOException("Could not write to temp file " + file, ex);
|
||||
}
|
||||
}
|
||||
|
||||
private void writeBuffer(DataBuffer dataBuffer) {
|
||||
try (InputStream in = dataBuffer.asInputStream()) {
|
||||
in.transferTo(this.outputStream);
|
||||
|
||||
Reference in New Issue
Block a user