TMV encoder rewritten in Java because cross-platform.