From 7f274721ba2953b889a9ef91586cf3cf0077d3af Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Wed, 8 Jan 2020 13:49:45 +0100 Subject: [PATCH] Complete chop process --- src/eu/steffo/cleaver/Test.java | 20 ------------- src/eu/steffo/cleaver/logic/job/ChopJob.java | 10 ++++--- .../output/CleaverCryptOutputStream.java | 6 ++-- .../output/CleaverForkFileOutputStream.java | 22 +++++++++------ .../output/CleaverSimpleFileOutputStream.java | 28 +++++++++++++++---- .../output/CleaverSplitFileOutputStream.java | 24 ++++++++++------ 6 files changed, 60 insertions(+), 50 deletions(-) delete mode 100644 src/eu/steffo/cleaver/Test.java diff --git a/src/eu/steffo/cleaver/Test.java b/src/eu/steffo/cleaver/Test.java deleted file mode 100644 index fd6203f..0000000 --- a/src/eu/steffo/cleaver/Test.java +++ /dev/null @@ -1,20 +0,0 @@ -package eu.steffo.cleaver; - -import eu.steffo.cleaver.logic.stream.output.CleaverSplitFileOutputStream; - -import java.io.IOException; -import java.io.OutputStream; - -//TODO: delet this -public class Test { - public static void main(String[] args) { - OutputStream stream = new CleaverSplitFileOutputStream("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/logic/job/ChopJob.java b/src/eu/steffo/cleaver/logic/job/ChopJob.java index 90ac72d..a391e2e 100644 --- a/src/eu/steffo/cleaver/logic/job/ChopJob.java +++ b/src/eu/steffo/cleaver/logic/job/ChopJob.java @@ -18,6 +18,8 @@ import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import java.io.*; +import java.nio.file.Path; +import java.nio.file.Paths; /** * A {@link Job} that converts regular files into chopped (*.chp + *.cXX) files. @@ -40,7 +42,7 @@ public class ChopJob extends Job { */ public ChopJob(File file, ISplitConfig splitConfig, ICryptConfig cryptConfig, ICompressConfig compressConfig, Runnable onProgressChange) { super(onProgressChange); - this.file = file; + this.file = file.getAbsoluteFile(); this.splitConfig = splitConfig; this.cryptConfig = cryptConfig; this.compressConfig = compressConfig; @@ -91,13 +93,13 @@ public class ChopJob extends Job { OutputStream outputStream; if(splitConfig instanceof SizeConfig) { - outputStream = new CleaverSplitFileOutputStream(file.getAbsolutePath(), ((SizeConfig)splitConfig).getPartSize()); + outputStream = new CleaverSplitFileOutputStream(file, ((SizeConfig)splitConfig).getPartSize()); } else if(splitConfig instanceof PartsConfig) { - outputStream = new CleaverForkFileOutputStream(file.getAbsolutePath(), ((PartsConfig)splitConfig).getPartCount()); + outputStream = new CleaverForkFileOutputStream(file, ((PartsConfig)splitConfig).getPartCount()); } else { - outputStream = new CleaverSimpleFileOutputStream(file.getAbsolutePath()); + outputStream = new CleaverSimpleFileOutputStream(file); } if(compressConfig instanceof DeflateConfig) { diff --git a/src/eu/steffo/cleaver/logic/stream/output/CleaverCryptOutputStream.java b/src/eu/steffo/cleaver/logic/stream/output/CleaverCryptOutputStream.java index ef45cae..309a722 100644 --- a/src/eu/steffo/cleaver/logic/stream/output/CleaverCryptOutputStream.java +++ b/src/eu/steffo/cleaver/logic/stream/output/CleaverCryptOutputStream.java @@ -54,7 +54,7 @@ public class CleaverCryptOutputStream extends FilterOutputStream implements ICle /** * The length in bits of the key to be generated with the {@link #KEY_DERIVATION_ALGORITHM}. */ - public static final int KEY_LENGTH = 512; + public static final int KEY_LENGTH = 256; /** * @return The full transformation string as required by {@link Cipher#getInstance(String)}. @@ -70,7 +70,7 @@ public class CleaverCryptOutputStream extends FilterOutputStream implements ICle * @see SecureRandom */ protected byte[] generateSecure(int size) { - byte[] salt = new byte[8]; + byte[] salt = new byte[size]; SecureRandom secureRandom = new SecureRandom(); secureRandom.nextBytes(salt); return salt; @@ -111,7 +111,7 @@ public class CleaverCryptOutputStream extends FilterOutputStream implements ICle SecretKey aes = new SecretKeySpec(generatePasswordKey(key).getEncoded(), ENCRYPTION_ALGORITHM); //Generate the initialization vector - IvParameterSpec iv = generateIV(1); + IvParameterSpec iv = generateIV(16); //Init the cipher instance cipher.init(Cipher.ENCRYPT_MODE, aes, iv); diff --git a/src/eu/steffo/cleaver/logic/stream/output/CleaverForkFileOutputStream.java b/src/eu/steffo/cleaver/logic/stream/output/CleaverForkFileOutputStream.java index 1a1fd2c..dc66bd1 100644 --- a/src/eu/steffo/cleaver/logic/stream/output/CleaverForkFileOutputStream.java +++ b/src/eu/steffo/cleaver/logic/stream/output/CleaverForkFileOutputStream.java @@ -12,21 +12,21 @@ import java.io.*; * Bytes are written one at a time to the files in a round-robin format until the stream is exausted. */ public class CleaverForkFileOutputStream extends OutputStream implements ICleaverOutputStream { - private final String fileBaseName; + private final File baseFile; private FileOutputStream[] fileOutputStreams; private int writeTo; private long partSize; /** * Construct a CleaverForkFileOutputStream. - * @param fileBaseName The name of the files without the extension. If it is {@literal example}, the created files will be {@literal example.c1}, {@literal example.c2}, and so on. + * @param baseFile The name of the files without the extension. If it is {@literal example}, the created files will be {@literal example.c1}, {@literal example.c2}, and so on. * @param parts The number of parts to be created. */ - public CleaverForkFileOutputStream(String fileBaseName, int parts) throws FileNotFoundException { - this.fileBaseName = fileBaseName; + public CleaverForkFileOutputStream(File baseFile, int parts) throws FileNotFoundException { + this.baseFile = baseFile; this.fileOutputStreams = new FileOutputStream[parts]; for(int i = 0; i < parts; i++) { - File file = new File(String.format("%s.c%s", fileBaseName, i)); + File file = new File(String.format("%s.c%s", baseFile.getAbsolutePath(), i)); this.fileOutputStreams[i] = new FileOutputStream(file); } this.writeTo = 0; @@ -36,7 +36,7 @@ public class CleaverForkFileOutputStream extends OutputStream implements ICleave @Override public Element toElement(Document doc) { Element element = doc.createElement("Fork"); - element.setTextContent(fileBaseName); + element.setTextContent(baseFile.getName()); Attr partSizeAttr = doc.createAttribute("part-size"); partSizeAttr.setValue(Long.toString(partSize)); @@ -60,10 +60,14 @@ public class CleaverForkFileOutputStream extends OutputStream implements ICleave } /** - * @return The name of the files without the extension. If it is {@literal example}, the created files will be {@literal example.c1}, {@literal example.c2}, and so on. + * Get the base {@link File}. + * + * The base {@link File} is the one that gives the name to all generated files, including the file parts (*.cXX) and the reconstructed file. + * + * For example, if it is {@literal foo.txt}, the created files will be {@literal foo.txt.c1}, {@literal foo.txt.c2}, and so on. */ - public String getFileBaseName() { - return fileBaseName; + public File getBaseFile() { + return baseFile; } /** diff --git a/src/eu/steffo/cleaver/logic/stream/output/CleaverSimpleFileOutputStream.java b/src/eu/steffo/cleaver/logic/stream/output/CleaverSimpleFileOutputStream.java index 1794030..bbb7c2c 100644 --- a/src/eu/steffo/cleaver/logic/stream/output/CleaverSimpleFileOutputStream.java +++ b/src/eu/steffo/cleaver/logic/stream/output/CleaverSimpleFileOutputStream.java @@ -3,22 +3,40 @@ package eu.steffo.cleaver.logic.stream.output; import org.w3c.dom.Document; import org.w3c.dom.Element; +import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; +import java.io.OutputStream; +/** + * A custom {@link OutputStream} that writes the bytes received in input to a single file with a *.c0 extension. + * + * @see FileOutputStream + */ public class CleaverSimpleFileOutputStream extends FileOutputStream implements ICleaverOutputStream { - private final String fileBaseName; + private final File baseFile; - public CleaverSimpleFileOutputStream(String fileBaseName) throws FileNotFoundException { - super(String.format("%s.c0", fileBaseName)); - this.fileBaseName = fileBaseName; + public CleaverSimpleFileOutputStream(File baseFile) throws FileNotFoundException { + super(String.format("%s.c0", baseFile)); + this.baseFile = baseFile; } @Override public Element toElement(Document doc) { Element element = doc.createElement("Simple"); - element.setTextContent(fileBaseName); + element.setTextContent(baseFile.getName()); return element; } + + /** + * Get the base {@link File}. + * + * The base {@link File} is the one that gives the name to all generated files, including the chopped file (*.c0) and the reconstructed file. + * + * For example, if it is {@literal foo.txt}, the created file will be {@literal foo.txt.c0}. + */ + public File getBaseFile() { + return baseFile; + } } diff --git a/src/eu/steffo/cleaver/logic/stream/output/CleaverSplitFileOutputStream.java b/src/eu/steffo/cleaver/logic/stream/output/CleaverSplitFileOutputStream.java index 5d35357..0ae8d2e 100644 --- a/src/eu/steffo/cleaver/logic/stream/output/CleaverSplitFileOutputStream.java +++ b/src/eu/steffo/cleaver/logic/stream/output/CleaverSplitFileOutputStream.java @@ -4,6 +4,7 @@ import org.w3c.dom.Attr; import org.w3c.dom.Document; import org.w3c.dom.Element; +import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; @@ -14,7 +15,7 @@ import java.io.OutputStream; * Bytes are written to a file until its length reaches {@link #maximumByteCount}, then the program switches to the following file (.c2 if .c1 is full, .c3 if .c2 is full, and so on). */ public class CleaverSplitFileOutputStream extends OutputStream implements ICleaverOutputStream { - private final String fileBaseName; + private final File baseFile; private long currentByteCount; private long maximumByteCount; private int currentFileCount; @@ -26,11 +27,12 @@ public class CleaverSplitFileOutputStream extends OutputStream implements ICleav /** * Construct a CleaverSplitFileOutputStream. - * @param fileBaseName The name of the files without the extension. If it is {@literal example}, the created files will be {@literal example.c1}, {@literal example.c2}, and so on. + * @param baseFile The {@link File} that will be reconstructed after reversing the Split operation. + * The split files will have the same name with the addition of a .cXX extension. * @param maximumByteCount The number of bytes that should be written to a file before switching to the next one. */ - public CleaverSplitFileOutputStream(String fileBaseName, long maximumByteCount) { - this.fileBaseName = fileBaseName; + public CleaverSplitFileOutputStream(File baseFile, long maximumByteCount) { + this.baseFile = baseFile; this.maximumByteCount = maximumByteCount; this.currentByteCount = 0; this.currentFileCount = 0; @@ -47,7 +49,7 @@ public class CleaverSplitFileOutputStream extends OutputStream implements ICleav } currentFileCount += 1; - currentFileOutputStream = new FileOutputStream(String.format("%s.c%d", fileBaseName, currentFileCount)); + currentFileOutputStream = new FileOutputStream(String.format("%s.c%d", baseFile.getAbsolutePath(), currentFileCount)); currentByteCount = 0; } @@ -67,10 +69,14 @@ public class CleaverSplitFileOutputStream extends OutputStream implements ICleav } /** - * @return The name of the files without the extension. If it is {@literal example}, the created files will be {@literal example.c1}, {@literal example.c2}, and so on. + * Get the base {@link File}. + * + * The base {@link File} is the one that gives the name to all generated files, including the file parts (*.cXX) and the reconstructed file. + * + * For example, if it is {@literal foo.txt}, the created files will be {@literal foo.txt.c1}, {@literal foo.txt.c2}, and so on. */ - public String getFileBaseName() { - return fileBaseName; + public File getBaseFile() { + return baseFile; } /** @@ -97,7 +103,7 @@ public class CleaverSplitFileOutputStream extends OutputStream implements ICleav @Override public Element toElement(Document doc) { Element element = doc.createElement("Split"); - element.setTextContent(fileBaseName); + element.setTextContent(baseFile.getName()); Attr partSizeAttr = doc.createAttribute("part-size"); partSizeAttr.setValue(Long.toString(maximumByteCount));