compression

main
Brett 2023-11-14 00:32:14 -05:00
parent 1327177656
commit dd6a138aa1
10 changed files with 4686 additions and 9 deletions

View File

@ -2,10 +2,26 @@
<module type="JAVA_MODULE" version="4"> <module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true"> <component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output /> <exclude-output />
<javadoc-paths>
<root url="file://$MODULE_DIR$/libs" />
</javadoc-paths>
<content url="file://$MODULE_DIR$"> <content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content> </content>
<orderEntry type="inheritedJdk" /> <orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library" exported="">
<library>
<CLASSES>
<root url="file://$MODULE_DIR$/libs" />
</CLASSES>
<JAVADOC />
<SOURCES>
<root url="file://$MODULE_DIR$/libs" />
</SOURCES>
<jarDirectory url="file://$MODULE_DIR$/libs" recursive="false" />
<jarDirectory url="file://$MODULE_DIR$/libs" recursive="false" type="SOURCES" />
</library>
</orderEntry>
</component> </component>
</module> </module>

1543
hs_err_pid128602.log Normal file

File diff suppressed because it is too large Load Diff

1542
hs_err_pid130169.log Normal file

File diff suppressed because it is too large Load Diff

1542
hs_err_pid130528.log Normal file

File diff suppressed because it is too large Load Diff

Binary file not shown.

Binary file not shown.

BIN
libs/lz4-java-1.8.0.jar Normal file

Binary file not shown.

View File

@ -1,5 +1,9 @@
package client; package client;
import net.jpountz.lz4.LZ4BlockInputStream;
import net.jpountz.lz4.LZ4BlockOutputStream;
import net.jpountz.lz4.LZ4FrameInputStream;
import net.jpountz.lz4.LZ4FrameOutputStream;
import server.Server; import server.Server;
import shared.ExceptionLogger; import shared.ExceptionLogger;
import shared.FileHeader; import shared.FileHeader;

View File

@ -1,5 +1,10 @@
package server; package server;
import net.jpountz.lz4.LZ4BlockInputStream;
import net.jpountz.lz4.LZ4BlockOutputStream;
import net.jpountz.lz4.LZ4FrameInputStream;
import net.jpountz.lz4.LZ4FrameOutputStream;
import shared.ExceptionLogger;
import shared.FileHeader; import shared.FileHeader;
import java.io.*; import java.io.*;
@ -18,7 +23,8 @@ public class Connection extends Thread {
try { try {
out = new DataOutputStream(new BufferedOutputStream(clientSocket.getOutputStream())); out = new DataOutputStream(new BufferedOutputStream(clientSocket.getOutputStream()));
in = new DataInputStream(new BufferedInputStream(clientSocket.getInputStream())); in = new DataInputStream(new BufferedInputStream(clientSocket.getInputStream()));
} catch (Exception ignored) { } catch (Exception e) {
ExceptionLogger.log(e);
} }
} }

View File

@ -1,6 +1,10 @@
package shared; package shared;
import client.Client; import client.Client;
import net.jpountz.lz4.LZ4Compressor;
import net.jpountz.lz4.LZ4Factory;
import net.jpountz.lz4.LZ4FastDecompressor;
import net.jpountz.lz4.LZ4SafeDecompressor;
import java.io.*; import java.io.*;
import java.nio.file.Files; import java.nio.file.Files;
@ -8,7 +12,11 @@ import java.nio.file.Paths;
public class FileHeader { public class FileHeader {
private static final int READER_SIZE = 8192; public static final int READER_SIZE = 8192;
private static final LZ4Factory factory = LZ4Factory.fastestInstance();
private static final LZ4Compressor compressor = factory.highCompressor();
private static final LZ4FastDecompressor decompressor = factory.fastDecompressor();
public enum COMMAND { public enum COMMAND {
WRITE((byte) 1); WRITE((byte) 1);
@ -42,15 +50,24 @@ public class FileHeader {
writer.writeUTF(relative_path); writer.writeUTF(relative_path);
while (reader.available() > 0) { while (reader.available() > 0) {
// read / write files in chunks
int read = Integer.min(reader.available(), READER_SIZE); int read = Integer.min(reader.available(), READER_SIZE);
byte[] bytes = new byte[read]; byte[] bytes = new byte[read];
int amount = reader.read(bytes); int amount = reader.read(bytes);
if (amount <= 0) if (amount <= 0)
break; break;
System.out.println("Writing " + amount + " bytes");
// apply compression
int maxCompressedLength = compressor.maxCompressedLength(bytes.length);
byte[] compressed = new byte[maxCompressedLength];
int compressedLength = compressor.compress(bytes, 0, bytes.length, compressed, 0, maxCompressedLength);
System.out.println("Writing " + compressedLength + " bytes");
writer.writeInt(amount); writer.writeInt(amount);
writer.write(bytes, 0, amount); writer.writeInt(compressedLength);
writer.write(compressed, 0, compressedLength);
writer.flush();
} }
reader.close(); reader.close();
writer.writeInt(0); writer.writeInt(0);
@ -75,13 +92,20 @@ public class FileHeader {
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 size = 0; int uncompressed_size = 0;
while ((size = reader.readInt()) > 0) { while ((uncompressed_size = reader.readInt()) > 0) {
byte[] data = new byte[size]; int compressed_size = reader.readInt();
int amount = reader.read(data, 0, size); byte[] data = new byte[compressed_size];
writer.write(data, 0, amount); int amount = reader.read(data, 0, compressed_size);
byte[] restored = new byte[uncompressed_size];
int len = decompressor.decompress(data, 0, restored, 0, uncompressed_size);
writer.write(restored, 0, uncompressed_size);
} }
writer.flush(); writer.flush();
writer.close();
} catch (Exception e){ } catch (Exception e){
ExceptionLogger.log(e); ExceptionLogger.log(e);
} }