[
https://issues.apache.org/jira/browse/VFS-842?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17767144#comment-17767144
]
Gary D. Gregory commented on VFS-842:
-------------------------------------
FileObjects are cached. Manage each with a try-with-resources block. For
example:
{code:java}
try (FileObject fileToCopy = fsManager.resolveFile(source);
FileObject destinationFilePath = fsManager.resolveFile(destination)) {
System.out.println("Source File Size:
"+fileToCopy.getContent().getSize());
System.out.println("Destination File Size Before Copy:
"+destinationFilePath.getContent().getSize());
destinationFilePath.copyFrom(fileToCopy, Selectors.SELECT_ALL);
}
try (FileObject destinationFilePath = fsManager.resolveFile(destination)) {
System.out.println("Destination File Size After Copy:
"+destinationFilePath.getContent().getSize());
}
{code}
If that does not do it, alternatively, try calling
{{org.apache.commons.vfs2.FilesCache.clear(FileSystem)}} before calling
{{fsManager.resolveFile(destination)}} again.
You can also provide a reproducible failing unit test as a patch file or GitHub
PR.
> CopyFrom method doesn't update File Size after replacing file
> -------------------------------------------------------------
>
> Key: VFS-842
> URL: https://issues.apache.org/jira/browse/VFS-842
> Project: Commons VFS
> Issue Type: Bug
> Affects Versions: 2.9.0
> Reporter: Usman Ashraf Bajwah
> Priority: Major
>
> Consider the scenario where we have two folders source and destination both
> have file with SAME NAME use copyFrom method file will be copied (replaced)
> in the destination folder alright but size of the file WILL NOT BE UPDATED IF
> THE SIZE OF DESTINATION FILE BEFORE COPY WAS GREATER THAN THE SIZE OF SOURCE
> FILE. In order to produce this issue. Use following code. and also take 2
> image files in two different folders. Also make sure the destination folder
> contains image with higher size.
>
>
> {color:#7f0055}public{color}{color:#000000}
> {color}{color:#7f0055}static{color}{color:#000000}
> {color}{color:#7f0055}void{color}{color:#000000} main(String[]
> {color}{color:#6a3e3e}args{color}{color:#000000})
> {color}{color:#7f0055}throws{color}{color:#000000} FileSystemException,
> UnsupportedEncodingException {{color}
> {color:#7f0055}try{color}{color:#000000} {{color}
> {color:#000000} String {color}{color:#6a3e3e}source{color}{color:#000000} =
> {color}{color:#2a00ff}"C:
> SourceFolder
> Usman.jpg"{color}{color:#000000};{color}
> {color:#000000} String
> {color}{color:#6a3e3e}destination{color}{color:#000000} =
> {color}{color:#2a00ff}"C:
> TargetFolder
> Usman.jpg"{color}{color:#000000};{color}
> {color:#000000} FileSystemManager
> {color}{color:#6a3e3e}fsManager{color}{color:#000000} =
> VFS.{color}{color:#000000}getManager{color}{color:#000000}();{color}
> {color:#000000} FileObject
> {color}{color:#6a3e3e}fileToCopy{color}{color:#000000} =
> {color}{color:#6a3e3e}fsManager{color}{color:#000000}.resolveFile({color}{color:#6a3e3e}source{color}{color:#000000});{color}
> {color:#000000} FileObject
> {color}{color:#6a3e3e}destinationFilePath{color}{color:#000000} =
> {color}{color:#6a3e3e}fsManager{color}{color:#000000}.resolveFile({color}{color:#6a3e3e}destination{color}{color:#000000});{color}
> {color:#000000}
> System.{color}{color:#0000c0}out{color}{color:#000000}.println({color}{color:#2a00ff}"Source
> File Size:
> "{color}{color:#000000}+{color}{color:#6a3e3e}fileToCopy{color}{color:#000000}.getContent().getSize());{color}
> {color:#000000}
> System.{color}{color:#0000c0}out{color}{color:#000000}.println({color}{color:#2a00ff}"Destination
> File Size Before Copy:
> "{color}{color:#000000}+{color}{color:#6a3e3e}destinationFilePath{color}{color:#000000}.getContent().getSize());{color}
> {color:#6a3e3e}destinationFilePath{color}{color:#000000}.copyFrom({color}{color:#6a3e3e}fileToCopy{color}{color:#000000},
> Selectors.{color}{color:#0000c0}SELECT_ALL{color}{color:#000000});{color}
> {color:#000000}
> System.{color}{color:#0000c0}out{color}{color:#000000}.println({color}{color:#2a00ff}"Destination
> File Size After Copy:
> "{color}{color:#000000}+{color}{color:#6a3e3e}destinationFilePath{color}{color:#000000}.getContent().getSize());{color}
> {color:#000000} }{color}{color:#7f0055}catch{color}{color:#000000}(Exception
> {color}{color:#6a3e3e}ex{color}{color:#000000}) {{color}
> {color:#6a3e3e}ex{color}{color:#000000}.printStackTrace();{color}
> {color:#000000} } {color}
> {color:#000000} }{color}
> {color:#000000}The output it shows : {color}
> {color:#000000}Source File Size: 17259{color}
> {color:#000000}Destination File Size Before Copy: 110696{color}
> {color:#000000}Destination File Size After Copy: 110696{color}
> {color:#000000}I have verified the file is successfully copied but on File
> explorer the old size is showing. It seems there is some issue while we write
> the content of the file. Please let me know if you need any information. The
> documentation [here
> |https://commons.apache.org/proper/commons-vfs/commons-vfs2/apidocs/org/apache/commons/vfs2/FileObject.html#copyFrom-org.apache.commons.vfs2.FileObject-org.apache.commons.vfs2.FileSelector-]says
> the destination file is deleted first but i don't see in code while
> debugging that file is actually deleting. or I am missing something but issue
> is definitely there.{color}
>
--
This message was sent by Atlassian Jira
(v8.20.10#820010)