bit of code cleanup
parent
4c52ea7ed4
commit
d5b3ef5ac2
|
@ -51,38 +51,36 @@ public class FileHeader {
|
||||||
|
|
||||||
public void write(DataOutputStream writer) {
|
public void write(DataOutputStream writer) {
|
||||||
try {
|
try {
|
||||||
DataInputStream reader = new DataInputStream(new BufferedInputStream(Files.newInputStream(Paths.get(full_path))));
|
DataInputStream fileReader = new DataInputStream(new BufferedInputStream(Files.newInputStream(Paths.get(full_path))));
|
||||||
|
|
||||||
writer.writeByte(COMMAND.WRITE.type);
|
writer.writeByte(COMMAND.WRITE.type);
|
||||||
writer.writeUTF(relative_path);
|
writer.writeUTF(relative_path);
|
||||||
|
|
||||||
StreamingXXHash64 streamHash = XX_HASH_FACTORY.newStreamingHash64(SEED);
|
StreamingXXHash64 streamHash = XX_HASH_FACTORY.newStreamingHash64(SEED);
|
||||||
while (reader.available() > 0) {
|
while (fileReader.available() > 0) {
|
||||||
// read / write files in chunks
|
// read / write files in chunks
|
||||||
int read = Integer.min(reader.available(), READER_SIZE);
|
byte[] readBytes = new byte[Integer.min(fileReader.available(), READER_SIZE)];
|
||||||
byte[] bytes = new byte[read];
|
|
||||||
|
|
||||||
int amount = reader.read(bytes);
|
int totalRead = fileReader.read(readBytes);
|
||||||
if (amount <= 0)
|
if (totalRead <= 0)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// create a checksum for this chunk + update the overall checksum
|
// create a checksum for this chunk + update the overall checksum
|
||||||
streamHash.update(bytes, 0, amount);
|
streamHash.update(readBytes, 0, totalRead);
|
||||||
long hash = HASH_64.hash(bytes, 0, amount, SEED);
|
long hash = HASH_64.hash(readBytes, 0, totalRead, SEED);
|
||||||
|
|
||||||
// apply compression
|
// apply compression
|
||||||
int maxCompressedLength = COMPRESSOR.maxCompressedLength(bytes.length);
|
int maxCompressedLength = COMPRESSOR.maxCompressedLength(readBytes.length);
|
||||||
byte[] compressed = new byte[maxCompressedLength];
|
byte[] compressedBytes = new byte[maxCompressedLength];
|
||||||
int compressedLength = COMPRESSOR.compress(bytes, 0, bytes.length, compressed, 0, maxCompressedLength);
|
int compressedLength = COMPRESSOR.compress(readBytes, 0, readBytes.length, compressedBytes, 0, maxCompressedLength);
|
||||||
|
|
||||||
System.out.println("Writing " + compressedLength + " bytes");
|
writer.writeInt(totalRead);
|
||||||
writer.writeInt(amount);
|
|
||||||
writer.writeInt(compressedLength);
|
writer.writeInt(compressedLength);
|
||||||
writer.writeLong(hash);
|
writer.writeLong(hash);
|
||||||
writer.write(compressed, 0, compressedLength);
|
writer.write(compressedBytes, 0, compressedLength);
|
||||||
writer.flush();
|
writer.flush();
|
||||||
}
|
}
|
||||||
reader.close();
|
fileReader.close();
|
||||||
writer.writeInt(0);
|
writer.writeInt(0);
|
||||||
writer.writeLong(streamHash.getValue());
|
writer.writeLong(streamHash.getValue());
|
||||||
writer.flush();
|
writer.flush();
|
||||||
|
@ -93,30 +91,30 @@ public class FileHeader {
|
||||||
|
|
||||||
public static void receive(DataInputStream reader) {
|
public static void receive(DataInputStream reader) {
|
||||||
try {
|
try {
|
||||||
String userFile = reader.readUTF();
|
String path = createPath(reader.readUTF());
|
||||||
String[] pathParts = userFile.split("/");
|
|
||||||
String userDirectory = userFile.replace(pathParts[pathParts.length-1], "");
|
|
||||||
|
|
||||||
File ld = new File(System.getProperty("user.dir") + "/write/" + userDirectory);
|
|
||||||
if (!ld.exists())
|
|
||||||
if(!ld.mkdirs())
|
|
||||||
System.out.println("Failed to create directory");
|
|
||||||
|
|
||||||
String path = System.getProperty("user.dir") + "/write/" + userFile;
|
|
||||||
System.out.println("Writing to file: " + path);
|
System.out.println("Writing to file: " + path);
|
||||||
|
|
||||||
DataOutputStream writer = new DataOutputStream(new BufferedOutputStream(Files.newOutputStream(Paths.get(path))));
|
DataOutputStream writer = new DataOutputStream(new BufferedOutputStream(Files.newOutputStream(Paths.get(path))));
|
||||||
int uncompressed_size = 0;
|
|
||||||
StreamingXXHash64 computedStreamHash = XX_HASH_FACTORY.newStreamingHash64(SEED);
|
StreamingXXHash64 computedStreamHash = XX_HASH_FACTORY.newStreamingHash64(SEED);
|
||||||
while ((uncompressed_size = reader.readInt()) > 0) {
|
while (true) {
|
||||||
|
int uncompressed_size = reader.readInt();
|
||||||
|
|
||||||
|
if (uncompressed_size <= 0)
|
||||||
|
break;
|
||||||
|
|
||||||
int compressed_size = reader.readInt();
|
int compressed_size = reader.readInt();
|
||||||
long hash = reader.readLong();
|
long hash = reader.readLong();
|
||||||
byte[] data = new byte[compressed_size];
|
byte[] data = new byte[compressed_size];
|
||||||
int amount = reader.read(data, 0, compressed_size);
|
int amount = reader.read(data, 0, compressed_size);
|
||||||
|
|
||||||
|
assert(amount == compressed_size);
|
||||||
|
|
||||||
byte[] restored = new byte[uncompressed_size];
|
byte[] restored = new byte[uncompressed_size];
|
||||||
int len = DECOMPRESSOR.decompress(data, 0, restored, 0, uncompressed_size);
|
int len = DECOMPRESSOR.decompress(data, 0, restored, 0, uncompressed_size);
|
||||||
|
|
||||||
|
assert(len == uncompressed_size);
|
||||||
|
|
||||||
long computedHash = HASH_64.hash(restored, 0, uncompressed_size, SEED);
|
long computedHash = HASH_64.hash(restored, 0, uncompressed_size, SEED);
|
||||||
computedStreamHash.update(restored, 0, uncompressed_size);
|
computedStreamHash.update(restored, 0, uncompressed_size);
|
||||||
|
|
||||||
|
@ -130,9 +128,20 @@ public class FileHeader {
|
||||||
throw new RuntimeException("HELP 22");
|
throw new RuntimeException("HELP 22");
|
||||||
writer.flush();
|
writer.flush();
|
||||||
writer.close();
|
writer.close();
|
||||||
} catch (Exception e){
|
} catch (Exception e) {
|
||||||
ExceptionLogger.log(e);
|
ExceptionLogger.log(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static String createPath(String userFile) {
|
||||||
|
String[] pathParts = userFile.split("/");
|
||||||
|
String userDirectory = userFile.replace(pathParts[pathParts.length - 1], "");
|
||||||
|
|
||||||
|
File ld = new File(System.getProperty("user.dir") + "/write/" + userDirectory);
|
||||||
|
if (!ld.exists() && !ld.mkdirs())
|
||||||
|
System.out.println("Failed to create directory");
|
||||||
|
|
||||||
|
return System.getProperty("user.dir") + "/write/" + userFile;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue