Skip to content

Instantly share code, notes, and snippets.

@pageman
Forked from kosta/inout.java
Created March 26, 2014 10:44
Show Gist options
  • Save pageman/9780643 to your computer and use it in GitHub Desktop.
Save pageman/9780643 to your computer and use it in GitHub Desktop.
import java.io.IOException;
/**
* Class that copies stdin to stdout, as compained about as not being cleanly
* writable in Java on Hacker News.
* In real code, you would just write IOUtils.copy(System.in, System.out),
* which does basically the same thing.
* This does not catch any exceptions as a) this is just an "exercise" and
* b) all we could do with them is pretty-print them. So let the runtime
* print them for you.
*
* Usage: javac inout.java && echo -e "foo\nbar" | java InOut
*/
class InOut {
public static void main(String[] args) throws IOException {
byte[] buffer = new byte[8192];
while(true) {
int bytesRead = System.in.read(buffer);
if (bytesRead == -1) {
return;
}
System.out.write(buffer, 0, bytesRead);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment