Skip to content

Commit 91f51b8

Browse files
committed
Fixed bug in the copyTo() method in the File class
1 parent d2206f3 commit 91f51b8

1 file changed

Lines changed: 15 additions & 22 deletions

File tree

‎src/javaxt/io/File.java‎

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -553,35 +553,28 @@ public boolean copyTo(javaxt.io.File Destination, boolean Overwrite){
553553
Destination.getParentDirectory().create();
554554

555555
//Copy File
556-
ReadableByteChannel inputChannel = null;
557-
WritableByteChannel outputChannel = null;
558556
try{
559-
FileInputStream input = new FileInputStream(File);
560-
FileOutputStream output = new FileOutputStream(Destination.toFile());
561-
inputChannel = Channels.newChannel(input);
562-
outputChannel = Channels.newChannel(output);
563-
final java.nio.ByteBuffer buffer = java.nio.ByteBuffer.allocateDirect(bufferSize);
564-
565-
while (inputChannel.read(buffer) != -1) {
566-
buffer.flip();
567-
outputChannel.write(buffer);
568-
buffer.compact();
569-
570-
}
571-
buffer.flip();
572-
while (buffer.hasRemaining()) {
573-
outputChannel.write(buffer);
557+
try (
558+
ReadableByteChannel inputChannel = Channels.newChannel(new FileInputStream(File));
559+
WritableByteChannel outputChannel = Channels.newChannel(new FileOutputStream(Destination.toFile()));
560+
){
561+
562+
final java.nio.ByteBuffer buffer = java.nio.ByteBuffer.allocateDirect(bufferSize);
563+
while (inputChannel.read(buffer) != -1) {
564+
((java.nio.Buffer) buffer).flip();
565+
outputChannel.write(buffer);
566+
buffer.compact();
567+
}
568+
((java.nio.Buffer) buffer).flip();
569+
while (buffer.hasRemaining()) {
570+
outputChannel.write(buffer);
571+
}
574572
}
575573

576-
inputChannel.close();
577-
outputChannel.close();
578-
579574
Destination.toFile().setLastModified(File.lastModified());
580575
return true;
581576
}
582577
catch(Exception e){
583-
try{inputChannel.close();}catch(Exception ex){}
584-
try{outputChannel.close();}catch(Exception ex){}
585578
return false;
586579
}
587580
}

0 commit comments

Comments
 (0)