Browse Source

Merge branch '2.3.x'

Closes gh-21892
pull/21912/head
Scott Frederick 6 years ago
parent
commit
9d9a501b17
  1. 121
      spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/socket/NamedPipeSocket.java

121
spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/socket/NamedPipeSocket.java

@ -16,12 +16,21 @@
package org.springframework.boot.buildpack.platform.socket; package org.springframework.boot.buildpack.platform.socket;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.net.Socket; import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousByteChannel;
import java.nio.channels.AsynchronousCloseException;
import java.nio.channels.AsynchronousFileChannel;
import java.nio.channels.Channels;
import java.nio.channels.CompletionHandler;
import java.nio.file.NoSuchFileException;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.function.Consumer; import java.util.function.Consumer;
@ -32,6 +41,7 @@ import com.sun.jna.platform.win32.Kernel32;
* A {@link Socket} implementation for named pipes. * A {@link Socket} implementation for named pipes.
* *
* @author Phillip Webb * @author Phillip Webb
* @author Scott Frederick
* @since 2.3.0 * @since 2.3.0
*/ */
public class NamedPipeSocket extends Socket { public class NamedPipeSocket extends Socket {
@ -40,27 +50,22 @@ public class NamedPipeSocket extends Socket {
private static final long TIMEOUT = TimeUnit.MILLISECONDS.toNanos(1000); private static final long TIMEOUT = TimeUnit.MILLISECONDS.toNanos(1000);
private final RandomAccessFile file; private final AsynchronousFileByteChannel channel;
private final InputStream inputStream;
private final OutputStream outputStream;
NamedPipeSocket(String path) throws IOException { NamedPipeSocket(String path) throws IOException {
this.file = open(path); this.channel = open(path);
this.inputStream = new NamedPipeInputStream();
this.outputStream = new NamedPipeOutputStream();
} }
private static RandomAccessFile open(String path) throws IOException { private AsynchronousFileByteChannel open(String path) throws IOException {
Consumer<String> awaiter = Platform.isWindows() ? new WindowsAwaiter() : new SleepAwaiter(); Consumer<String> awaiter = Platform.isWindows() ? new WindowsAwaiter() : new SleepAwaiter();
long startTime = System.nanoTime(); long startTime = System.nanoTime();
while (true) { while (true) {
try { try {
return new RandomAccessFile(path, "rw"); return new AsynchronousFileByteChannel(AsynchronousFileChannel.open(Paths.get(path),
StandardOpenOption.READ, StandardOpenOption.WRITE));
} }
catch (FileNotFoundException ex) { catch (NoSuchFileException ex) {
if (System.nanoTime() - startTime > TIMEOUT) { if (System.nanoTime() - startTime >= TIMEOUT) {
throw ex; throw ex;
} }
awaiter.accept(path); awaiter.accept(path);
@ -70,21 +75,19 @@ public class NamedPipeSocket extends Socket {
@Override @Override
public InputStream getInputStream() { public InputStream getInputStream() {
return this.inputStream; return Channels.newInputStream(this.channel);
} }
@Override @Override
public OutputStream getOutputStream() { public OutputStream getOutputStream() {
return this.outputStream; return Channels.newOutputStream(this.channel);
} }
@Override @Override
public void close() throws IOException { public void close() throws IOException {
this.file.close(); if (this.channel != null) {
} this.channel.close();
}
protected final RandomAccessFile getFile() {
return this.file;
} }
/** /**
@ -98,35 +101,81 @@ public class NamedPipeSocket extends Socket {
} }
/** /**
* {@link InputStream} returned from the {@link NamedPipeSocket}. * Adapt an {@code AsynchronousByteChannel} to an {@code AsynchronousFileChannel}.
*/ */
private class NamedPipeInputStream extends InputStream { private static class AsynchronousFileByteChannel implements AsynchronousByteChannel {
private final AsynchronousFileChannel fileChannel;
AsynchronousFileByteChannel(AsynchronousFileChannel fileChannel) {
this.fileChannel = fileChannel;
}
@Override @Override
public int read() throws IOException { public <A> void read(ByteBuffer dst, A attachment, CompletionHandler<Integer, ? super A> handler) {
return getFile().read(); this.fileChannel.read(dst, 0, attachment, new CompletionHandler<Integer, A>() {
@Override
public void completed(Integer read, A attachment) {
handler.completed((read > 0) ? read : -1, attachment);
}
@Override
public void failed(Throwable exc, A attachment) {
if (exc instanceof AsynchronousCloseException) {
handler.completed(-1, attachment);
return;
}
handler.failed(exc, attachment);
}
});
} }
@Override @Override
public int read(byte[] bytes, int off, int len) throws IOException { public Future<Integer> read(ByteBuffer dst) {
return getFile().read(bytes, off, len); CompletableFutureHandler future = new CompletableFutureHandler();
this.fileChannel.read(dst, 0, null, future);
return future;
} }
} @Override
public <A> void write(ByteBuffer src, A attachment, CompletionHandler<Integer, ? super A> handler) {
this.fileChannel.write(src, 0, attachment, handler);
}
/** @Override
* {@link InputStream} returned from the {@link NamedPipeSocket}. public Future<Integer> write(ByteBuffer src) {
*/ return this.fileChannel.write(src, 0);
private class NamedPipeOutputStream extends OutputStream { }
@Override @Override
public void write(int value) throws IOException { public void close() throws IOException {
NamedPipeSocket.this.file.write(value); this.fileChannel.close();
} }
@Override @Override
public void write(byte[] bytes, int off, int len) throws IOException { public boolean isOpen() {
NamedPipeSocket.this.file.write(bytes, off, len); return this.fileChannel.isOpen();
}
private static class CompletableFutureHandler extends CompletableFuture<Integer>
implements CompletionHandler<Integer, Object> {
@Override
public void completed(Integer read, Object attachment) {
complete((read > 0) ? read : -1);
}
@Override
public void failed(Throwable exc, Object attachment) {
if (exc instanceof AsynchronousCloseException) {
complete(-1);
return;
}
completeExceptionally(exc);
}
} }
} }

Loading…
Cancel
Save