[#1730] Correctly release resources in MixedFileUpload after switching from Memory to Disk

This commit is contained in:
Norman Maurer 2013-08-12 07:23:00 +02:00
parent b9e279064d
commit cc12e40065

View File

@ -57,10 +57,14 @@ public class MixedFileUpload implements FileUpload {
.getContentType(), fileUpload .getContentType(), fileUpload
.getContentTransferEncoding(), fileUpload.getCharset(), .getContentTransferEncoding(), fileUpload.getCharset(),
definedSize); definedSize);
if (((MemoryFileUpload) fileUpload).getByteBuf() != null) {
diskFileUpload.addContent(((MemoryFileUpload) fileUpload) ByteBuf data = fileUpload.getByteBuf();
.getByteBuf(), false); if (data != null && data.isReadable()) {
diskFileUpload.addContent(data.retain(), false);
} }
// release old upload
fileUpload.release();
fileUpload = diskFileUpload; fileUpload = diskFileUpload;
} }
} }
@ -141,12 +145,16 @@ public class MixedFileUpload implements FileUpload {
public void setContent(ByteBuf buffer) throws IOException { public void setContent(ByteBuf buffer) throws IOException {
if (buffer.readableBytes() > limitSize) { if (buffer.readableBytes() > limitSize) {
if (fileUpload instanceof MemoryFileUpload) { if (fileUpload instanceof MemoryFileUpload) {
FileUpload memoryUpload = fileUpload;
// change to Disk // change to Disk
fileUpload = new DiskFileUpload(fileUpload fileUpload = new DiskFileUpload(memoryUpload
.getName(), fileUpload.getFilename(), fileUpload .getName(), memoryUpload.getFilename(), memoryUpload
.getContentType(), fileUpload .getContentType(), memoryUpload
.getContentTransferEncoding(), fileUpload.getCharset(), .getContentTransferEncoding(), memoryUpload.getCharset(),
definedSize); definedSize);
// release old upload
memoryUpload.release();
} }
} }
fileUpload.setContent(buffer); fileUpload.setContent(buffer);
@ -156,12 +164,17 @@ public class MixedFileUpload implements FileUpload {
public void setContent(File file) throws IOException { public void setContent(File file) throws IOException {
if (file.length() > limitSize) { if (file.length() > limitSize) {
if (fileUpload instanceof MemoryFileUpload) { if (fileUpload instanceof MemoryFileUpload) {
FileUpload memoryUpload = fileUpload;
// change to Disk // change to Disk
fileUpload = new DiskFileUpload(fileUpload fileUpload = new DiskFileUpload(memoryUpload
.getName(), fileUpload.getFilename(), fileUpload .getName(), memoryUpload.getFilename(), memoryUpload
.getContentType(), fileUpload .getContentType(), memoryUpload
.getContentTransferEncoding(), fileUpload.getCharset(), .getContentTransferEncoding(), memoryUpload.getCharset(),
definedSize); definedSize);
// release old upload
memoryUpload.release();
} }
} }
fileUpload.setContent(file); fileUpload.setContent(file);
@ -170,12 +183,17 @@ public class MixedFileUpload implements FileUpload {
@Override @Override
public void setContent(InputStream inputStream) throws IOException { public void setContent(InputStream inputStream) throws IOException {
if (fileUpload instanceof MemoryFileUpload) { if (fileUpload instanceof MemoryFileUpload) {
FileUpload memoryUpload = fileUpload;
// change to Disk // change to Disk
fileUpload = new DiskFileUpload(fileUpload fileUpload = new DiskFileUpload(fileUpload
.getName(), fileUpload.getFilename(), fileUpload .getName(), fileUpload.getFilename(), fileUpload
.getContentType(), fileUpload .getContentType(), fileUpload
.getContentTransferEncoding(), fileUpload.getCharset(), .getContentTransferEncoding(), fileUpload.getCharset(),
definedSize); definedSize);
// release old upload
memoryUpload.release();
} }
fileUpload.setContent(inputStream); fileUpload.setContent(inputStream);
} }