Skip to content

Instantly share code, notes, and snippets.

@ivanursul
Created June 29, 2016 18:03
Show Gist options
  • Save ivanursul/71e53b3ed4bae44388d22ac6280b06a5 to your computer and use it in GitHub Desktop.
Save ivanursul/71e53b3ed4bae44388d22ac6280b06a5 to your computer and use it in GitHub Desktop.
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.TeeInputStream;
import org.apache.commons.io.output.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
String string = Stream.iterate(1L, n -> n + 1)
.limit(10000)
.map(n -> String.format("Number%d", n))
.collect(Collectors.joining(" "));
try (InputStream is = new ByteArrayInputStream(string.getBytes())) {
doSomething(is);
} catch (IOException e) {
e.printStackTrace();
}
}
private static void doSomething(InputStream is) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
TeeInputStream teeInputStream = new TeeInputStream(is, out);
try {
String line = IOUtils.toString(teeInputStream);
System.out.println("main thread line: " + line);
} catch (IOException e) {
e.printStackTrace();
}
ByteArrayInputStream btis = new ByteArrayInputStream(out.toByteArray());
Thread t1 = new Thread(() -> {
try {
String line = IOUtils.toString(btis);
System.out.println("t1 line: " + line);
} catch (IOException e) {
e.printStackTrace();
}
});
t1.start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment