|
1 | 1 | /* |
2 | | - * Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * The Universal Permissive License (UPL), Version 1.0 |
|
43 | 43 | import java.io.IOException; |
44 | 44 | import java.io.InputStream; |
45 | 45 | import java.io.OutputStream; |
| 46 | +import java.nio.ByteBuffer; |
46 | 47 | import java.nio.channels.Channel; |
47 | 48 | import java.nio.channels.Channels; |
48 | 49 | import java.nio.channels.FileLock; |
49 | 50 | import java.nio.channels.Pipe; |
50 | 51 | import java.nio.channels.SeekableByteChannel; |
| 52 | +import java.nio.channels.WritableByteChannel; |
51 | 53 | import java.util.ArrayList; |
52 | 54 | import java.util.Collections; |
53 | 55 | import java.util.HashMap; |
@@ -154,6 +156,33 @@ public Process destroyForcibly() { |
154 | 156 | } |
155 | 157 | } |
156 | 158 |
|
| 159 | + private static class FlushingWritableByteChannel implements WritableByteChannel { |
| 160 | + private final OutputStream stream; |
| 161 | + private final WritableByteChannel delegate; |
| 162 | + |
| 163 | + private FlushingWritableByteChannel(OutputStream stream) { |
| 164 | + this.stream = stream; |
| 165 | + delegate = Channels.newChannel(stream); |
| 166 | + } |
| 167 | + |
| 168 | + @Override |
| 169 | + public int write(ByteBuffer src) throws IOException { |
| 170 | + int res = delegate.write(src); |
| 171 | + stream.flush(); |
| 172 | + return res; |
| 173 | + } |
| 174 | + |
| 175 | + @Override |
| 176 | + public boolean isOpen() { |
| 177 | + return delegate.isOpen(); |
| 178 | + } |
| 179 | + |
| 180 | + @Override |
| 181 | + public void close() throws IOException { |
| 182 | + delegate.close(); |
| 183 | + } |
| 184 | + } |
| 185 | + |
157 | 186 | private static class ChannelWrapper { |
158 | 187 | Channel channel; |
159 | 188 | int cnt; |
@@ -184,7 +213,7 @@ void setNewChannel(InputStream inputStream) { |
184 | 213 | } |
185 | 214 |
|
186 | 215 | void setNewChannel(OutputStream outputStream) { |
187 | | - this.channel = Channels.newChannel(outputStream); |
| 216 | + this.channel = new FlushingWritableByteChannel(outputStream); |
188 | 217 | this.cnt = 1; |
189 | 218 | } |
190 | 219 | } |
|
0 commit comments