markap14 commented on a change in pull request #3578: NIFI-5952 [WIP] Fixing
RAW S2S illegal blocking mode on Java 11
URL: https://github.com/apache/nifi/pull/3578#discussion_r301656105
##########
File path:
nifi-commons/nifi-site-to-site-client/src/main/java/org/apache/nifi/remote/codec/StandardFlowFileCodec.java
##########
@@ -56,13 +62,32 @@ public void encode(final DataPacket dataPacket, final
OutputStream encodedOut) t
writeString(entry.getValue(), out);
}
- out.writeLong(dataPacket.getSize());
+ final long dataSize = dataPacket.getSize();
+ out.writeLong(dataSize);
final InputStream in = dataPacket.getData();
- StreamUtils.copy(in, encodedOut);
+ copy(dataSize, in, encodedOut);
encodedOut.flush();
}
+ private long copy(long length, final InputStream source, final
OutputStream destination) throws IOException {
+
+ final StopWatch stopWatch = new StopWatch(false);
+ final byte[] buffer = new byte[8192];
+ long totalRead = 0;
+ // Use 'available' so that it doesn't block
+ for (int available; totalRead < length && (available =
source.available()) > 0;) {
Review comment:
We cannot use InputStream.available() for this purpose. See
https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#available()
for an explanation of the method. It is an "estimate" of the number of bytes,
but a great number of InputStreams simply return `0`, which makes the result
unreliable. In such a case, this will immediately return without copying any
bytes.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services