package nyc.angus.huffman.encode;

import java.util.BitSet;
import java.util.Map;

import org.apache.commons.lang3.tuple.Pair;

public class Encoder {

	static final int SIZE = Long.SIZE / 2;

	private final Map<Character, String> codes;

	public Encoder(final Map<Character, String> codes) {
		this.codes = codes;
	}

	public Pair<Integer, byte[]> encode(final String str) {

		final BitSet encoded = new BitSet();

		int pos = 0;

		for (int i = 0; i < str.length(); i++) {

			final String code = codes.get(str.charAt(i));

			for (final char c : code.toCharArray()) {
				encoded.set(pos++, c == '1');
			}
		}

		return Pair.of(pos, encoded.toByteArray());
	}
}

@@ -74,16 +74,102 @@
 til.Map;
+%0Aimport java.util.concurrent.atomic.AtomicInteger;%0Aimport java.util.stream.Collectors;
 %0A%0Aimport
@@ -455,17 +455,16 @@
  str) %7B%0A
-%0A
 %09%09final 
@@ -498,91 +498,86 @@
 ();%0A
-%0A
 %09%09
+f
 in
-t pos = 0;%0A%0A%09%09for (int i = 0; i %3C str.length(); i++) %7B%0A%0A%09%09%09final String code =
+al AtomicInteger pos = new AtomicInteger(0);%0A%0A%09%09str.chars().mapToObj(i -%3E
  cod
@@ -587,91 +587,117 @@
 get(
-str.
+(
 char
-At(i));%0A
+) i))//
 %0A%09%09%09
-for (final char c : code.toCharArray()) %7B%0A%09%09%09%09encoded.set(pos++
+%09.collect(Collectors.joining()).chars()//%0A%09%09%09%09.forEach(c -%3E encoded.set(pos.getAndIncrement()
 , c 
@@ -707,18 +707,10 @@
 '1')
+)
 ;
-%0A%09%09%09%7D%0A%09%09%7D
 %0A%0A%09%09
@@ -727,16 +727,22 @@
 r.of(pos
+.get()
 , encode

switch to stream implementation