Commit 997b1078 by Facundo Javier Gelatti

Move methods to FileBlock (to solve Feature Envy)

1 parent 381b1523
......@@ -3,14 +3,8 @@ package org.stegosuite.application.block_processing;
import org.stegosuite.model.payload.block.FileBlock;
import org.stegosuite.util.FileUtils;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import static java.nio.file.StandardOpenOption.CREATE;
class FileBlockContainer implements BlockContainer {
private final FileBlock block;
private final List<String> processedFiles;
......@@ -25,8 +19,8 @@ class FileBlockContainer implements BlockContainer {
@Override
public void processBlock() {
String extractionPath = getExtractionPath();
saveFileTo(extractionPath);
markAsProcessed(extractionPath);
block.saveFileTo(extractionPath);
processedFiles.add(extractionPath);
}
private String getExtractionPath() {
......@@ -35,28 +29,4 @@ class FileBlockContainer implements BlockContainer {
block.getFileName()
);
}
private void saveFileTo(String extractionPath) {
try {
writeContentToFileIn(extractionPath);
updateFileNameInBlock(extractionPath);
} catch (IOException e) {
e.printStackTrace();
}
}
private void writeContentToFileIn(String extractionPath) throws IOException {
byte[] fileContent = block.getFileContent();
Path path = Paths.get(extractionPath);
Files.write(path, fileContent, CREATE);
}
private void updateFileNameInBlock(String extractionPath) {
// Update the filename, so that we can compute the file size later
block.setFileName(extractionPath);
}
private void markAsProcessed(String extractionPath) {
processedFiles.add(extractionPath);
}
}
......@@ -8,9 +8,12 @@ import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import static java.nio.file.StandardOpenOption.CREATE;
/**
* This block contains the contents of a file as well as its file name
*/
......@@ -83,7 +86,18 @@ public final class FileBlock
return filename.equals(blockFilename);
}
public void setFileName(String fileName) {
this.fileName = fileName;
public void saveFileTo(String extractionPath) {
try {
writeContentToFileIn(extractionPath);
// Update the filename, so that we can compute the file size later
fileName = extractionPath;
} catch (IOException e) {
e.printStackTrace();
}
}
private void writeContentToFileIn(String extractionPath) throws IOException {
Path path = Paths.get(extractionPath);
Files.write(path, fileContent, CREATE);
}
}
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!