Last active
August 27, 2021 14:18
-
-
Save cbdyzj/5a9612a00db7fdcf1b9fc314b3c58c70 to your computer and use it in GitHub Desktop.
java_bio
This file contains hidden or 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
var handler = (Consumer<byte[]>) (bytes) -> { | |
CompletableFuture.runAsync(() -> { | |
var readStr = new String(bytes); | |
System.out.println("readStr: " + readStr); | |
}); | |
}; | |
var inputStream = System.in; | |
var readThread = new Thread(() -> { | |
try { | |
var DEFAULT_BUFFER_SIZE = 8192; | |
var buffer = new byte[DEFAULT_BUFFER_SIZE]; | |
int read; | |
while ((read = inputStream.read(buffer, 0, DEFAULT_BUFFER_SIZE)) >= 0) { | |
var readBytes = new byte[read]; | |
System.arraycopy(buffer, 0, readBytes, 0, read); | |
handler.accept(readBytes); | |
} | |
} catch (IOException ex) { | |
throw new UncheckedIOException(ex); | |
} | |
}); | |
readThread.start(); | |
readThread.join(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment