-
-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ ChannelReader uses exchange.getRequestReceiver()
- Loading branch information
Showing
3 changed files
with
29 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,13 +20,7 @@ | |
|
||
package org.restheart.utils; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
import java.nio.charset.Charset; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
import org.xnio.channels.Channels; | ||
|
||
import io.undertow.server.HttpServerExchange; | ||
|
||
|
@@ -35,18 +29,21 @@ | |
* @author Andrea Di Cesare {@literal <[email protected]>} | ||
*/ | ||
public class ChannelReader { | ||
|
||
final static Charset CHARSET = StandardCharsets.UTF_8; | ||
final static int CAPACITY = 1024; | ||
|
||
/** | ||
* | ||
* @param exchange | ||
* @return | ||
* @throws IOException | ||
*/ | ||
public static String readString(HttpServerExchange exchange) throws IOException { | ||
return new String(readBytes(exchange), CHARSET); | ||
final var receiver = exchange.getRequestReceiver(); | ||
final var ret = new String[1]; | ||
|
||
receiver.receiveFullString( | ||
(_exchange, data) -> ret[0] = data, | ||
(_exchange, ioe) -> LambdaUtils.throwsSneakyException(ioe)); | ||
|
||
return ret[0]; | ||
} | ||
|
||
/** | ||
|
@@ -56,22 +53,13 @@ public static String readString(HttpServerExchange exchange) throws IOException | |
* @throws IOException | ||
*/ | ||
public static byte[] readBytes(HttpServerExchange exchange) throws IOException { | ||
var channel = exchange.getRequestChannel(); | ||
|
||
if (channel == null) { | ||
return null; | ||
} | ||
|
||
try (var os = new ByteArrayOutputStream(CAPACITY)) { | ||
var buffer = ByteBuffer.allocate(CAPACITY); | ||
final var receiver = exchange.getRequestReceiver(); | ||
final var ret = new byte[1][]; | ||
|
||
while (Channels.readBlocking(channel, buffer) != -1) { | ||
buffer.flip(); | ||
os.write(buffer.array(), 0, buffer.remaining()); | ||
buffer.clear(); | ||
} | ||
receiver.receiveFullBytes( | ||
(_exchange, data) -> ret[0] = data, | ||
(_exchange, ioe) -> LambdaUtils.throwsSneakyException(ioe)); | ||
|
||
return os.toByteArray(); | ||
} | ||
return ret[0]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters