Internal copy file method. This caches the original file length, and throws an IOException if the output file length is different from the current input file length. So it may fail if the file changes size. It may also fail with "IllegalArgumentException: Negative size" if the input file is truncate
(final File srcFile, final File destFile, final boolean preserveFileDate)
| 1128 | * position |
| 1129 | */ |
| 1130 | private static void doCopyFile(final File srcFile, final File destFile, final boolean preserveFileDate) |
| 1131 | throws IOException { |
| 1132 | if (destFile.exists() && destFile.isDirectory()) { |
| 1133 | throw new IOException("Destination '" + destFile + "' exists but is a directory"); |
| 1134 | } |
| 1135 | |
| 1136 | FileInputStream fis = null; |
| 1137 | FileOutputStream fos = null; |
| 1138 | FileChannel input = null; |
| 1139 | FileChannel output = null; |
| 1140 | try { |
| 1141 | fis = new FileInputStream(srcFile); |
| 1142 | fos = new FileOutputStream(destFile); |
| 1143 | input = fis.getChannel(); |
| 1144 | output = fos.getChannel(); |
| 1145 | final long size = input.size(); // TODO See IO-386 |
| 1146 | long pos = 0; |
| 1147 | long count = 0; |
| 1148 | while (pos < size) { |
| 1149 | final long remain = size - pos; |
| 1150 | count = remain > FILE_COPY_BUFFER_SIZE ? FILE_COPY_BUFFER_SIZE : remain; |
| 1151 | final long bytesCopied = output.transferFrom(input, pos, count); |
| 1152 | if (bytesCopied == 0) { // IO-385 - can happen if file is truncated after caching the size |
| 1153 | break; // ensure we don't loop forever |
| 1154 | } |
| 1155 | pos += bytesCopied; |
| 1156 | } |
| 1157 | } finally { |
| 1158 | IOUtils.closeQuietly(output, fos, input, fis); |
| 1159 | } |
| 1160 | |
| 1161 | final long srcLen = srcFile.length(); // TODO See IO-386 |
| 1162 | final long dstLen = destFile.length(); // TODO See IO-386 |
| 1163 | if (srcLen != dstLen) { |
| 1164 | throw new IOException("Failed to copy full contents from '" + |
| 1165 | srcFile + "' to '" + destFile + "' Expected length: " + srcLen + " Actual: " + dstLen); |
| 1166 | } |
| 1167 | if (preserveFileDate) { |
| 1168 | destFile.setLastModified(srcFile.lastModified()); |
| 1169 | } |
| 1170 | } |
| 1171 | |
| 1172 | //----------------------------------------------------------------------- |
| 1173 | /** |
no test coverage detected