bschuchardt commented on a change in pull request #5363:
URL: https://github.com/apache/geode/pull/5363#discussion_r456519338
##########
File path:
geode-core/src/main/java/org/apache/geode/internal/net/SocketUtils.java
##########
@@ -73,4 +82,49 @@ public static boolean close(final ServerSocket serverSocket)
{
return true;
}
+ /**
+ * Read data from the given socket into the given ByteBuffer. If NIO is
supported
+ * we use Channel.read(ByteBuffer). If not we use byte arrays to read
available
+ * bytes or buffer.remaining() bytes, whichever is smaller. If buffer.limit
is zero
+ * and buffer.remaining is also zero the limit is changed to be
buffer.capacity
+ * before reading.
+ *
+ * @return the number of bytes read, which may be -1 for EOF
+ */
+ public static int readFromSocket(Socket socket, ByteBuffer inputBuffer,
+ InputStream socketInputStream) throws IOException {
+ int amountRead;
+ inputBuffer.limit(inputBuffer.capacity());
+ if (socket instanceof SSLSocket) {
+ amountRead = readFromStream(socketInputStream, inputBuffer);
+ } else {
+ amountRead = socket.getChannel().read(inputBuffer);
+ }
+ return amountRead;
+ }
+
+ private static int readFromStream(InputStream stream, ByteBuffer
inputBuffer) throws IOException {
+ int amountRead;
+ // if bytes are available we read that number of bytes. Otherwise we do a
blocking read
+ // of buffer.remaining() bytes
+ int amountToRead = inputBuffer.remaining();
+ // stream.available() > 0 ? Math.min(stream.available(),
inputBuffer.remaining())
+ // : inputBuffer.remaining();
+ if (inputBuffer.hasArray()) {
+ amountRead = stream.read(inputBuffer.array(),
+ inputBuffer.arrayOffset() + inputBuffer.position(), amountToRead);
+ if (amountRead > 0) {
+ inputBuffer.position(inputBuffer.position() + amountRead);
+ }
+ } else {
Review comment:
I've added tests for this, so I'm resolving this thread.
----------------------------------------------------------------
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]