From d79f83f3764cd0d25082a2e8be2c850890a22beb Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Mon, 16 Dec 2019 14:49:18 +0100 Subject: [PATCH] Add ChopJob.run() code (no crypt yet) --- sample_file.chp | 5 - src/eu/steffo/cleaver/Test.java | 21 +++++ .../cleaver/gui/rows/FileSelectRow.java | 1 + src/eu/steffo/cleaver/logic/ChopJob.java | 93 ++++++++++++++++++- .../logic/compress/CompressConfig.java | 7 ++ .../cleaver/logic/crypt/CryptConfig.java | 7 ++ .../cleaver/logic/progress/ErrorProgress.java | 14 +++ .../logic/split/SplitByPartsConfig.java | 17 ++++ .../logic/split/SplitBySizeConfig.java | 26 +++++- .../cleaver/logic/split/SplitConfig.java | 4 + .../logic/split/SplitFileOutputStream.java | 45 +++++++++ 11 files changed, 229 insertions(+), 11 deletions(-) delete mode 100644 sample_file.chp create mode 100644 src/eu/steffo/cleaver/Test.java create mode 100644 src/eu/steffo/cleaver/logic/progress/ErrorProgress.java create mode 100644 src/eu/steffo/cleaver/logic/split/SplitFileOutputStream.java diff --git a/sample_file.chp b/sample_file.chp deleted file mode 100644 index 7bfba25..0000000 --- a/sample_file.chp +++ /dev/null @@ -1,5 +0,0 @@ - - - - 10 - \ No newline at end of file diff --git a/src/eu/steffo/cleaver/Test.java b/src/eu/steffo/cleaver/Test.java new file mode 100644 index 0000000..b6ad08e --- /dev/null +++ b/src/eu/steffo/cleaver/Test.java @@ -0,0 +1,21 @@ +package eu.steffo.cleaver; + +import eu.steffo.cleaver.logic.ChopJob; +import eu.steffo.cleaver.logic.Job; +import eu.steffo.cleaver.logic.split.SplitFileOutputStream; + +import java.io.IOException; +import java.io.OutputStream; + +public class Test { + public static void main(String[] args) { + OutputStream stream = new SplitFileOutputStream("test", 64); + for(int i = 0; i < 256; i++) { + try { + stream.write(i); + } catch (IOException e) { + e.printStackTrace(); + } + } + } +} diff --git a/src/eu/steffo/cleaver/gui/rows/FileSelectRow.java b/src/eu/steffo/cleaver/gui/rows/FileSelectRow.java index b2b8261..3622ef3 100644 --- a/src/eu/steffo/cleaver/gui/rows/FileSelectRow.java +++ b/src/eu/steffo/cleaver/gui/rows/FileSelectRow.java @@ -17,6 +17,7 @@ public class FileSelectRow extends Row { fileChooser = new JFileChooser(); fileChooser.setMultiSelectionEnabled(true); + fileChooser.setCurrentDirectory(new File(".")); this.add(Box.createHorizontalStrut(8)); diff --git a/src/eu/steffo/cleaver/logic/ChopJob.java b/src/eu/steffo/cleaver/logic/ChopJob.java index 195ac42..718451b 100644 --- a/src/eu/steffo/cleaver/logic/ChopJob.java +++ b/src/eu/steffo/cleaver/logic/ChopJob.java @@ -1,11 +1,29 @@ package eu.steffo.cleaver.logic; +import eu.steffo.cleaver.errors.ChpFileError; +import eu.steffo.cleaver.errors.ProgrammingError; import eu.steffo.cleaver.logic.compress.CompressConfig; import eu.steffo.cleaver.logic.crypt.CryptConfig; +import eu.steffo.cleaver.logic.progress.ErrorProgress; import eu.steffo.cleaver.logic.progress.FinishedProgress; +import eu.steffo.cleaver.logic.progress.WorkingProgress; +import eu.steffo.cleaver.logic.split.SplitByPartsConfig; +import eu.steffo.cleaver.logic.split.SplitBySizeConfig; import eu.steffo.cleaver.logic.split.SplitConfig; +import eu.steffo.cleaver.logic.split.SplitFileOutputStream; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.xml.sax.SAXException; -import java.io.File; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; +import java.io.*; +import java.util.zip.DeflaterOutputStream; public class ChopJob extends Job { @@ -27,7 +45,78 @@ public class ChopJob extends Job { @Override public void run() { - this.setProgress(new FinishedProgress()); + try { + InputStream inputStream = new FileInputStream(file); + long partSize; + + if(splitConfig instanceof SplitBySizeConfig) { + partSize = ((SplitBySizeConfig)splitConfig).getSize(); + } + else if(splitConfig instanceof SplitByPartsConfig) { + partSize = ((SplitByPartsConfig)splitConfig).getParts(); + } + else { + partSize = file.length(); + } + OutputStream outputStream = new SplitFileOutputStream(file.getName(), partSize); + + if(compressConfig != null) { + outputStream = new DeflaterOutputStream(outputStream); + } + + if(cryptConfig != null) { + //TODO + } + + //Create the .chp file + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = factory.newDocumentBuilder(); + try { + builder = factory.newDocumentBuilder(); + } catch (ParserConfigurationException e) { + throw new ProgrammingError(); + } + Document doc = builder.newDocument(); + Element root = doc.createElement("Cleaver"); + doc.appendChild(root); + + if(splitConfig != null) { + root.appendChild(splitConfig.toElement(doc)); + } + if(compressConfig != null) { + root.appendChild(compressConfig.toElement(doc)); + } + if(cryptConfig != null) { + root.appendChild(cryptConfig.toElement(doc)); + } + + TransformerFactory transformerFactory = TransformerFactory.newInstance(); + Transformer transformer = transformerFactory.newTransformer(); + DOMSource source = new DOMSource(doc); + StreamResult result = new StreamResult(String.format("%s.chp", file.getName())); + transformer.transform(source, result); + + //Actually run the job + int bytesUntilNextUpdate = 1024; + this.setProgress(new WorkingProgress()); + + int i; + while((i = inputStream.read()) != -1) { + outputStream.write(i); + bytesUntilNextUpdate -= 1; + if(bytesUntilNextUpdate <= 0) { + this.setProgress(new WorkingProgress((float)file.length() / (float)partSize)); + bytesUntilNextUpdate = 1024; + } + } + + inputStream.close(); + outputStream.close(); + + this.setProgress(new FinishedProgress()); + } catch (Throwable e) { + this.setProgress(new ErrorProgress(e)); + } } public SplitConfig getSplitConfig() { diff --git a/src/eu/steffo/cleaver/logic/compress/CompressConfig.java b/src/eu/steffo/cleaver/logic/compress/CompressConfig.java index 813c894..6ce0649 100644 --- a/src/eu/steffo/cleaver/logic/compress/CompressConfig.java +++ b/src/eu/steffo/cleaver/logic/compress/CompressConfig.java @@ -1,8 +1,15 @@ package eu.steffo.cleaver.logic.compress; +import org.w3c.dom.Document; +import org.w3c.dom.Element; + public class CompressConfig { @Override public String toString() { return "Yes"; } + + public Element toElement(Document doc) { + return doc.createElement("Compress"); + } } diff --git a/src/eu/steffo/cleaver/logic/crypt/CryptConfig.java b/src/eu/steffo/cleaver/logic/crypt/CryptConfig.java index a407ab0..3bc6b12 100644 --- a/src/eu/steffo/cleaver/logic/crypt/CryptConfig.java +++ b/src/eu/steffo/cleaver/logic/crypt/CryptConfig.java @@ -1,5 +1,8 @@ package eu.steffo.cleaver.logic.crypt; +import org.w3c.dom.Document; +import org.w3c.dom.Element; + public class CryptConfig { protected String key; @@ -15,4 +18,8 @@ public class CryptConfig { public String toString() { return "••••••••"; } + + public Element toElement(Document doc) { + return doc.createElement("Crypt"); + } } diff --git a/src/eu/steffo/cleaver/logic/progress/ErrorProgress.java b/src/eu/steffo/cleaver/logic/progress/ErrorProgress.java new file mode 100644 index 0000000..76c5f94 --- /dev/null +++ b/src/eu/steffo/cleaver/logic/progress/ErrorProgress.java @@ -0,0 +1,14 @@ +package eu.steffo.cleaver.logic.progress; + +public class ErrorProgress extends Progress { + public final Throwable error; + + public ErrorProgress(Throwable error) { + this.error = error; + } + + @Override + public String toString() { + return "Error: " + error.getMessage(); + } +} diff --git a/src/eu/steffo/cleaver/logic/split/SplitByPartsConfig.java b/src/eu/steffo/cleaver/logic/split/SplitByPartsConfig.java index 14a3ef9..b89e3d7 100644 --- a/src/eu/steffo/cleaver/logic/split/SplitByPartsConfig.java +++ b/src/eu/steffo/cleaver/logic/split/SplitByPartsConfig.java @@ -1,5 +1,9 @@ package eu.steffo.cleaver.logic.split; +import org.w3c.dom.Attr; +import org.w3c.dom.Document; +import org.w3c.dom.Element; + public class SplitByPartsConfig extends SplitConfig { protected int parts; @@ -15,4 +19,17 @@ public class SplitByPartsConfig extends SplitConfig { public String toString() { return String.format("%d parts", this.parts); } + + @Override + public Element toElement(Document doc) { + Element element = doc.createElement("Split"); + + Attr attr = doc.createAttribute("mode"); + attr.setValue("by-parts"); + element.setAttributeNode(attr); + + element.setTextContent(Integer.toString(parts)); + + return element; + } } diff --git a/src/eu/steffo/cleaver/logic/split/SplitBySizeConfig.java b/src/eu/steffo/cleaver/logic/split/SplitBySizeConfig.java index b3e03c8..09c1113 100644 --- a/src/eu/steffo/cleaver/logic/split/SplitBySizeConfig.java +++ b/src/eu/steffo/cleaver/logic/split/SplitBySizeConfig.java @@ -1,13 +1,18 @@ package eu.steffo.cleaver.logic.split; -public class SplitBySizeConfig extends SplitConfig { - protected int size; +import org.w3c.dom.Attr; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Text; - public SplitBySizeConfig(int size) { +public class SplitBySizeConfig extends SplitConfig { + protected long size; + + public SplitBySizeConfig(long size) { this.size = size; } - public int getSize() { + public long getSize() { return size; } @@ -15,4 +20,17 @@ public class SplitBySizeConfig extends SplitConfig { public String toString() { return String.format("%d bytes", this.size); } + + @Override + public Element toElement(Document doc) { + Element element = doc.createElement("Split"); + + Attr attr = doc.createAttribute("mode"); + attr.setValue("by-size"); + element.setAttributeNode(attr); + + element.setTextContent(Long.toString(size)); + + return element; + } } diff --git a/src/eu/steffo/cleaver/logic/split/SplitConfig.java b/src/eu/steffo/cleaver/logic/split/SplitConfig.java index 26644ff..7b961b1 100644 --- a/src/eu/steffo/cleaver/logic/split/SplitConfig.java +++ b/src/eu/steffo/cleaver/logic/split/SplitConfig.java @@ -1,4 +1,8 @@ package eu.steffo.cleaver.logic.split; +import org.w3c.dom.Document; +import org.w3c.dom.Element; + public abstract class SplitConfig { + public abstract Element toElement(Document doc); } diff --git a/src/eu/steffo/cleaver/logic/split/SplitFileOutputStream.java b/src/eu/steffo/cleaver/logic/split/SplitFileOutputStream.java new file mode 100644 index 0000000..31815f1 --- /dev/null +++ b/src/eu/steffo/cleaver/logic/split/SplitFileOutputStream.java @@ -0,0 +1,45 @@ +package eu.steffo.cleaver.logic.split; + +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; + +public class SplitFileOutputStream extends OutputStream { + protected String fileBaseName; + protected FileOutputStream currentFileOutputStream; + protected long currentByteCount; + protected long maximumByteCount; + protected int currentFileCount; + + public SplitFileOutputStream(String fileBaseName, long maximumByteCount) { + this.fileBaseName = fileBaseName; + this.maximumByteCount = maximumByteCount; + this.currentByteCount = 0; + this.currentFileCount = 0; + this.currentFileOutputStream = null; + } + + protected void createNextFileOutputStream() throws IOException { + if(currentFileOutputStream != null) { + currentFileOutputStream.close(); + } + + currentFileCount += 1; + currentFileOutputStream = new FileOutputStream(String.format("%s.c%d", fileBaseName, currentFileCount)); + currentByteCount = 0; + } + + @Override + public void write(int b) throws IOException { + if(currentFileOutputStream == null || currentByteCount >= maximumByteCount) { + createNextFileOutputStream(); + } + currentFileOutputStream.write(b); + currentByteCount += 1; + } + + @Override + public void close() throws IOException { + currentFileOutputStream.close(); + } +}