Skip to content

Commit f963059

Browse files
asolntsevxael-fry
authored andcommitted
* [#1966] refactor(ApacheMultipartParser): avoid declaring finalize() methods
** It makes GC work slower. GC puts all objects with "finalize" method to a separate "finalization queue", and GC collects them only during FullGC pause.
1 parent 086a1a4 commit f963059

File tree

1 file changed

+33
-39
lines changed

1 file changed

+33
-39
lines changed

framework/src/play/data/parsing/ApacheMultipartParser.java

+33-39
Original file line numberDiff line numberDiff line change
@@ -437,17 +437,6 @@ public File getStoreLocation() {
437437
}
438438
// ------------------------------------------------------ Protected methods
439439

440-
/**
441-
* Removes the file contents from the temporary storage.
442-
*/
443-
protected void finalize() {
444-
File outputFile = dfos.getFile();
445-
446-
if (outputFile != null && outputFile.exists()) {
447-
outputFile.delete();
448-
}
449-
}
450-
451440
/**
452441
* Creates and returns a {@link java.io.File File} representing a uniquely
453442
* named temporary file in the configured repository path. The lifetime of
@@ -525,37 +514,42 @@ public Map<String, String[]> parse(InputStream body) {
525514
FileItemStream item = iter.next();
526515
FileItem fileItem = new AutoFileItem(item);
527516
try {
528-
Streams.copy(item.openStream(), fileItem.getOutputStream(), true);
529-
} catch (FileUploadIOException e) {
530-
throw (FileUploadException) e.getCause();
531-
} catch (IOException e) {
532-
throw new IOFileUploadException("Processing of " + MULTIPART_FORM_DATA + " request failed. " + e.getMessage(), e);
533-
}
534-
if (fileItem.isFormField()) {
535-
// must resolve encoding
536-
String _encoding = Request.current().encoding; // this is our default
537-
String _contentType = fileItem.getContentType();
538-
if( _contentType != null ) {
539-
HTTP.ContentTypeWithEncoding contentTypeEncoding = HTTP.parseContentType(_contentType);
540-
if( contentTypeEncoding.encoding != null ) {
541-
_encoding = contentTypeEncoding.encoding;
542-
}
517+
try {
518+
Streams.copy(item.openStream(), fileItem.getOutputStream(), true);
519+
} catch (FileUploadIOException e) {
520+
throw (FileUploadException) e.getCause();
521+
} catch (IOException e) {
522+
throw new IOFileUploadException("Processing of " + MULTIPART_FORM_DATA + " request failed. " + e.getMessage(), e);
543523
}
524+
if (fileItem.isFormField()) {
525+
// must resolve encoding
526+
String _encoding = Request.current().encoding; // this is our default
527+
String _contentType = fileItem.getContentType();
528+
if( _contentType != null ) {
529+
HTTP.ContentTypeWithEncoding contentTypeEncoding = HTTP.parseContentType(_contentType);
530+
if( contentTypeEncoding.encoding != null ) {
531+
_encoding = contentTypeEncoding.encoding;
532+
}
533+
}
544534

545-
putMapEntry(result, fileItem.getFieldName(), fileItem.getString( _encoding ));
546-
} else {
547-
@SuppressWarnings("unchecked") List<Upload> uploads = (List<Upload>) Request.current().args.get("__UPLOADS");
548-
if (uploads == null) {
549-
uploads = new ArrayList<Upload>();
550-
Request.current().args.put("__UPLOADS", uploads);
551-
}
552-
try {
553-
uploads.add(new FileUpload(fileItem));
554-
} catch (Exception e) {
555-
// GAE does not support it, we try in memory
556-
uploads.add(new MemoryUpload(fileItem));
535+
putMapEntry(result, fileItem.getFieldName(), fileItem.getString( _encoding ));
536+
} else {
537+
@SuppressWarnings("unchecked") List<Upload> uploads = (List<Upload>) Request.current().args.get("__UPLOADS");
538+
if (uploads == null) {
539+
uploads = new ArrayList<Upload>();
540+
Request.current().args.put("__UPLOADS", uploads);
541+
}
542+
try {
543+
uploads.add(new FileUpload(fileItem));
544+
} catch (Exception e) {
545+
// GAE does not support it, we try in memory
546+
uploads.add(new MemoryUpload(fileItem));
547+
}
548+
putMapEntry(result, fileItem.getFieldName(), fileItem.getFieldName());
557549
}
558-
putMapEntry(result, fileItem.getFieldName(), fileItem.getFieldName());
550+
}
551+
finally {
552+
fileItem.delete();
559553
}
560554
}
561555
} catch (FileUploadIOException e) {

0 commit comments

Comments
 (0)