-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathBytePipe.java
More file actions
74 lines (62 loc) · 1.68 KB
/
BytePipe.java
File metadata and controls
74 lines (62 loc) · 1.68 KB
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
package modules;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
public class BytePipe implements Pipe {
private PipedInputStream input;
private PipedOutputStream output;
public BytePipe() throws IOException {
this.reset();
}
/**
* Get the input stream.
* @return input stream
*/
public PipedInputStream getInput() {
return input;
}
/**
* Get the output stream.
* @return output stream
*/
public PipedOutputStream getOutput() {
return output;
}
/**
* Writes to the output pipe.
* @see PipedOutputStream#write(byte[], int, int) PipedOutputStream.write
* @param data byte-array with data to write
* @param offset write offset
* @param length length of data to write
* @throws IOException thrown on I/O error
*/
public void write(byte[] data, int offset, int length) throws IOException {
this.output.write(data, offset, length);
}
@Override
public void writeClose() throws IOException {
this.output.close();
}
/**
* Reads from the input pipe.
* @see PipedInputStream#read(byte[], int, int) PipedInputStream.read
* @param buffer buffer to store read input in
* @param offset read offset
* @param length amount of bytes to read
* @return amount of bytes read
* @throws IOException thrown on I/O error
*/
public int read(byte[] buffer, int offset, int length) throws IOException {
return this.input.read(buffer, offset, length);
}
@Override
public void readClose() throws IOException {
this.input.close();
}
@Override
public void reset() throws IOException {
this.input = new PipedInputStream();
this.output = new PipedOutputStream();
this.input.connect(this.output);
}
}