|
9 | 9 | import java.net.SocketAddress;
|
10 | 10 | import java.net.SocketException;
|
11 | 11 | import java.net.UnixDomainSocketAddress;
|
12 |
| -import java.nio.ByteBuffer; |
13 | 12 | import java.nio.channels.Channels;
|
14 |
| -import java.nio.channels.SelectionKey; |
15 |
| -import java.nio.channels.Selector; |
16 | 13 | import java.nio.channels.SocketChannel;
|
17 | 14 | import java.nio.file.Path;
|
18 | 15 |
|
@@ -121,46 +118,47 @@ public InputStream getInputStream() throws IOException {
|
121 | 118 | if (isInputShutdown()) {
|
122 | 119 | throw new SocketException("Socket input is shutdown");
|
123 | 120 | }
|
124 |
| - |
125 |
| - Selector selector = Selector.open(); |
126 |
| - unixSocketChannel.configureBlocking(false); |
127 |
| - unixSocketChannel.register(selector, SelectionKey.OP_READ); |
128 |
| - ByteBuffer buffer = ByteBuffer.allocate(256); |
129 |
| - |
130 |
| - try { |
131 |
| - if (selector.select(timeout) == 0) { |
132 |
| - System.out.println("Timeout (" + timeout + "ms) while waiting for data."); |
133 |
| - } |
134 |
| - for (SelectionKey key : selector.selectedKeys()) { |
135 |
| - if (key.isReadable()) { |
136 |
| - int r = unixSocketChannel.read(buffer); |
137 |
| - if (r == -1) { |
138 |
| - unixSocketChannel.close(); |
139 |
| - System.out.println("Not accepting client messages anymore."); |
140 |
| - } |
141 |
| - } |
142 |
| - } |
143 |
| - buffer.flip(); |
144 |
| - } finally { |
145 |
| - selector.close(); |
146 |
| - } |
147 |
| - |
148 |
| - return new InputStream() { |
149 |
| - @Override |
150 |
| - public int read() { |
151 |
| - return buffer.hasRemaining() ? (buffer.get() & 0xFF) : -1; |
152 |
| - } |
153 |
| - |
154 |
| - @Override |
155 |
| - public int read(byte[] bytes, int off, int len) { |
156 |
| - if (!buffer.hasRemaining()) { |
157 |
| - return -1; |
158 |
| - } |
159 |
| - len = Math.min(len, buffer.remaining()); |
160 |
| - buffer.get(bytes, off, len); |
161 |
| - return len; |
162 |
| - } |
163 |
| - }; |
| 121 | + return Channels.newInputStream(unixSocketChannel); |
| 122 | + |
| 123 | + // Selector selector = Selector.open(); |
| 124 | + // unixSocketChannel.configureBlocking(false); |
| 125 | + // unixSocketChannel.register(selector, SelectionKey.OP_READ); |
| 126 | + // ByteBuffer buffer = ByteBuffer.allocate(256); |
| 127 | + // |
| 128 | + // try { |
| 129 | + // if (selector.select(timeout) == 0) { |
| 130 | + // System.out.println("Timeout (" + timeout + "ms) while waiting for data."); |
| 131 | + // } |
| 132 | + // for (SelectionKey key : selector.selectedKeys()) { |
| 133 | + // if (key.isReadable()) { |
| 134 | + // int r = unixSocketChannel.read(buffer); |
| 135 | + // if (r == -1) { |
| 136 | + // unixSocketChannel.close(); |
| 137 | + // System.out.println("Not accepting client messages anymore."); |
| 138 | + // } |
| 139 | + // } |
| 140 | + // } |
| 141 | + // buffer.flip(); |
| 142 | + // } finally { |
| 143 | + // selector.close(); |
| 144 | + // } |
| 145 | + // |
| 146 | + // return new InputStream() { |
| 147 | + // @Override |
| 148 | + // public int read() { |
| 149 | + // return buffer.hasRemaining() ? (buffer.get() & 0xFF) : -1; |
| 150 | + // } |
| 151 | + // |
| 152 | + // @Override |
| 153 | + // public int read(byte[] bytes, int off, int len) { |
| 154 | + // if (!buffer.hasRemaining()) { |
| 155 | + // return -1; |
| 156 | + // } |
| 157 | + // len = Math.min(len, buffer.remaining()); |
| 158 | + // buffer.get(bytes, off, len); |
| 159 | + // return len; |
| 160 | + // } |
| 161 | + // }; |
164 | 162 | }
|
165 | 163 |
|
166 | 164 | @Override
|
|
0 commit comments