MCPcopy Create free account
hub / github.com/ReadyTalk/avian / SocketOutputStream

Class SocketOutputStream

test/extra/SendFile.java:13–45  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

11
12public class SendFile {
13 private static class SocketOutputStream extends OutputStream {
14 private final SocketChannel channel;
15 private final Selector selector;
16 public SocketOutputStream(String host, int port) throws Exception {
17 channel = SocketChannel.open();
18 channel.connect(new InetSocketAddress(host, port));
19 channel.configureBlocking(false);
20 selector = Selector.open();
21 channel.register(selector, SelectionKey.OP_WRITE, null);
22 }
23
24 public void close() throws IOException {
25 channel.close();
26 }
27
28 public void write(int c) {
29 throw new RuntimeException("Do not use!");
30 }
31 public void write(byte[] buffer, int offset, int length)
32 throws IOException {
33 ByteBuffer buf = ByteBuffer.wrap(buffer);
34 buf.position(offset);
35 buf.limit(offset+length);
36 while (buf.hasRemaining()) {
37 selector.select(10000);
38 for (SelectionKey key : selector.selectedKeys()) {
39 if (key.isWritable() && (key.channel() == channel)) {
40 channel.write(buf);
41 }
42 }
43 }
44 }
45 }
46
47 public static void sendFile(String file, String host, int port)
48 throws Exception {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected