mirror of
https://github.com/Steffo99/unimore-oop-2020-cleaver.git
synced 2024-11-22 08:04:19 +00:00
Add ChopJob.run() code (no crypt yet)
This commit is contained in:
parent
fbe9dbbd0f
commit
d79f83f376
11 changed files with 229 additions and 11 deletions
|
@ -1,5 +0,0 @@
|
|||
<Cleaver>
|
||||
<Compress/>
|
||||
<Crypt/>
|
||||
<Split mode="by-parts">10</Split>
|
||||
</Cleaver>
|
21
src/eu/steffo/cleaver/Test.java
Normal file
21
src/eu/steffo/cleaver/Test.java
Normal file
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -17,6 +17,7 @@ public class FileSelectRow extends Row {
|
|||
|
||||
fileChooser = new JFileChooser();
|
||||
fileChooser.setMultiSelectionEnabled(true);
|
||||
fileChooser.setCurrentDirectory(new File("."));
|
||||
|
||||
this.add(Box.createHorizontalStrut(8));
|
||||
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
}
|
||||
|
|
14
src/eu/steffo/cleaver/logic/progress/ErrorProgress.java
Normal file
14
src/eu/steffo/cleaver/logic/progress/ErrorProgress.java
Normal file
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
45
src/eu/steffo/cleaver/logic/split/SplitFileOutputStream.java
Normal file
45
src/eu/steffo/cleaver/logic/split/SplitFileOutputStream.java
Normal file
|
@ -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();
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue