Overloaded copyFile that is called whenever a Save As is being done, so that the ProgressBar is updated for very large files as well.
(File sourceFile, File targetFile,
long progress, long totalSize)
| 1132 | * so that the ProgressBar is updated for very large files as well. |
| 1133 | */ |
| 1134 | void copyFile(File sourceFile, File targetFile, |
| 1135 | long progress, long totalSize) throws IOException { |
| 1136 | BufferedInputStream from = |
| 1137 | new BufferedInputStream(new FileInputStream(sourceFile)); |
| 1138 | BufferedOutputStream to = |
| 1139 | new BufferedOutputStream(new FileOutputStream(targetFile)); |
| 1140 | byte[] buffer = new byte[16 * 1024]; |
| 1141 | int bytesRead; |
| 1142 | int progRead = 0; |
| 1143 | while ((bytesRead = from.read(buffer)) != -1) { |
| 1144 | to.write(buffer, 0, bytesRead); |
| 1145 | progRead += bytesRead; |
| 1146 | if (progRead >= 512 * 1024) { // to update progress bar every 0.5MB |
| 1147 | progress += progRead; |
| 1148 | //progressBar.setValue((int) Math.min(Math.ceil(progress * 100.0 / totalSize), 100)); |
| 1149 | setProgress((int) (100L * progress / totalSize)); |
| 1150 | progRead = 0; |
| 1151 | } |
| 1152 | } |
| 1153 | // Final update to progress bar |
| 1154 | setProgress((int) (100L * progress / totalSize)); |
| 1155 | |
| 1156 | from.close(); |
| 1157 | to.flush(); |
| 1158 | to.close(); |
| 1159 | |
| 1160 | if (!targetFile.setLastModified(sourceFile.lastModified())) { |
| 1161 | System.err.println("Warning: Could not set modification date/time for " + targetFile); |
| 1162 | } |
| 1163 | if (!targetFile.setExecutable(sourceFile.canExecute())) { |
| 1164 | if (!Platform.isWindows()) { // more of a UNIX thing |
| 1165 | System.err.println("Warning: Could not set permissions for " + targetFile); |
| 1166 | } |
| 1167 | } |
| 1168 | } |
| 1169 | |
| 1170 | |
| 1171 | long copyDir(File sourceDir, File targetDir, |
no test coverage detected