mirror of
https://github.com/Steffo99/unimore-oop-2020-cleaver.git
synced 2024-11-22 16:14:18 +00:00
Complete chop process
This commit is contained in:
parent
1c46967d58
commit
7f274721ba
6 changed files with 60 additions and 50 deletions
|
@ -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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -18,6 +18,8 @@ import javax.xml.transform.TransformerFactory;
|
||||||
import javax.xml.transform.dom.DOMSource;
|
import javax.xml.transform.dom.DOMSource;
|
||||||
import javax.xml.transform.stream.StreamResult;
|
import javax.xml.transform.stream.StreamResult;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A {@link Job} that converts regular files into <i>chopped</i> (*.chp + *.cXX) files.
|
* A {@link Job} that converts regular files into <i>chopped</i> (*.chp + *.cXX) files.
|
||||||
|
@ -40,7 +42,7 @@ public class ChopJob extends Job {
|
||||||
*/
|
*/
|
||||||
public ChopJob(File file, ISplitConfig splitConfig, ICryptConfig cryptConfig, ICompressConfig compressConfig, Runnable onProgressChange) {
|
public ChopJob(File file, ISplitConfig splitConfig, ICryptConfig cryptConfig, ICompressConfig compressConfig, Runnable onProgressChange) {
|
||||||
super(onProgressChange);
|
super(onProgressChange);
|
||||||
this.file = file;
|
this.file = file.getAbsoluteFile();
|
||||||
this.splitConfig = splitConfig;
|
this.splitConfig = splitConfig;
|
||||||
this.cryptConfig = cryptConfig;
|
this.cryptConfig = cryptConfig;
|
||||||
this.compressConfig = compressConfig;
|
this.compressConfig = compressConfig;
|
||||||
|
@ -91,13 +93,13 @@ public class ChopJob extends Job {
|
||||||
OutputStream outputStream;
|
OutputStream outputStream;
|
||||||
|
|
||||||
if(splitConfig instanceof SizeConfig) {
|
if(splitConfig instanceof SizeConfig) {
|
||||||
outputStream = new CleaverSplitFileOutputStream(file.getAbsolutePath(), ((SizeConfig)splitConfig).getPartSize());
|
outputStream = new CleaverSplitFileOutputStream(file, ((SizeConfig)splitConfig).getPartSize());
|
||||||
}
|
}
|
||||||
else if(splitConfig instanceof PartsConfig) {
|
else if(splitConfig instanceof PartsConfig) {
|
||||||
outputStream = new CleaverForkFileOutputStream(file.getAbsolutePath(), ((PartsConfig)splitConfig).getPartCount());
|
outputStream = new CleaverForkFileOutputStream(file, ((PartsConfig)splitConfig).getPartCount());
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
outputStream = new CleaverSimpleFileOutputStream(file.getAbsolutePath());
|
outputStream = new CleaverSimpleFileOutputStream(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(compressConfig instanceof DeflateConfig) {
|
if(compressConfig instanceof DeflateConfig) {
|
||||||
|
|
|
@ -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}.
|
* 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)}.
|
* @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
|
* @see SecureRandom
|
||||||
*/
|
*/
|
||||||
protected byte[] generateSecure(int size) {
|
protected byte[] generateSecure(int size) {
|
||||||
byte[] salt = new byte[8];
|
byte[] salt = new byte[size];
|
||||||
SecureRandom secureRandom = new SecureRandom();
|
SecureRandom secureRandom = new SecureRandom();
|
||||||
secureRandom.nextBytes(salt);
|
secureRandom.nextBytes(salt);
|
||||||
return salt;
|
return salt;
|
||||||
|
@ -111,7 +111,7 @@ public class CleaverCryptOutputStream extends FilterOutputStream implements ICle
|
||||||
SecretKey aes = new SecretKeySpec(generatePasswordKey(key).getEncoded(), ENCRYPTION_ALGORITHM);
|
SecretKey aes = new SecretKeySpec(generatePasswordKey(key).getEncoded(), ENCRYPTION_ALGORITHM);
|
||||||
|
|
||||||
//Generate the initialization vector
|
//Generate the initialization vector
|
||||||
IvParameterSpec iv = generateIV(1);
|
IvParameterSpec iv = generateIV(16);
|
||||||
|
|
||||||
//Init the cipher instance
|
//Init the cipher instance
|
||||||
cipher.init(Cipher.ENCRYPT_MODE, aes, iv);
|
cipher.init(Cipher.ENCRYPT_MODE, aes, iv);
|
||||||
|
|
|
@ -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.
|
* 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 {
|
public class CleaverForkFileOutputStream extends OutputStream implements ICleaverOutputStream {
|
||||||
private final String fileBaseName;
|
private final File baseFile;
|
||||||
private FileOutputStream[] fileOutputStreams;
|
private FileOutputStream[] fileOutputStreams;
|
||||||
private int writeTo;
|
private int writeTo;
|
||||||
private long partSize;
|
private long partSize;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a CleaverForkFileOutputStream.
|
* 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.
|
* @param parts The number of parts to be created.
|
||||||
*/
|
*/
|
||||||
public CleaverForkFileOutputStream(String fileBaseName, int parts) throws FileNotFoundException {
|
public CleaverForkFileOutputStream(File baseFile, int parts) throws FileNotFoundException {
|
||||||
this.fileBaseName = fileBaseName;
|
this.baseFile = baseFile;
|
||||||
this.fileOutputStreams = new FileOutputStream[parts];
|
this.fileOutputStreams = new FileOutputStream[parts];
|
||||||
for(int i = 0; i < parts; i++) {
|
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.fileOutputStreams[i] = new FileOutputStream(file);
|
||||||
}
|
}
|
||||||
this.writeTo = 0;
|
this.writeTo = 0;
|
||||||
|
@ -36,7 +36,7 @@ public class CleaverForkFileOutputStream extends OutputStream implements ICleave
|
||||||
@Override
|
@Override
|
||||||
public Element toElement(Document doc) {
|
public Element toElement(Document doc) {
|
||||||
Element element = doc.createElement("Fork");
|
Element element = doc.createElement("Fork");
|
||||||
element.setTextContent(fileBaseName);
|
element.setTextContent(baseFile.getName());
|
||||||
|
|
||||||
Attr partSizeAttr = doc.createAttribute("part-size");
|
Attr partSizeAttr = doc.createAttribute("part-size");
|
||||||
partSizeAttr.setValue(Long.toString(partSize));
|
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() {
|
public File getBaseFile() {
|
||||||
return fileBaseName;
|
return baseFile;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -3,22 +3,40 @@ package eu.steffo.cleaver.logic.stream.output;
|
||||||
import org.w3c.dom.Document;
|
import org.w3c.dom.Document;
|
||||||
import org.w3c.dom.Element;
|
import org.w3c.dom.Element;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.FileOutputStream;
|
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 {
|
public class CleaverSimpleFileOutputStream extends FileOutputStream implements ICleaverOutputStream {
|
||||||
private final String fileBaseName;
|
private final File baseFile;
|
||||||
|
|
||||||
public CleaverSimpleFileOutputStream(String fileBaseName) throws FileNotFoundException {
|
public CleaverSimpleFileOutputStream(File baseFile) throws FileNotFoundException {
|
||||||
super(String.format("%s.c0", fileBaseName));
|
super(String.format("%s.c0", baseFile));
|
||||||
this.fileBaseName = fileBaseName;
|
this.baseFile = baseFile;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Element toElement(Document doc) {
|
public Element toElement(Document doc) {
|
||||||
Element element = doc.createElement("Simple");
|
Element element = doc.createElement("Simple");
|
||||||
element.setTextContent(fileBaseName);
|
element.setTextContent(baseFile.getName());
|
||||||
|
|
||||||
return element;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import org.w3c.dom.Attr;
|
||||||
import org.w3c.dom.Document;
|
import org.w3c.dom.Document;
|
||||||
import org.w3c.dom.Element;
|
import org.w3c.dom.Element;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
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).
|
* 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 {
|
public class CleaverSplitFileOutputStream extends OutputStream implements ICleaverOutputStream {
|
||||||
private final String fileBaseName;
|
private final File baseFile;
|
||||||
private long currentByteCount;
|
private long currentByteCount;
|
||||||
private long maximumByteCount;
|
private long maximumByteCount;
|
||||||
private int currentFileCount;
|
private int currentFileCount;
|
||||||
|
@ -26,11 +27,12 @@ public class CleaverSplitFileOutputStream extends OutputStream implements ICleav
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a CleaverSplitFileOutputStream.
|
* 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.
|
* @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) {
|
public CleaverSplitFileOutputStream(File baseFile, long maximumByteCount) {
|
||||||
this.fileBaseName = fileBaseName;
|
this.baseFile = baseFile;
|
||||||
this.maximumByteCount = maximumByteCount;
|
this.maximumByteCount = maximumByteCount;
|
||||||
this.currentByteCount = 0;
|
this.currentByteCount = 0;
|
||||||
this.currentFileCount = 0;
|
this.currentFileCount = 0;
|
||||||
|
@ -47,7 +49,7 @@ public class CleaverSplitFileOutputStream extends OutputStream implements ICleav
|
||||||
}
|
}
|
||||||
|
|
||||||
currentFileCount += 1;
|
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;
|
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() {
|
public File getBaseFile() {
|
||||||
return fileBaseName;
|
return baseFile;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -97,7 +103,7 @@ public class CleaverSplitFileOutputStream extends OutputStream implements ICleav
|
||||||
@Override
|
@Override
|
||||||
public Element toElement(Document doc) {
|
public Element toElement(Document doc) {
|
||||||
Element element = doc.createElement("Split");
|
Element element = doc.createElement("Split");
|
||||||
element.setTextContent(fileBaseName);
|
element.setTextContent(baseFile.getName());
|
||||||
|
|
||||||
Attr partSizeAttr = doc.createAttribute("part-size");
|
Attr partSizeAttr = doc.createAttribute("part-size");
|
||||||
partSizeAttr.setValue(Long.toString(maximumByteCount));
|
partSizeAttr.setValue(Long.toString(maximumByteCount));
|
||||||
|
|
Loading…
Reference in a new issue