Hallo Leute,
ich kopiere Dateien mit meinem Javatool mittels fileInputStream.getChannel() und fileOutputStream.getChannel(). Habe aber festgestellt, dass dies mit nur ca. 20 % der Leistung die möglich wäre passiert. Einstellungen an buffer und ähnlichen haben auch keine Auswirkung. Gibt es eine Möglichkeit mit Java dies zu beschleunigen. Z.B. anderer Algorithmus?
Hier mal der Code:
ich kopiere Dateien mit meinem Javatool mittels fileInputStream.getChannel() und fileOutputStream.getChannel(). Habe aber festgestellt, dass dies mit nur ca. 20 % der Leistung die möglich wäre passiert. Einstellungen an buffer und ähnlichen haben auch keine Auswirkung. Gibt es eine Möglichkeit mit Java dies zu beschleunigen. Z.B. anderer Algorithmus?
Hier mal der Code:
Code:
public class FileCopy { private long chunckSizeInBytes = 1024 * 1024; //Standard: Buffer 1MB; private JobStatistic statistics; private File source; private File target; private long overallBytesTransfered = 0L; private boolean interupt = false; public FileCopy(File source, File target, JobStatistic statistics) { this.source = source; this.target = target; this.statistics = statistics; } public long copy() { FileInputStream fis = null; FileOutputStream fos = null; try { fis = new FileInputStream(source); fos = new FileOutputStream(target); FileChannel inputChannel = fis.getChannel(); FileChannel outputChannel = fos.getChannel(); transfer(inputChannel, outputChannel, source.length()); fis.close(); fos.close(); target.setLastModified(source.lastModified()); } catch (Exception ex) { statistics.addLogEntry(ex.getMessage()); statistics.addErrorFile(); overallBytesTransfered = 0L; try { // Bei Fehler Versuche die Streams zu schließen fis.close(); fos.close(); } catch (Exception ex2) { } } return overallBytesTransfered; } public void transfer(FileChannel fileChannel, ByteChannel byteChannel, long lengthInBytes) throws IOException { long overallBytesTransfered = 0L; while (overallBytesTransfered < lengthInBytes) { if(!interupt){ long bytesTransfered = 0L; bytesTransfered = fileChannel.transferTo(overallBytesTransfered, Math.min(chunckSizeInBytes, lengthInBytes - overallBytesTransfered), byteChannel); overallBytesTransfered += bytesTransfered; } else break; } this.overallBytesTransfered = overallBytesTransfered; } public void breakCopy(){ interupt = true; } }
Comment