Duplicate of c code to java. Thread closed.