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.Copying stdin to stdout in Java
11–20 of 61 posts
Re: Copying stdin to stdout in Java
#12I 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…
Re: Copying stdin to stdout in Java
#13If 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…
Re: Copying stdin to stdout in Java
#14At 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
#15I 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.
package main
import ("os"; "io")
func main() {
io.Copy(os.Stdout, os.Stdin)
}Re: Copying stdin to stdout in Java
#16Earlier 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…
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
#17Earlier 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.…
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
#18If 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
#19Earlier 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…
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
#20I 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…
[1] http://docs.oracle.com/javase/7/docs/api/java/io/InputStream...