|
| 1 | +package com.github.dockerjava.transport; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.InputStream; |
| 5 | +import java.io.OutputStream; |
| 6 | +import java.net.Socket; |
| 7 | +import java.net.SocketAddress; |
| 8 | +import java.net.SocketException; |
| 9 | +import java.nio.channels.Channels; |
| 10 | +import java.nio.channels.SocketChannel; |
| 11 | + |
| 12 | +public class UnixSocket extends AbstractSocket { |
| 13 | + |
| 14 | + /** |
| 15 | + * Return a new {@link Socket} for the given path. Will use JDK's {@link java.net.UnixDomainSocketAddress} |
| 16 | + * if available and fallback to {@link DomainSocket} otherwise. |
| 17 | + * |
| 18 | + * @param path the path to the domain socket |
| 19 | + * @return a {@link Socket} instance |
| 20 | + * @throws IOException if the socket cannot be opened |
| 21 | + */ |
| 22 | + public static Socket get(String path) throws IOException { |
| 23 | + try { |
| 24 | + return new UnixSocket(path); |
| 25 | + } catch (Exception e) { |
| 26 | + //noinspection deprecation |
| 27 | + return DomainSocket.get(path); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + private final SocketAddress socketAddress; |
| 32 | + |
| 33 | + private final SocketChannel socketChannel; |
| 34 | + |
| 35 | + private UnixSocket(String path) throws Exception { |
| 36 | + Class<?> unixDomainSocketAddress = Class.forName("java.net.UnixDomainSocketAddress"); |
| 37 | + this.socketAddress = |
| 38 | + (SocketAddress) unixDomainSocketAddress.getMethod("of", String.class) |
| 39 | + .invoke(null, path); |
| 40 | + this.socketChannel = SocketChannel.open(this.socketAddress); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public InputStream getInputStream() throws IOException { |
| 45 | + if (isClosed()) { |
| 46 | + throw new SocketException("Socket is closed"); |
| 47 | + } |
| 48 | + if (!isConnected()) { |
| 49 | + throw new SocketException("Socket is not connected"); |
| 50 | + } |
| 51 | + if (isInputShutdown()) { |
| 52 | + throw new SocketException("Socket input is shutdown"); |
| 53 | + } |
| 54 | + |
| 55 | + return Channels.newInputStream(socketChannel); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public OutputStream getOutputStream() throws IOException { |
| 60 | + if (isClosed()) { |
| 61 | + throw new SocketException("Socket is closed"); |
| 62 | + } |
| 63 | + if (!isConnected()) { |
| 64 | + throw new SocketException("Socket is not connected"); |
| 65 | + } |
| 66 | + if (isOutputShutdown()) { |
| 67 | + throw new SocketException("Socket output is shutdown"); |
| 68 | + } |
| 69 | + |
| 70 | + return Channels.newOutputStream(socketChannel); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public SocketAddress getLocalSocketAddress() { |
| 75 | + return socketAddress; |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public SocketAddress getRemoteSocketAddress() { |
| 80 | + return socketAddress; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public void close() throws IOException { |
| 85 | + super.close(); |
| 86 | + this.socketChannel.close(); |
| 87 | + } |
| 88 | +} |
0 commit comments