|
| 1 | +/* |
| 2 | +Copyright 2020 The Kubernetes Authors. |
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +Unless required by applicable law or agreed to in writing, software |
| 8 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +See the License for the specific language governing permissions and |
| 11 | +limitations under the License. |
| 12 | +*/ |
| 13 | +package io.kubernetes.client.custom; |
| 14 | + |
| 15 | +import java.io.InputStream; |
| 16 | +import java.util.concurrent.*; |
| 17 | + |
| 18 | +public class AsyncPump implements Runnable { |
| 19 | + |
| 20 | + private Thread thread; |
| 21 | + private ExecutorService executorService; |
| 22 | + private byte[] buff; |
| 23 | + private InputStream is; |
| 24 | + private CompletableFuture<?> promise; |
| 25 | + private NoisyConsumer<byte[]> consumer; |
| 26 | + private NoisyConsumer<Exception> errorHandler; |
| 27 | + private NoisyRunnable onFinish; |
| 28 | + |
| 29 | + public AsyncPump(NoisyConsumer<byte[]> consumer, NoisyConsumer<Exception> errorHandler, NoisyRunnable onFinish) { |
| 30 | + buff = new byte[1024]; |
| 31 | + this.consumer = consumer != null ? consumer : NoisyConsumer.NOP; |
| 32 | + this.errorHandler = errorHandler != null ? errorHandler : NoisyConsumer.NOP; |
| 33 | + this.onFinish = onFinish != null ? onFinish : NoisyRunnable.NOP; |
| 34 | + this.promise = new CompletableFuture<>(); |
| 35 | + } |
| 36 | + |
| 37 | + public AsyncPump(NoisyConsumer<byte[]> consumer, NoisyRunnable onFinish) { |
| 38 | + this(consumer, Exception::printStackTrace, onFinish); |
| 39 | + } |
| 40 | + |
| 41 | + public AsyncPump(NoisyConsumer<byte[]> consumer) { |
| 42 | + this(consumer, NoisyRunnable.NOP); |
| 43 | + } |
| 44 | + |
| 45 | + public AsyncPump setExecutorService(ExecutorService executorService) { |
| 46 | + this.executorService = executorService; |
| 47 | + return this; |
| 48 | + } |
| 49 | + |
| 50 | + public AsyncPump stop() { |
| 51 | + if (thread != null) |
| 52 | + thread.interrupt(); |
| 53 | + return this; |
| 54 | + } |
| 55 | + |
| 56 | + public AsyncPump startAsync(InputStream is) { |
| 57 | + this.is = is; |
| 58 | + |
| 59 | + if (executorService != null) { |
| 60 | + executorService.submit(this); |
| 61 | + } else { |
| 62 | + thread = new Thread(this); |
| 63 | + thread.setName("Async pump"); |
| 64 | + thread.start(); |
| 65 | + } |
| 66 | + return this; |
| 67 | + } |
| 68 | + |
| 69 | + public boolean waitForExit() { |
| 70 | + try { |
| 71 | + promise.get(); |
| 72 | + return true; |
| 73 | + } catch (InterruptedException | ExecutionException e) { |
| 74 | + return false; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + public boolean waitForExit(long timeoutMs) { |
| 79 | + try { |
| 80 | + promise.get(timeoutMs, TimeUnit.MILLISECONDS); |
| 81 | + return true; |
| 82 | + } catch (InterruptedException | ExecutionException | TimeoutException e) { |
| 83 | + return false; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + public AsyncPump start(InputStream is) { |
| 88 | + this.is = is; |
| 89 | + return this; |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public void run() { |
| 94 | + int nRead; |
| 95 | + try { |
| 96 | + while ((nRead = is.read(buff)) > -1) { |
| 97 | + byte[] giveAway = new byte[nRead]; |
| 98 | + System.arraycopy(buff, 0, giveAway, 0, nRead); |
| 99 | + consumer.accept(giveAway); |
| 100 | + } |
| 101 | + onFinish.run(); |
| 102 | + } catch (Exception e) { |
| 103 | + try { |
| 104 | + errorHandler.accept(e); |
| 105 | + } catch (Exception exception) { |
| 106 | + promise.completeExceptionally(exception); |
| 107 | + } |
| 108 | + } |
| 109 | + promise.complete(null); |
| 110 | + } |
| 111 | + |
| 112 | + public interface NoisyConsumer<T> { |
| 113 | + NoisyConsumer NOP = t -> { }; |
| 114 | + |
| 115 | + void accept(T t) throws Exception; |
| 116 | + } |
| 117 | + |
| 118 | + public interface NoisyRunnable { |
| 119 | + NoisyRunnable NOP = () -> { }; |
| 120 | + |
| 121 | + void run() throws Exception; |
| 122 | + } |
| 123 | +} |
0 commit comments