Skip to content

Commit 654a78a

Browse files
committed
Clean up streams and write README.
1 parent cd26393 commit 654a78a

14 files changed

+281
-14
lines changed

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
Jupiter
2+
=====
3+
4+
### Opus #15
5+
6+
A library for working with common Input/Output routes and providing better utilities for Java's I/O streams.
7+
8+
## Maven Information
9+
```xml
10+
<repository>
11+
<id>kenzie</id>
12+
<name>Kenzie's Repository</name>
13+
<url>https://repo.kenzie.mx/releases</url>
14+
</repository>
15+
```
16+
17+
```xml
18+
<dependency>
19+
<groupId>mx.kenzie</groupId>
20+
<artifactId>jupiter</artifactId>
21+
<version>1.0.0</version>
22+
</dependency>
23+
```
24+
25+
## Description
26+
27+
Jupiter provides a set of utilities for working with streams and channels, to make integrating them easier and to reduce the amount of necessary boilerplate for common tasks.
28+
29+
These utilities include:
30+
1. Synchronization locks for streams (where multithreaded access would normally be dangerous)
31+
2. Stream controllers that can perform simple conversion tasks
32+
3. Forking, locking and wrapping streams with automatic resource disposal
33+
34+
There are also more advanced utilities for handling complex socket connections.
35+
36+
The `SocketPair` provides a smart two-way socket channel between two known addresses with automatic host/guest deference.
37+
38+
The `SocketHub` provides a fluid-size socket network connection for organising channels between an unknown set of guests.

src/main/java/mx/kenzie/jupiter/socket/SocketHub.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,9 @@
1010
import java.util.Collections;
1111
import java.util.List;
1212

13+
@SuppressWarnings("unused")
1314
public class SocketHub extends Thread implements AutoCloseable {
1415

15-
public static final byte
16-
JOIN = 1,
17-
FAIL = 2,
18-
LEAVE = 3;
1916
private static final ByteBuffer BUFFER = ByteBuffer.allocate(4);
2017
protected final ServerSocket central;
2118
protected final List<Socket> list = Collections.synchronizedList(new ArrayList<>());

src/main/java/mx/kenzie/jupiter/socket/SocketPair.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.net.ServerSocket;
77
import java.net.Socket;
88

9+
@SuppressWarnings("unused")
910
public class SocketPair extends Thread implements AutoCloseable {
1011

1112
protected final Object lock = new Object();
@@ -14,7 +15,6 @@ public class SocketPair extends Thread implements AutoCloseable {
1415
protected SocketOpeningProcess process;
1516

1617
protected SocketPair() {
17-
1818
}
1919

2020
public static Builder host(int port) {

src/main/java/mx/kenzie/jupiter/stream/InputStreamController.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.io.OutputStream;
66
import java.nio.ByteBuffer;
77

8+
@SuppressWarnings("unused")
89
public class InputStreamController extends InputStream implements StreamController {
910

1011
protected final InputStream stream;

src/main/java/mx/kenzie/jupiter/stream/OutputStreamController.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.io.OutputStream;
66
import java.nio.ByteBuffer;
77

8+
@SuppressWarnings("unused")
89
public class OutputStreamController extends OutputStream implements StreamController {
910

1011
protected final OutputStream stream;

src/main/java/mx/kenzie/jupiter/stream/SplitStream.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.io.OutputStream;
55
import java.util.Arrays;
66

7+
@SuppressWarnings("unused")
78
public class SplitStream extends OutputStream implements Stream {
89

910
protected static final byte GROW_SIZE = 8;

src/main/java/mx/kenzie/jupiter/stream/Stream.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,12 @@ static SplitStream split(OutputStream... streams) {
1717
return new SplitStream(streams);
1818
}
1919

20+
static InputStream synchronize(InputStream stream) {
21+
return new SynchronizedInputStream(stream);
22+
}
23+
24+
static OutputStream synchronize(OutputStream stream) {
25+
return new SynchronizedOutputStream(stream);
26+
}
27+
2028
}

src/main/java/mx/kenzie/jupiter/stream/StreamController.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@
22

33
public interface StreamController extends Stream {
44

5-
65
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package mx.kenzie.jupiter.stream;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
8+
class SynchronizedInputStream extends InputStream {
9+
10+
protected final InputStream stream;
11+
12+
SynchronizedInputStream(InputStream stream) {
13+
this.stream = stream;
14+
}
15+
16+
@Override
17+
public int read() throws IOException {
18+
synchronized (stream) {
19+
return stream.read();
20+
}
21+
}
22+
23+
@Override
24+
public int read(byte @NotNull [] bytes) throws IOException {
25+
synchronized (stream) {
26+
return stream.read(bytes);
27+
}
28+
}
29+
30+
@Override
31+
public int read(byte @NotNull [] b, int offset, int length) throws IOException {
32+
synchronized (stream) {
33+
return stream.read(b, offset, length);
34+
}
35+
}
36+
37+
@Override
38+
public byte[] readAllBytes() throws IOException {
39+
synchronized (stream) {
40+
return stream.readAllBytes();
41+
}
42+
}
43+
44+
@Override
45+
public byte[] readNBytes(int len) throws IOException {
46+
synchronized (stream) {
47+
return stream.readNBytes(len);
48+
}
49+
}
50+
51+
@Override
52+
public int readNBytes(byte[] b, int off, int len) throws IOException {
53+
synchronized (stream) {
54+
return super.readNBytes(b, off, len);
55+
}
56+
}
57+
58+
@Override
59+
public long skip(long n) throws IOException {
60+
synchronized (stream) {
61+
return super.skip(n);
62+
}
63+
}
64+
65+
@Override
66+
public void skipNBytes(long n) throws IOException {
67+
synchronized (stream) {
68+
super.skipNBytes(n);
69+
}
70+
}
71+
72+
@Override
73+
public int available() throws IOException {
74+
synchronized (stream) {
75+
return super.available();
76+
}
77+
}
78+
79+
@Override
80+
public void close() throws IOException {
81+
this.stream.close();
82+
}
83+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package mx.kenzie.jupiter.stream;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
import java.io.IOException;
6+
import java.io.OutputStream;
7+
8+
class SynchronizedOutputStream extends OutputStream {
9+
10+
protected final OutputStream stream;
11+
12+
SynchronizedOutputStream(OutputStream stream) {
13+
this.stream = stream;
14+
}
15+
16+
@Override
17+
public void write(byte @NotNull [] bytes) throws IOException {
18+
synchronized (stream) {
19+
super.write(bytes);
20+
}
21+
}
22+
23+
@Override
24+
public void write(byte @NotNull [] bytes, int offset, int length) throws IOException {
25+
synchronized (stream) {
26+
super.write(bytes, offset, length);
27+
}
28+
}
29+
30+
@Override
31+
public void flush() throws IOException {
32+
synchronized (stream) {
33+
stream.flush();
34+
}
35+
}
36+
37+
@Override
38+
public void write(int b) throws IOException {
39+
synchronized (stream) {
40+
stream.write(b);
41+
}
42+
}
43+
44+
@Override
45+
public void close() throws IOException {
46+
this.stream.close();
47+
}
48+
}

0 commit comments

Comments
 (0)