-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IOBundle.java
110 lines (99 loc) · 3.11 KB
/
IOBundle.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package me.retrodaredevil.io;
import java.io.InputStream;
import java.io.OutputStream;
import static java.util.Objects.requireNonNull;
/**
* Represents a {@link InputStream} and an {@link OutputStream}
* <p>
* It is expected that throughout the lifetime of this object that calls to both {@link #getInputStream()} and {@link #getOutputStream()}
* will return the same instance each time it is called.
*/
public interface IOBundle extends AutoCloseable {
InputStream getInputStream();
OutputStream getOutputStream();
/**
* This should be overridden by subclasses to close the input and output streams
* @throws Exception If this cannot be closed
*/
@Override
default void close() throws Exception {
}
/**
* NOTE: The {@link #close()} method will do nothing on the instance returned by this method
* @param inputStream The {@link InputStream} to return in {@link #getInputStream()}
* @param outputStream The {@link OutputStream} to return in {@link #getOutputStream()}
* @return An immutable {@link IOBundle} representing {@code inputStream} and {@code outputStream}
*/
static IOBundle of(final InputStream inputStream, final OutputStream outputStream){
requireNonNull(inputStream);
requireNonNull(outputStream);
return new IOBundle() {
@Override
public InputStream getInputStream() {
return inputStream;
}
@Override
public OutputStream getOutputStream() {
return outputStream;
}
};
}
/**
* NOTE: Calling close() will close {@code inputStream} then {@code outputStream}
* @param inputStream The {@link InputStream} to return in {@link #getInputStream()}
* @param outputStream The {@link OutputStream} to return in {@link #getOutputStream()}
* @return An immutable {@link IOBundle} representing {@code inputStream} and {@code outputStream}
*/
static IOBundle ofWithClose(final InputStream inputStream, final OutputStream outputStream){
requireNonNull(inputStream);
requireNonNull(outputStream);
return new IOBundle() {
@Override
public InputStream getInputStream() {
return inputStream;
}
@Override
public OutputStream getOutputStream() {
return outputStream;
}
@Override
public void close() throws Exception {
inputStream.close();
outputStream.close();
}
};
}
final class Defaults {
private Defaults(){ throw new UnsupportedOperationException(); }
/**
* Represents an {@link IOBundle} that returns {@link System#in} and {@link System#out}
* <p>
* NOTE: Calling {@link #close()} does nothing.
*/
public static IOBundle STANDARD_IN_OUT = new IOBundle() {
@Override
public InputStream getInputStream() {
return System.in;
}
@Override
public OutputStream getOutputStream() {
return System.out;
}
};
/**
* Represents an {@link IOBundle} that returns {@link System#in} and {@link System#err}
* <p>
* NOTE: Calling {@link #close()} does nothing.
*/
public static IOBundle STANDARD_IN_ERR = new IOBundle() {
@Override
public InputStream getInputStream() {
return System.in;
}
@Override
public OutputStream getOutputStream() {
return System.err;
}
};
}
}