228 writeData(fc, 0L, bbuf); 229 if (fc.isOpen()) { 230 fc.close(); 231 } 232 } 233 234 public static void copyFile(File from, File to) throws Exception { 235 if (to.exists()) { 236 if(!to.delete()) { 237 throw new IOException("Could not delete file " + to); 238 } 239 } 240 to.createNewFile(); 241 setReadWritePermission(to); 242 Files.copy(from.toPath(), to.toPath(), REPLACE_EXISTING); 243 } 244 245 // Copy file with bytes deleted or inserted 246 // del -- true, deleted, false, inserted 247 public static void copyFile(File from, File to, boolean del) throws Exception { 248 FileChannel inputChannel = null; 249 FileChannel outputChannel = null; 250 try { 251 inputChannel = new FileInputStream(from).getChannel(); 252 outputChannel = new FileOutputStream(to).getChannel(); 253 long size = inputChannel.size(); 254 int init_size = getFileHeaderSize(inputChannel); 255 outputChannel.transferFrom(inputChannel, 0, init_size); 256 int n = (int)getRandomBetween(0, 1024); 257 if (del) { 258 System.out.println("Delete " + n + " bytes at data start section"); 259 inputChannel.position(init_size + n); 260 outputChannel.transferFrom(inputChannel, init_size, size - init_size - n); 261 } else { 262 System.out.println("Insert " + n + " bytes at data start section"); 263 outputChannel.position(init_size); 264 outputChannel.write(ByteBuffer.wrap(new byte[n])); 265 outputChannel.transferFrom(inputChannel, init_size + n , size - init_size); 266 } 267 } finally { 268 inputChannel.close(); 269 outputChannel.close(); 270 } 271 } 272 273 public static void restoreJsaFile() throws Exception { 274 Files.copy(orgJsaFile.toPath(), jsa.toPath(), REPLACE_EXISTING); 275 } 276 277 public static void setReadWritePermission(File file) throws Exception { 278 if (!file.canRead()) { 279 if (!file.setReadable(true)) { 280 throw new IOException("Cannot modify file " + file + " as readable"); 281 } 282 } 283 if (!file.canWrite()) { 284 if (!file.setWritable(true)) { 285 throw new IOException("Cannot modify file " + file + " as writable"); 286 } 287 } 288 } 289 | 228 writeData(fc, 0L, bbuf); 229 if (fc.isOpen()) { 230 fc.close(); 231 } 232 } 233 234 public static void copyFile(File from, File to) throws Exception { 235 if (to.exists()) { 236 if(!to.delete()) { 237 throw new IOException("Could not delete file " + to); 238 } 239 } 240 to.createNewFile(); 241 setReadWritePermission(to); 242 Files.copy(from.toPath(), to.toPath(), REPLACE_EXISTING); 243 } 244 245 // Copy file with bytes deleted or inserted 246 // del -- true, deleted, false, inserted 247 public static void copyFile(File from, File to, boolean del) throws Exception { 248 try ( 249 FileChannel inputChannel = new FileInputStream(from).getChannel(); 250 FileChannel outputChannel = new FileOutputStream(to).getChannel() 251 ) { 252 long size = inputChannel.size(); 253 int init_size = getFileHeaderSize(inputChannel); 254 outputChannel.transferFrom(inputChannel, 0, init_size); 255 int n = (int)getRandomBetween(0, 1024); 256 if (del) { 257 System.out.println("Delete " + n + " bytes at data start section"); 258 inputChannel.position(init_size + n); 259 outputChannel.transferFrom(inputChannel, init_size, size - init_size - n); 260 } else { 261 System.out.println("Insert " + n + " bytes at data start section"); 262 outputChannel.position(init_size); 263 outputChannel.write(ByteBuffer.wrap(new byte[n])); 264 outputChannel.transferFrom(inputChannel, init_size + n , size - init_size); 265 } 266 } 267 } 268 269 public static void restoreJsaFile() throws Exception { 270 Files.copy(orgJsaFile.toPath(), jsa.toPath(), REPLACE_EXISTING); 271 } 272 273 public static void setReadWritePermission(File file) throws Exception { 274 if (!file.canRead()) { 275 if (!file.setReadable(true)) { 276 throw new IOException("Cannot modify file " + file + " as readable"); 277 } 278 } 279 if (!file.canWrite()) { 280 if (!file.setWritable(true)) { 281 throw new IOException("Cannot modify file " + file + " as writable"); 282 } 283 } 284 } 285 |