News for hackers. Stuff that matters.
Re:Why I like Java by brilee on Tuesday March 25, 2014 @02:03AM (#7464138)
Question: I see all this discussion about buffering and encoding and such.

In python, this seems to work: import sys sys.stdout.write(sys.stdin.read())

What is being abstracted away/handled by python that I'm not seeing?

Re:Why I like Java by sjy on Tuesday March 25, 2014 @02:27AM (#7464218)
.read() will read the entire contents of the file object into memory. That's fine if you're just doing `io.py < smallfile.txt`, but if the file is huge you'll run out of memory. And stdin is an infinite stream (eg. `io.py < /dev/urandom`), your Python code will never print anything. If you write `for line in sys.stdin: sys.stdout.write(line)` instead, you'll solve these problems by buffering line-by-line.
Re:Why I like Java by cbsmith on Tuesday March 25, 2014 @02:30AM (#7464231)
> If you write `for line in sys.stdin: sys.stdout.writte(line)` instead, you'll solve these problems by buffering line-by-line.

Except no. Then you run in to problems with a binary file that might not have a line break.

Re:Why I like Java by cbsmith on Tuesday March 25, 2014 @02:21AM (#7464202)
Nothing beyond an automatic flush/close of stdout on exit. The python code above is also reading the entire file in to memory before writing it out to disk, which isn't exactly wise. Aside from the addition of a flush at the end, the correct Java code for this is simply a loop with an exit test over a read() & write() though, which is really no different from the proper Python code.
Re:Why I like Java by cbsmith on Tuesday March 25, 2014 @02:32AM (#7464234)
There seems to be confusion on how to write this properly, so don't feel bad about missing this (or alternatively feel bad for all of humanity). I believe this Java code I posted elsewhere handles things just fine: https://gist.github.com/cbsmith/9755809