Skip to content

Commit 05cb53a

Browse files
committed
chore
1 parent 9505198 commit 05cb53a

9 files changed

Lines changed: 172 additions & 174 deletions

File tree

src/main/java/de/gesellix/docker/client/filesocket/ExtendedKernel32.java

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,30 @@
44
import com.sun.jna.Native;
55
import com.sun.jna.Pointer;
66
import com.sun.jna.platform.win32.WinBase;
7-
import com.sun.jna.platform.win32.WinDef;
87
import com.sun.jna.platform.win32.WinNT;
98
import com.sun.jna.ptr.IntByReference;
109
import com.sun.jna.win32.W32APIOptions;
1110

1211
public interface ExtendedKernel32 extends Library {
1312

14-
ExtendedKernel32 INSTANCE = Native.load("kernel32", ExtendedKernel32.class, W32APIOptions.DEFAULT_OPTIONS);
13+
ExtendedKernel32 INSTANCE = Native.load("kernel32", ExtendedKernel32.class, W32APIOptions.DEFAULT_OPTIONS);
1514

16-
boolean GetOverlappedResult(
15+
boolean GetOverlappedResult(
1716
WinNT.HANDLE hFile,
1817
WinBase.OVERLAPPED lpOverlapped,
1918
IntByReference lpNumberOfBytesTransferred,
2019
boolean bWait
21-
);
20+
);
2221

23-
/**
24-
* Cancels all pending I/O operations on the specified file handle.
25-
*
26-
* @param hFile The handle to the file or I/O device.
27-
* @param lpOverlapped Reserved; should be NULL.
28-
* @return true if successful; false otherwise.
29-
*/
30-
boolean CancelIoEx(
22+
/**
23+
* Cancels all pending I/O operations on the specified file handle.
24+
*
25+
* @param hFile The handle to the file or I/O device.
26+
* @param lpOverlapped Reserved; should be NULL.
27+
* @return true if successful; false otherwise.
28+
*/
29+
boolean CancelIoEx(
3130
WinNT.HANDLE hFile,
3231
Pointer lpOverlapped
33-
);
34-
35-
WinDef.DWORD WaitForSingleObject(
36-
WinNT.HANDLE hHandle,
37-
int dwMilliseconds
38-
);
39-
40-
boolean CloseHandle(
41-
WinNT.HANDLE hObject
42-
);
32+
);
4333
}

src/main/java/de/gesellix/docker/client/filesocket/FileSocketFactory.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
package de.gesellix.docker.client.filesocket;
22

3-
import okhttp3.Dns;
4-
5-
import javax.net.SocketFactory;
63
import java.net.InetAddress;
74
import java.net.Socket;
85
import java.net.UnknownHostException;
96
import java.util.Collections;
107
import java.util.List;
118

9+
import javax.net.SocketFactory;
10+
11+
import okhttp3.Dns;
12+
1213
public abstract class FileSocketFactory extends SocketFactory implements Dns {
1314

1415
@Override
1516
public List<InetAddress> lookup(String hostname) throws UnknownHostException {
1617
if (hostname.endsWith(FileSocket.SOCKET_MARKER)) {
17-
return Collections.singletonList(InetAddress.getByAddress(hostname, new byte[] {0, 0, 0, 0}));
18-
}
19-
else {
18+
return Collections.singletonList(InetAddress.getByAddress(hostname, new byte[]{0, 0, 0, 0}));
19+
} else {
2020
return Dns.SYSTEM.lookup(hostname);
2121
}
2222
}

src/main/java/de/gesellix/docker/client/filesocket/HostnameEncoder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package de.gesellix.docker.client.filesocket;
22

3-
import okio.ByteString;
4-
53
import java.util.ArrayList;
64
import java.util.List;
75

6+
import okio.ByteString;
7+
88
public class HostnameEncoder {
99

1010
/**

src/main/java/de/gesellix/docker/client/filesocket/NamedPipeSink.java

Lines changed: 45 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -15,54 +15,58 @@
1515

1616
public class NamedPipeSink implements Sink {
1717

18-
private final WinNT.HANDLE handle;
19-
private final int timeoutMillis;
18+
private final WinNT.HANDLE handle;
19+
private final int timeoutMillis;
2020

21-
public NamedPipeSink(WinNT.HANDLE handle, int timeoutMillis) {
22-
this.handle = handle;
23-
this.timeoutMillis = timeoutMillis;
24-
}
25-
26-
@Override
27-
public void write(@NotNull Buffer source, long byteCount) throws IOException {
28-
if (byteCount == 0) {
29-
return;
30-
}
31-
if (byteCount > Integer.MAX_VALUE) {
32-
throw new IllegalArgumentException("Can only write max " + Integer.MAX_VALUE + " bytes");
33-
}
21+
public NamedPipeSink(WinNT.HANDLE handle, Timeout timeout) {
22+
this.handle = handle;
23+
this.timeoutMillis = (int) timeout.timeoutNanos() / 1000;
24+
}
3425

35-
byte[] data = source.readByteArray(byteCount);
36-
IntByReference bytesWritten = new IntByReference();
37-
//boolean ok = NamedPipeUtils.writeFromBuffer(handle, data, data.length, bytesWritten);
38-
boolean ok = NamedPipeUtils.writeOverlapped(handle, data, data.length, bytesWritten, timeoutMillis);
39-
if (!ok) {
40-
int err = Kernel32.INSTANCE.GetLastError();
41-
if (err == WinError.ERROR_OPERATION_ABORTED) {
42-
// Expected when CancelIoEx() is called during close()
43-
return;
44-
}
45-
throw new IOException("Failed to write to Named Pipe. WinError=" + err);
46-
}
47-
48-
if (bytesWritten.getValue() <= 0) {
49-
throw new IOException("No bytes written to Named Pipe");
50-
}
26+
@Override
27+
public void write(@NotNull Buffer source, long byteCount) throws IOException {
28+
if (byteCount < 0) {
29+
throw new IllegalArgumentException("Invalid buffer size: " + byteCount);
30+
}
31+
if (byteCount > Integer.MAX_VALUE) {
32+
throw new IllegalArgumentException("Can only write max " + Integer.MAX_VALUE + " bytes");
5133
}
5234

53-
@Override
54-
public void flush() {
55-
// No-op for Named Pipes
35+
if (byteCount == 0) {
36+
return;
5637
}
5738

58-
@NotNull
59-
@Override
60-
public Timeout timeout() {
61-
return Timeout.NONE;
39+
byte[] data = source.readByteArray(byteCount);
40+
IntByReference bytesWritten = new IntByReference();
41+
//boolean ok = NamedPipeUtils.writeFromBuffer(handle, data, data.length, bytesWritten);
42+
boolean ok = NamedPipeUtils.writeOverlapped(handle, data, data.length, bytesWritten, timeoutMillis);
43+
if (!ok) {
44+
int err = Kernel32.INSTANCE.GetLastError();
45+
if (err == WinError.ERROR_OPERATION_ABORTED) {
46+
// Expected when CancelIoEx() is called during close()
47+
return;
48+
}
49+
throw new IOException("Failed to write to Named Pipe. WinError=" + err);
6250
}
6351

64-
@Override
65-
public void close() {
66-
// No-op, handle closed by NamedPipeSocket
52+
if (bytesWritten.getValue() <= 0) {
53+
throw new IOException("No bytes written to Named Pipe");
6754
}
55+
}
56+
57+
@Override
58+
public void flush() {
59+
// No-op for Named Pipes
60+
}
61+
62+
@NotNull
63+
@Override
64+
public Timeout timeout() {
65+
return Timeout.NONE;
66+
}
67+
68+
@Override
69+
public void close() {
70+
// No-op, handle closed by NamedPipeSocket
71+
}
6872
}

src/main/java/de/gesellix/docker/client/filesocket/NamedPipeSocket.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.net.InetAddress;
1414
import java.net.InetSocketAddress;
1515
import java.net.SocketAddress;
16+
import java.util.concurrent.TimeUnit;
1617

1718
import org.slf4j.Logger;
1819
import org.slf4j.LoggerFactory;
@@ -23,6 +24,7 @@
2324
import okio.BufferedSink;
2425
import okio.BufferedSource;
2526
import okio.Okio;
27+
import okio.Timeout;
2628

2729
public class NamedPipeSocket extends FileSocket {
2830

@@ -35,7 +37,7 @@ public class NamedPipeSocket extends FileSocket {
3537
private BufferedSource source;
3638
private BufferedSink sink;
3739

38-
private final int ioTimeoutMillis = 2000;
40+
private final Timeout ioTimeout = new Timeout().timeout(2000, TimeUnit.MILLISECONDS);
3941

4042
@Override
4143
public void connect(SocketAddress endpoint, int timeout) throws IOException {
@@ -71,8 +73,8 @@ void connect(String socketPath) throws IOException {
7173
}
7274

7375
connected = true;
74-
source = Okio.buffer(new NamedPipeSource(handle, ioTimeoutMillis));
75-
sink = Okio.buffer(new NamedPipeSink(handle, ioTimeoutMillis));
76+
source = Okio.buffer(new NamedPipeSource(handle, ioTimeout));
77+
sink = Okio.buffer(new NamedPipeSink(handle, ioTimeout));
7678
}
7779

7880
@Override

src/main/java/de/gesellix/docker/client/filesocket/NamedPipeSource.java

Lines changed: 45 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -13,53 +13,57 @@
1313

1414
public class NamedPipeSource implements Source {
1515

16-
private final WinNT.HANDLE handle;
17-
private final int timeoutMillis;
16+
private final WinNT.HANDLE handle;
17+
private final int timeoutMillis;
1818

19-
public NamedPipeSource(WinNT.HANDLE handle, int timeoutMillis) {
20-
this.handle = handle;
21-
this.timeoutMillis = timeoutMillis;
22-
}
23-
24-
@Override
25-
public long read(@NotNull Buffer sink, long byteCount) {
26-
if (byteCount == 0) {
27-
return 0;
28-
}
29-
if (byteCount > Integer.MAX_VALUE) {
30-
throw new IllegalArgumentException("Can only read max " + Integer.MAX_VALUE + " bytes");
31-
}
19+
public NamedPipeSource(WinNT.HANDLE handle, Timeout timeout) {
20+
this.handle = handle;
21+
this.timeoutMillis = (int) timeout.timeoutNanos() / 1000;
22+
}
3223

33-
byte[] data = new byte[(int) byteCount];
34-
IntByReference bytesRead = new IntByReference();
35-
//boolean ok = NamedPipeUtils.readToBuffer(handle, data, bytesRead);
36-
boolean ok = NamedPipeUtils.readOverlapped(handle, data, bytesRead, timeoutMillis);
37-
if (!ok) {
38-
int err = Kernel32.INSTANCE.GetLastError();
39-
if (err == WinError.ERROR_OPERATION_ABORTED) {
40-
// Expected when CancelIoEx() is called during close()
41-
return -1;
42-
}
43-
return -1; // Other read error
44-
}
45-
46-
if (bytesRead.getValue() <= 0) {
47-
return -1; // EOF
48-
}
24+
@Override
25+
public long read(@NotNull Buffer sink, long byteCount) {
26+
if (byteCount < 0) {
27+
throw new IllegalArgumentException("Invalid buffer size: " + byteCount);
28+
}
29+
if (byteCount > Integer.MAX_VALUE) {
30+
throw new IllegalArgumentException("Can only read max " + Integer.MAX_VALUE + " bytes");
31+
}
4932

50-
sink.write(data, 0, bytesRead.getValue());
51-
sink.flush();
52-
return bytesRead.getValue();
33+
if (byteCount == 0) {
34+
return 0;
5335
}
5436

55-
@NotNull
56-
@Override
57-
public Timeout timeout() {
58-
return Timeout.NONE;
37+
byte[] data = new byte[(int) byteCount];
38+
IntByReference bytesRead = new IntByReference();
39+
//boolean ok = NamedPipeUtils.readToBuffer(handle, data, bytesRead);
40+
boolean ok = NamedPipeUtils.readOverlapped(handle, data, bytesRead, timeoutMillis);
41+
if (!ok) {
42+
int err = Kernel32.INSTANCE.GetLastError();
43+
if (err == WinError.ERROR_OPERATION_ABORTED) {
44+
// Expected when CancelIoEx() is called during close()
45+
return -1;
46+
}
47+
return -1; // Other read error
5948
}
6049

61-
@Override
62-
public void close() {
63-
// No-op, handle closed by NamedPipeSocket
50+
if (bytesRead.getValue() <= 0) {
51+
return -1; // EOF
6452
}
53+
54+
sink.write(data, 0, bytesRead.getValue());
55+
sink.flush();
56+
return bytesRead.getValue();
57+
}
58+
59+
@NotNull
60+
@Override
61+
public Timeout timeout() {
62+
return Timeout.NONE;
63+
}
64+
65+
@Override
66+
public void close() {
67+
// No-op, handle closed by NamedPipeSocket
68+
}
6569
}

0 commit comments

Comments
 (0)