mirror of
https://github.com/corda/corda.git
synced 2025-01-23 12:58:35 +00:00
add Channels.newChannel methods
This commit is contained in:
parent
dd52908f89
commit
4f0dccb53c
@ -24,6 +24,14 @@ public class Channels {
|
|||||||
return new MyOutputStream(channel);
|
return new MyOutputStream(channel);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static ReadableByteChannel newChannel(InputStream stream) {
|
||||||
|
return new InputStreamChannel(stream);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static WritableByteChannel newChannel(OutputStream stream) {
|
||||||
|
return new OutputStreamChannel(stream);
|
||||||
|
}
|
||||||
|
|
||||||
private static class MyInputStream extends InputStream {
|
private static class MyInputStream extends InputStream {
|
||||||
private final ReadableByteChannel channel;
|
private final ReadableByteChannel channel;
|
||||||
|
|
||||||
@ -72,4 +80,63 @@ public class Channels {
|
|||||||
channel.close();
|
channel.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static class InputStreamChannel implements ReadableByteChannel {
|
||||||
|
private InputStream stream;
|
||||||
|
|
||||||
|
public InputStreamChannel(InputStream stream) {
|
||||||
|
this.stream = stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void close() throws IOException {
|
||||||
|
if (stream != null) {
|
||||||
|
stream.close();
|
||||||
|
stream = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isOpen() {
|
||||||
|
return stream != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int read(ByteBuffer b) throws IOException {
|
||||||
|
int c = stream.read
|
||||||
|
(b.array(), b.arrayOffset() + b.position(), b.remaining());
|
||||||
|
|
||||||
|
if (c > 0) {
|
||||||
|
b.position(b.position() + c);
|
||||||
|
}
|
||||||
|
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class OutputStreamChannel implements WritableByteChannel {
|
||||||
|
private OutputStream stream;
|
||||||
|
|
||||||
|
public OutputStreamChannel(OutputStream stream) {
|
||||||
|
this.stream = stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void close() throws IOException {
|
||||||
|
if (stream != null) {
|
||||||
|
stream.close();
|
||||||
|
stream = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isOpen() {
|
||||||
|
return stream != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int write(ByteBuffer b) throws IOException {
|
||||||
|
stream.write(b.array(), b.arrayOffset() + b.position(), b.remaining());
|
||||||
|
|
||||||
|
int c = b.remaining();
|
||||||
|
|
||||||
|
b.position(b.limit());
|
||||||
|
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user