Live data from Hacker News

Copying stdin to stdout in Java

gist.github.com

11–20 of 61 posts

Re: Copying stdin to stdout in Java

#11
I don't know much Java. Does 'System.in.read(buffer)' block until stdin is at EOF or buffer is full? Then this solution would be somewhat broken. Imagine a low-bandwidth source on stdin and a audio player connected to stdout, like

  wget -O - http://audiostream | java inout.java | mplayer /dev/stdin
If I had to write this in C I'd use file descriptors in non-blocking mode and select(). With Tcl one could use event-based IO.

Re: Copying stdin to stdout in Java

#12

I don't know much Java. Does 'System.in.read(buffer)' block until stdin is at EOF or buffer is full? Then this solution would be somewhat broken. Imagine a low-bandwidth source on stdin and a audio player connected to stdout, like wget -O - http://audiostream | java inout.java | mplayer /dev/stdin If I had to write this in C I'd use file descriptors in non-blocking mode and select(). With Tcl one could use event-base…

[deleted]

Re: Copying stdin to stdout in Java

#13
post #9

If you want a single point generalization about Java programmers, I was one for years, and the way I would have implemented this involves minimally a BufferedReader and an InputStreamReader. You'll also probably need to catch the IOException. Plus or minus 20 lines or so. I don't hate Java, but "Java as it is spoken" does not often get used for Unix-style piping in a chain of small programs on a command line. It also…

Sure, but I wouldn't bring my forklift out of storage to move a sack of potatoes across the room either which is what you're doing when you use Java for a 10 line shell script. That's not what Java is designed for.

Re: Copying stdin to stdout in Java

#14
Amazingly, in Java 8, they added a Files utility class to copy an InputStream to a Path, and copy a Path to an OutputStream, but no Files.copy(InputStream, OutputStream).

At this point, Guava should just be added as part of the JDK. :) In Guava, ByteStreams.copy(System.in, System.out)

Re: Copying stdin to stdout in Java

#15

I don't have the post context, but at 13 lines, I feel even something like Go wouldn't even be considerably larger or marginally easier to understand.

It's not much shorter, but it was pretty easy to write it you know the libraries.

    package main

    import ("os"; "io")

    func main() {
        io.Copy(os.Stdout, os.Stdin)
    }

Re: Copying stdin to stdout in Java

#16
post #8
post #6

Earlier quoted context omitted.

Who never wrote it but assumed we know that the same program in Perl, without using command line switches is while ( ) { print; } Still Awk is shorter: { print } And if you are allowed change the switches of the command line to Perl for your program that Perl program can have exactly 0 bytes .

Awk is even shorter: anything truthy works. You don't even need the print statement or brackets. Thus there are nine separate 1-byte programs which produce cat: [1], [2], [3], etc etc. By default, unless you use the BEGIN block or something, awk will run your program on each line of stdlin. This is useful for programs of the type: (/some regular expression/) { some action} The default action if you don't specify one…

You are right, the shortest awk is one byte. Please post the link all nine variants I wasn't able to Google it.

Still the shortest Perl is 0 bytes when the command line switch is allowed to be -p

Update: That's actually called "the sed mode of Perl." Moreover these two command lines behave similarly:

    perl -pe 's/search/replace/g'
and

    sed 's/search/replace/'

Re: Copying stdin to stdout in Java

#17
post #10
post #6

Earlier quoted context omitted.

Who never wrote it but assumed we know that the same program in Perl, without using command line switches is while ( ) { print; } Still Awk is shorter: { print } And if you are allowed change the switches of the command line to Perl for your program that Perl program can have exactly 0 bytes .

Sure, but if you want to modify this code to send stdin over the network or through bluetooth in the Java version, you would just replace 'System.out' with the socket. The code would remain the same - in fact the whole class could be reused by having it pass in an input and output stream as parameters. Your example is very much just a special shortcut for one special condition. It's not generalizable in the same way.…

In Linux you can redirect the stdout to the the socket outside of your program, no programming required. You can test and develop everything with stdout and let the same unmodified file run in the web server. See traditional CGI, that's how dynamically generated web pages were easiest to implement long ago:

http://computer.howstuffworks.com/cgi3.htm

The server opens the socket, redirects the stdout to his socket while calling your program fully unmodified.

Re: Copying stdin to stdout in Java

#18
post #13
post #9

If you want a single point generalization about Java programmers, I was one for years, and the way I would have implemented this involves minimally a BufferedReader and an InputStreamReader. You'll also probably need to catch the IOException. Plus or minus 20 lines or so. I don't hate Java, but "Java as it is spoken" does not often get used for Unix-style piping in a chain of small programs on a command line. It also…

Sure, but I wouldn't bring my forklift out of storage to move a sack of potatoes across the room either which is what you're doing when you use Java for a 10 line shell script. That's not what Java is designed for.

Nor you grab an camel when you want a ride, as there are better options.

Re: Copying stdin to stdout in Java

#19
post #17
post #10

Earlier quoted context omitted.

Sure, but if you want to modify this code to send stdin over the network or through bluetooth in the Java version, you would just replace 'System.out' with the socket. The code would remain the same - in fact the whole class could be reused by having it pass in an input and output stream as parameters. Your example is very much just a special shortcut for one special condition. It's not generalizable in the same way.…

In Linux you can redirect the stdout to the the socket outside of your program, no programming required. You can test and develop everything with stdout and let the same unmodified file run in the web server. See traditional CGI, that's how dynamically generated web pages were easiest to implement long ago: http://computer.howstuffworks.com/cgi3.htm The server opens the socket, redirects the stdout to his socket whil…

You open network sockets in Linux and redirect stdout to them? It's possible with curl I guess, but generally people don't do that as it's hard to send the data in the right format. Mostly people will use a tool to access network services.

As for serving files, you'd normally let nginx or apache take care of the sockets for you and not use output redirection.

In Plan9 it's actually viable to use network sockets as files which is pretty nice, but for Linux it's not really how people do it.

Re: Copying stdin to stdout in Java

#20

I don't know much Java. Does 'System.in.read(buffer)' block until stdin is at EOF or buffer is full? Then this solution would be somewhat broken. Imagine a low-bandwidth source on stdin and a audio player connected to stdout, like wget -O - http://audiostream | java inout.java | mplayer /dev/stdin If I had to write this in C I'd use file descriptors in non-blocking mode and select(). With Tcl one could use event-base…

Using non-blocking mode may be better, but to answer your question: yes, it does block [1].

[1] http://docs.oracle.com/javase/7/docs/api/java/io/InputStream...

Post reply on HN