Skip to content

Commit e517d09

Browse files
committed
Updated to v1.3.1
Added DeflaterCompression class to compress packets using the DeflaterOutputStream/InflaterOutputStream. Moved the initialization of ByteArrayOutputStream & DataOutputStream to the constructor of PacketBuilder. Added a default buffer size for Client & Server of value 8192 and updated the example & readme file accordingly.
1 parent 426d4a8 commit e517d09

9 files changed

Lines changed: 68 additions & 7 deletions

File tree

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ private static final String secretKey = "1F16hIQ3SjQ$k1!9";
2424

2525
public static void main(String[] args) throws Exception {
2626
// Create and start the server
27-
Server server = new Server(1024);
27+
Server server = new Server();
2828
server.addListener(createServerListener());
2929
server.start(3300, 3301);
3030

3131
// Create and connect the client to the server
32-
Client client = new Client(1024);
32+
Client client = new Client();
3333
client.addListener(createClientListener());
3434
client.connect("127.0.0.1", 3300, 3301);
3535

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>xyz.synse</groupId>
88
<artifactId>PacketNet</artifactId>
9-
<version>1.3</version>
9+
<version>1.3.1</version>
1010

1111
<properties>
1212
<maven.compiler.source>20</maven.compiler.source>

src/main/java/xyz/synse/packetnet/Test.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ public class Test {
2222

2323
public static void main(String[] args) throws Exception {
2424
// Create and start the server
25-
Server server = new Server(1024);
25+
Server server = new Server();
2626
server.addListener(createServerListener());
2727
server.start(3300, 3301);
2828

2929
// Create and connect the client to the server
30-
Client client = new Client(1024);
30+
Client client = new Client();
3131
client.addListener(createClientListener());
3232
client.connect("127.0.0.1", 3300, 3301);
3333

src/main/java/xyz/synse/packetnet/client/Client.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,22 @@ public class Client {
2222
private Thread tcpThread;
2323
private Thread udpThread;
2424

25+
/**
26+
* Creates a new instance of the Client class.
27+
*
28+
* @param bufferSize The size of the buffer for receiving data.
29+
*/
2530
public Client(int bufferSize) {
2631
this.bufferSize = bufferSize;
2732
}
2833

34+
/**
35+
* Creates a new instance of the Client class with buffer size of 8192 bytes.
36+
*/
37+
public Client() {
38+
this(8192);
39+
}
40+
2941
/**
3042
* Connects the client to the server using the specified TCP and UDP ports.
3143
*

src/main/java/xyz/synse/packetnet/common/compression/PacketCompressor.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package xyz.synse.packetnet.common.compression;
22

3+
import xyz.synse.packetnet.common.compression.compressors.DeflaterCompressor;
34
import xyz.synse.packetnet.common.compression.compressors.GZipCompressor;
45
import xyz.synse.packetnet.common.compression.compressors.LZ4Compressor;
56
import xyz.synse.packetnet.common.packets.Packet;
67

78
import java.io.IOException;
89

910
public class PacketCompressor {
11+
public static final ICompressor DEFLATER_COMPRESSOR = new DeflaterCompressor();
1012
public static final ICompressor GZIP_COMPRESSOR = new GZipCompressor();
1113
public static final ICompressor LZ4_COMPRESSOR = new LZ4Compressor();
1214

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package xyz.synse.packetnet.common.compression.compressors;
2+
3+
import xyz.synse.packetnet.common.compression.ICompressor;
4+
5+
import java.io.ByteArrayOutputStream;
6+
import java.io.IOException;
7+
import java.util.zip.DeflaterOutputStream;
8+
import java.util.zip.InflaterOutputStream;
9+
10+
public class DeflaterCompressor implements ICompressor {
11+
@Override
12+
public byte[] compress(byte[] in) throws IOException {
13+
ByteArrayOutputStream out = new ByteArrayOutputStream();
14+
DeflaterOutputStream defl = new DeflaterOutputStream(out);
15+
defl.write(in);
16+
defl.flush();
17+
defl.close();
18+
19+
return out.toByteArray();
20+
}
21+
22+
@Override
23+
public byte[] decompress(byte[] in) throws IOException {
24+
ByteArrayOutputStream out = new ByteArrayOutputStream();
25+
InflaterOutputStream infl = new InflaterOutputStream(out);
26+
infl.write(in);
27+
infl.flush();
28+
infl.close();
29+
30+
return out.toByteArray();
31+
}
32+
}

src/main/java/xyz/synse/packetnet/common/packets/PacketBuilder.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77

88
public class PacketBuilder implements AutoCloseable {
99
private final short id;
10-
private final ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
11-
private final DataOutputStream out = new DataOutputStream(byteOut);
10+
private final ByteArrayOutputStream byteOut;
11+
private final DataOutputStream out;
1212

1313
public PacketBuilder(short id) {
1414
this.id = id;
15+
this.byteOut = new ByteArrayOutputStream();
16+
this.out = new DataOutputStream(byteOut);
1517
}
1618

1719
public PacketBuilder withBytes(byte[] data) {

src/main/java/xyz/synse/packetnet/server/Server.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ public Server(int bufferSize) {
3232
running = false;
3333
}
3434

35+
/**
36+
* Creates a new instance of the Server class with buffer size of 8192 bytes.
37+
*/
38+
public Server() {
39+
this(8192);
40+
}
41+
3542
/**
3643
* Starts the server on the specified TCP and UDP ports.
3744
*

0 commit comments

Comments
 (0)