package imj3.draft;

import static multij.tools.Tools.baseName;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;

import multij.tools.CommandLineArgumentsParser;
import multij.tools.IllegalInstantiationException;

/**
 * @author codistmonk (creation 2015-09-02)
 */
public final class Bin2LIBSVM {
	
	private Bin2LIBSVM() {
		throw new IllegalInstantiationException();
	}
	
	/**
	 * @param commandLineArguments
	 * <br>Must not be null
	 * @throws IOException 
	 */
	public static final void main(final String[] commandLineArguments) throws IOException {
		final CommandLineArgumentsParser arguments = new CommandLineArgumentsParser(commandLineArguments);
		final String binPath = arguments.get("bin", "");
		final int itemSize = arguments.get("itemSize", 2)[0];
		final String outPath = arguments.get("out", baseName(binPath) + ".txt");
		final byte[] item = new byte[itemSize];
		
		try (final InputStream input = new FileInputStream(binPath);
				final PrintStream output = new PrintStream(outPath)) {
			while (input.read(item) == itemSize) {
				output.print(0xFF & item[0]);
				
				for (int i = 1; i < itemSize; ++i) {
					final int value = 0xFF & item[i];
					
					if (value != 0) {
						output.print(" ");
						output.print(i);
						output.print(":");
						output.println(value);
					}
				}
			}
		}
	}
	
}

@@ -1112,28 +1112,84 @@
 ) %7B%0A%09%09%09%09
-output.print
+final StringBuilder line = new StringBuilder();%0A%09%09%09%09%0A%09%09%09%09line.append
 (0xFF & 
@@ -1322,77 +1322,80 @@
 %09%09%09%09
-output.print(%22 %22);%0A%09%09%09%09%09%09output.print(i);%0A%09%09%09%09%09%09output.print(%22:%22);%0A
+line.append(' ').append(i).append(':').append(value);%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D%0A%09%09
 %09%09
+%0A
 %09%09%09%09
@@ -1409,36 +1409,22 @@
 println(
-value);%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D
+line);
 %0A%09%09%09%7D%0A%09%09

Fix a defect.