[ 
https://issues.apache.org/jira/browse/FILEUPLOAD-295?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18005013#comment-18005013
 ] 

Jochen Wiedmann commented on FILEUPLOAD-295:
--------------------------------------------

I have just committed an update, which makes the DiskFileItem implement the 
specification below (Commit f3f27401baea7e8a0be8155fa26afe026d0205af).

A unit test (DiskFileItemTest) should ensure that contract.  Please review.

 

[[email protected]]: Upon review, please keep in mind, that this 
change is not supposed to fulfill your assumptions on the model. The purpose is 
to have something, that is clear, precise, documented, and maintainable. We are 
taking the chance, that Fileupload 2 is not 100% compatible to any previous 
version. Looking forward is more important.

 

{_}State model{_}: Instances of 
{{[DiskFileItem|eclipse-javadoc:%E2%98%82=commons-fileupload2-core/src%5C/main%5C/java=/optional=/true=/=/maven.pomderived=/true=/%3Corg.apache.commons.fileupload2.core%7BDiskFileItem.java%E2%98%83DiskFileItem%E2%98%82DiskFileItem]}}
 are subject to a carefully designed state model. Depending on the so-called 
{{{}[threshold|eclipse-javadoc:%E2%98%82=commons-fileupload2-core/src%5C/main%5C/java=/optional=/true=/=/maven.pomderived=/true=/%3Corg.apache.commons.fileupload2.core%7BDiskFileItem.java%E2%98%83DiskFileItem%E2%98%82%E2%98%82getThreshold%E2%98%82]{}}},
 either of the three models are possible:
 # _threshold = -1_ Uploaded data is never kept in memory. Instead, a temporary 
file is being created immediately. 
{{[isInMemory()|eclipse-javadoc:%E2%98%82=commons-fileupload2-core/src%5C/main%5C/java=/optional=/true=/=/maven.pomderived=/true=/%3Corg.apache.commons.fileupload2.core%7BDiskFileItem.java%E2%98%83DiskFileItem%E2%98%82%E2%98%82isInMemory%E2%98%82]}}
 will always return false, 
{{[getPath()|eclipse-javadoc:%E2%98%82=commons-fileupload2-core/src%5C/main%5C/java=/optional=/true=/=/maven.pomderived=/true=/%3Corg.apache.commons.fileupload2.core%7BDiskFileItem.java%E2%98%83DiskFileItem%E2%98%82%E2%98%82getPath%E2%98%82]}}
 will always return the path of an existing file. The temporary file may be 
empty.
 # _threshold = 0_ Uploaded data is never kept in memory. (Same as 
threshold=-1.) However, the temporary file is only created, if data was 
uploaded. Or, in other words: The uploaded file will never be empty. 
{{[isInMemory()|eclipse-javadoc:%E2%98%82=commons-fileupload2-core/src%5C/main%5C/java=/optional=/true=/=/maven.pomderived=/true=/%3Corg.apache.commons.fileupload2.core%7BDiskFileItem.java%E2%98%83DiskFileItem%E2%98%82%E2%98%82isInMemory%E2%98%82]}}
 will return true, if no data was uploaded, otherwise it will be false. In the 
former case 
{{[getPath()|eclipse-javadoc:%E2%98%82=commons-fileupload2-core/src%5C/main%5C/java=/optional=/true=/=/maven.pomderived=/true=/%3Corg.apache.commons.fileupload2.core%7BDiskFileItem.java%E2%98%83DiskFileItem%E2%98%82%E2%98%82getPath%E2%98%82]}}
 will return, but in the latter case it returns the path of an existing, 
non-empty file.
 # _threshold > 0_ Uploaded data will be kept in memory, if the size is below 
the threshold. If the size is equal to, or above the threshold, then a 
temporary file has been created, and all uploaded data has been transferred to 
that file. 
{{[isInMemory()|eclipse-javadoc:%E2%98%82=commons-fileupload2-core/src%5C/main%5C/java=/optional=/true=/=/maven.pomderived=/true=/%3Corg.apache.commons.fileupload2.core%7BDiskFileItem.java%E2%98%83DiskFileItem%E2%98%82%E2%98%82isInMemory%E2%98%82]}}
 returns true, if the size of the uploaded data is below the threshold. If so, 
{{[getPath()|eclipse-javadoc:%E2%98%82=commons-fileupload2-core/src%5C/main%5C/java=/optional=/true=/=/maven.pomderived=/true=/%3Corg.apache.commons.fileupload2.core%7BDiskFileItem.java%E2%98%83DiskFileItem%E2%98%82%E2%98%82getPath%E2%98%82]}}
 returns null. Otherwise, 
{{[isInMemory()|eclipse-javadoc:%E2%98%82=commons-fileupload2-core/src%5C/main%5C/java=/optional=/true=/=/maven.pomderived=/true=/%3Corg.apache.commons.fileupload2.core%7BDiskFileItem.java%E2%98%83DiskFileItem%E2%98%82%E2%98%82isInMemory%E2%98%82]}}
 returns false, and 
{{[getPath()|eclipse-javadoc:%E2%98%82=commons-fileupload2-core/src%5C/main%5C/java=/optional=/true=/=/maven.pomderived=/true=/%3Corg.apache.commons.fileupload2.core%7BDiskFileItem.java%E2%98%83DiskFileItem%E2%98%82%E2%98%82getPath%E2%98%82]}}
 returns the path of an existing, temporary file. The size of the temporary 
file is equal to, or above the threshold.

> DiskFileItem: getStoreLocation() may return non-existing file
> -------------------------------------------------------------
>
>                 Key: FILEUPLOAD-295
>                 URL: https://issues.apache.org/jira/browse/FILEUPLOAD-295
>             Project: Commons FileUpload
>          Issue Type: Bug
>    Affects Versions: 1.4
>            Reporter: Stephan Markwalder
>            Assignee: Jochen Wiedmann
>            Priority: Minor
>
> *How to reproduce*
>  # Create a DiskFileItem with threshold set to -1 (force save to disk).
>  # Get OutputStream from DiskFileItem.
>  # Close OutputStream without calling any write(...) method (e.g., because 
> the uploaded file is empty).
>  # Test the return value of the following methods:
>  ** isInMemory() --> returns false (OK)
>  ** getSize() --> returns 0 (OK)
>  ** getStoreLocation() --> returns a File object (OK), but the file does not 
> exist (FAILURE).
> I think this is an inconsistency. If isInMemory() returns false adn 
> getStoreLocation() returns a File object, the file should also exist.
> Java code (run with -ea to enable assertions):
> {code:java}
> // create a DiskFileItem
> int sizeThreshold = -1; // always store to disk
> File repository = null; // use temporary folder
> DiskFileItem item = new DiskFileItem("file", "text/plain", false, "file.txt", 
> sizeThreshold, repository);
> OutputStream outputStream = item.getOutputStream();
> // do not write to stream <-- IMPORTANT
> outputStream.close();
> // assert that data has been stored to disk
> assert !item.isInMemory(); // pass
> assert item.getSize() == 0; // pass
> assert item.getStoreLocation() != null; // pass
> assert item.getStoreLocation().isFile(); // fails
> {code}
> When adding a call to outputStream.write(new byte[0]), the behavior changes 
> and the empty file is created on disk.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to