Java: how to abort a thread reading from System.in

Heinz Kabutz’s newsletter shows how to abort System.in reads: import java.io.*; import java.util.concurrent.*; class ConsoleInputReadTask implements Callable<String> { public String call() throws IOException { BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); System.out.println(“ConsoleInputReadTask run() called.”); String input; do { System.out.println(“Please type something: “); try { // wait until we have data to complete a readLine() while … Read more

StackOverflowError computing factorial of a BigInteger?

The problem here looks like its a stack overflow from too much recursion (5000 recursive calls looks like about the right number of calls to blow out a Java call stack) and not a limitation of BigInteger. Rewriting the factorial function iteratively should fix this. For example: public static BigInteger factorial(BigInteger n) { BigInteger result … Read more

How to transmit live video from within a Java application?

Honestly don’t waste your time with JMF, you can consider that offering dead. Here is how you would do screen shotting to an rtmp stream using h.264 (thanks to [email protected] for the example). If the code doesn’t show up here’s pastebin for it: http://pastebin.com/sJHwj0nW import com.xuggle.xuggler.Configuration; import com.xuggle.xuggler.ICodec; import com.xuggle.xuggler.IContainer; import com.xuggle.xuggler.IContainerFormat; import com.xuggle.xuggler.IPacket; import … Read more

how to read from standard input non-blocking?

Using BufferedReader.available() as suggested by Sibbo isn’t reliable. Documentation of available() states: Returns an estimate of the number of bytes that can be read… It is never correct to use the return value of this method to allocate a buffer. In other words, you cannot rely on this value, e.g., it can return 0 even … Read more