by cs02rm0 · 12y ago · 0 comments
Doesn't matter. For this problem it is just a conveniently available InputStream. How often do you read from an InputStream? I'd hope the answer to that isn't 0.
> I also programmed in Java for nearly a decade in the late nineties till about 2004 and have never used System.in for anything.
Okay, then change the problem to implementing this interface:
public interface Copier {
public void copy(java.io.InputStream in, java.io.PrintStream out);
}
That doesn't change the problem.Your interface does not change the problem, but it does phrase the problem in a more familiar way and would have been much more easy for me to respond to (in my Java hey day that is).
Java didn't grow up in the world of unix where scripts that read from stdin and write to stdout get chained to produce pipelines. It grew up in the world of long-running stand-alone applications that communicate over sockets (like web applications). Java, because of the virtual machine, has a particularly slow start-up time and would be a poor choice for implementing mini pipeline components like this anyway.
import java.io.*;
public static void main(String[] args) {
byte[] buf = new byte[4096];
while (true) {
int numRead = System.in.read(buf);
if (numRead < 0) {
// hit EOF, terminate
System.out.flush();
System.exit(0);
}
System.out.write(buf,0,numRead);
}
} public class SimpleJavaTest
{
public interface RunnableEx
{
// can't use Callable<Void> because that run method needs to return a value
public abstract void run() throws Exception;
}
public static void main(String[] args)
{
// write standard in to standard out:
uncheck( () -> {
int c;
while( (c = System.in.read()) > -1)
{
System.out.write(c);
}
} ).run();
}
public static Runnable uncheck(RunnableEx r)
{
return () -> {
try
{
r.run();
}
catch(Exception e)
{
throw new RuntimeException(e.getMessage(), e);
}
};
}
}... And here's a fine example of Java culture.
2 lines do stuff, everything else is fluff, and it is considered prettier.
BTW: I did not run this specific code, but dropping the buffer is likely to make this code take much, much more CPU (unless HotSpot is much better these days than it was in 2010 when I last used it). That's another pillar of Java culture - care not about performance.
Disclaimer: I didn't test this, and any mention of performance requires testing, rather than reasoning. I don't have a Java compiler handy anymore, or I would test it.
Yeah, but you've probably done a read and write independently. This is just about joining the two together.
> There's a method in one of the apache commons libraries for that.
Yup, and that's great to use that, and you'll no doubt end up with more efficient code most of the time if you do. But... you still should be able to write correct code for reading bytes from one stream, writing to another, without losing data in between.
With access to Google and the library documentation sure, but I don't use those APIs often enough to be worth keeping in memory.
I guess I've been stuck in webapps and NoSQL libraries for a while now.