Polishing in PartGenerator

This commit is contained in:
rstoyanchev
2026-06-17 13:05:10 +01:00
parent 65fe0f1d2f
commit 2bc0ee7ec1
@@ -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);