Live data from Hacker News

Copying stdin to stdout in Java

gist.github.com

51–60 of 61 posts

Re: Copying stdin to stdout in Java

#52
post #6
post #5

Earlier quoted context omitted.

"Why I like Java" by Mark Dominus: https://news.ycombinator.com/item?id=7463671

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 .

That mangles binary data.

    $ printf 'binarydata'|awk '{ print }'|hexdump -C
    00000000  62 69 6e 61 72 79 64 61  74 61 0a                 |binarydata.|
    0000000b

Re: Copying stdin to stdout in Java

#53
post #6
post #5

Earlier quoted context omitted.

"Why I like Java" by Mark Dominus: https://news.ycombinator.com/item?id=7463671

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 .

[deleted]

Re: Copying stdin to stdout in Java

#54
post #44

The essence of the argument here is that Java is a poor language as it doesn't offer simple abstraction to copy standard input into standard output; that Java is too verbose and low-level for the task. The lack of direct abstraction is not a valid argument, because as a programmer, you shouldn't be writing the logic in Java, Python, C or Scala but rather a higher order domain language implemented in the chosen host l…

Two things.

First, constantly having to write implementations like this introduces a ton of friction as compared with reusing existing implementations. Languages do have an influence on that.

Second, languages vary in "expressiveness," determining how much code you have to write in order to make an implementation like this. In one language, copying stdin to stdout fits into a natural idiom which also handles other cases naturally. In another language with less care for ergonomics, every task might be equally un-idiomatic.

In other words, a language can offer its own "higher order domain languages" for the core tasks that everyone is doing over and over again as part of their general programming. Or it can choose not to do that, because what it offers is already Turing complete. But then the ergonomics are bad, and it makes a real difference.

It seems wrong that when I am paying for things like long JIT warmups and stop-the-world GC, I am still writing piles of functions with low-level idioms that are no more expressive than C's.

If Java ships with a 'copy from one stream to another' primitive then I'd find that a much more compelling argument than that I can treat myself to reimplementing things like stream copying, sorting and basic data structures on a regular basis. It's unbelievably tedious and wasteful to do this, there's just no reason.

Re: Copying stdin to stdout in Java

#55

There are some comments on the Gist claiming Python or Ruby is a one liner. My response: https://gist.github.com/kosta/9777932/#comment-1199293

Your Python code is just wrong. I'm not sure what you think it's proving when you write explicitly wrong Python code, and then show that the output is wrong.

Re: Copying stdin to stdout in Java

#56
post #38

Python, shell, etc. isn't suitable for high scalability, fast applications (e.g. Apache Solr, Lucene, Hadoop etc.). And beginner examples like these make no sense. Java clearly has an important role in this world, maybe not in yours (if you're so irked by "verbosity").

Are you absolutely certain that nobody has used Python in high scalability, fast applications?

Re: Copying stdin to stdout in Java

#57
post #55

There are some comments on the Gist claiming Python or Ruby is a one liner. My response: https://gist.github.com/kosta/9777932/#comment-1199293

Your Python code is just wrong. I'm not sure what you think it's proving when you write explicitly wrong Python code, and then show that the output is wrong.

>Your Python code is just wrong.

Could you elaborate? I want to understand how to fix it.

EDIT: I really want to know what is wrong with my code. It works. Is it a stylistic concern?

    $ dd bs=1m count=10 if=/dev/random of=randomdata
    10+0 records in
    10+0 records out
    10485760 bytes transferred in 0.878533 secs (11935535 bytes/sec)
    $ python stdin_to_stdout.py 
EDIT2: I think I know how you came to the conclusion my code is wrong. Perhaps next time you should read the __entire__ post. Thank you.

Re: Copying stdin to stdout in Java

#58
post #54
post #44

The essence of the argument here is that Java is a poor language as it doesn't offer simple abstraction to copy standard input into standard output; that Java is too verbose and low-level for the task. The lack of direct abstraction is not a valid argument, because as a programmer, you shouldn't be writing the logic in Java, Python, C or Scala but rather a higher order domain language implemented in the chosen host l…

Two things. First, constantly having to write implementations like this introduces a ton of friction as compared with reusing existing implementations. Languages do have an influence on that. Second, languages vary in "expressiveness," determining how much code you have to write in order to make an implementation like this. In one language, copying stdin to stdout fits into a natural idiom which also handles other ca…

Agree, in Java (and any other language) the main culprit is not the complexity that can eventually be abstracted away, but the trivial noise that cannot be abstracted at all: verbose class definitions, clumsy anonymous functions prior to SE8 (functors), lack of implicit interfaces, lack of infix method call notation, generic type erasure, lack of basic type inference, lack of continuations etc.

My point was that limited expression means of a language are sometimes compounded by inability of a programmer to make a good use of the expression means already available to them.

I also understand the desire for more a expressive language, I am a programmer after all. One has to keep in mind, however, that the more expressive a language is the harder it is on the reader. Java code is trivial for a reader to follow (if not for the excessive verbosity sometimes covering up the true intent); much of Scala code base, on the other hand, is not that trivial to comprehend due to the high expressiveness of the language.

Re: Copying stdin to stdout in Java

#59
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…

Wrapping System.in in a Reader is exactly the wrong thing to do. You want to copy the bytes directly, not guess what the character encoding might. The input might not be characters at all.

Re: Copying stdin to stdout in Java

#60
post #41

Earlier quoted context omitted.

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) }

The order of os.Stdout, os.Stdin is interesting. I'd assume src->dest being more intuitive order. Any ideas if this dest By the way, Pipe in the same package is the other way around: ( PipeReader, PipeWriter)

It is by design. All those work in the same direction:

    dest = src // Variable assignment.
    
So the reason is consistency. I suppose the Pipe exception is to map Unix's pipes.
Post reply on HN