final public long write(final BitOutputStream bitOutputStream, final byte value) throws IOException { final HuffmanBitCode code = valueToCode[value]; if (code.value != value) throw new RuntimeException(String.format( "Searching for %d but found %s.", value, code.toString())); bitOutputStream.write(code.bitCode, code.bitLength); // System.out.println("Writing: " + code.toString()); return code.bitLength; }
private void buildCodes() { codes = new TreeMap<Integer, HuffmanBitCode>(); int codeLength = 0, codeValue = -1; for (final Object key : codeBook.keySet()) { // Iterate over code lengths @SuppressWarnings("SuspiciousMethodCalls") final SortedSet<Integer> get = codeBook.get(key); final int intKey = Integer.parseInt(key.toString()); for (final Integer entry : get) { // Iterate over symbols final HuffmanBitCode code = new HuffmanBitCode(); code.bitLength = intKey; // given: bit length code.value = entry; // given: symbol codeValue++; // increment bit value by 1 final int delta = intKey - codeLength; // new length? codeValue = codeValue << delta; // pad with 0's code.bitCode = codeValue; // calculated: huffman code codeLength += delta; // adjust current code length if (NumberOfSetBits(codeValue) > intKey) throw new IllegalArgumentException("Symbol out of range"); codes.put(entry, code); // Store HuffmanBitCode } } }
private void buildCodes() { codes = new TreeMap<Integer, HuffmanBitCode>(); int codeLength = 0, codeValue = -1; for (final Object key : codeBook.keySet()) { // Iterate over code lengths @SuppressWarnings("SuspiciousMethodCalls") final SortedSet<Integer> get = codeBook.get(key); final int intKey = Integer.parseInt(key.toString()); for (final Integer entry : get) { // Iterate over symbols final HuffmanBitCode code = new HuffmanBitCode(); code.bitLength = intKey; // given: bit length code.value = entry; // given: symbol codeValue++; // increment bit value by 1 final int delta = intKey - codeLength; // new length? codeValue = codeValue << delta; // pad with 0's code.bitCode = codeValue; // calculated: huffman code codeLength += delta; // adjust current code length if (NumberOfSetBits(codeValue) > intKey) throw new IllegalArgumentException("Symbol out of range"); codes.put(entry, code); // Store HuffmanBitCode } } }
public final long write(final BitOutputStream bitOutputStream, final int value) throws IOException { final int index = Arrays.binarySearch(sortedValues, value); final HuffmanBitCode code = sortedByValue[index]; if (code.value != value) throw new RuntimeException(String.format( "Searching for %d but found %s.", value, code.toString())); bitOutputStream.write(code.bitCode, code.bitLength); return code.bitLength; }
private void buildCodes() { codes = new TreeMap<Integer, HuffmanBitCode>(); int codeLength = 0, codeValue = -1; for (final Object key : codeBook.keySet()) { // Iterate over code lengths @SuppressWarnings("SuspiciousMethodCalls") final SortedSet<Integer> get = codeBook.get(key); final int intKey = Integer.parseInt(key.toString()); for (final Integer entry : get) { // Iterate over symbols final HuffmanBitCode code = new HuffmanBitCode(); code.bitLength = intKey; // given: bit length code.value = entry; // given: symbol codeValue++; // increment bit value by 1 final int delta = intKey - codeLength; // new length? codeValue = codeValue << delta; // pad with 0's code.bitCode = codeValue; // calculated: huffman code codeLength += delta; // adjust current code length if (NumberOfSetBits(codeValue) > intKey) throw new IllegalArgumentException("Symbol out of range"); codes.put(entry, code); // Store HuffmanBitCode } } }
public final long write(final BitOutputStream bitOutputStream, final int value) { final int index = Arrays.binarySearch(sortedValues, value); final HuffmanBitCode code = sortedByValue[index]; if (code.value != value) throw new RuntimeException(String.format( "Searching for %d but found %s.", value, code.toString())); bitOutputStream.write(code.bitCode, code.bitLength); return code.bitLength; }
private void buildCodes() { codes = new TreeMap<>(); int codeLength = 0, codeValue = -1; for (final Object key : codeBook.keySet()) { // Iterate over code lengths @SuppressWarnings("SuspiciousMethodCalls") final SortedSet<Integer> get = codeBook.get(key); final int intKey = Integer.parseInt(key.toString()); for (final Integer entry : get) { // Iterate over symbols final HuffmanBitCode code = new HuffmanBitCode(); code.bitLength = intKey; // given: bit length code.value = entry; // given: symbol codeValue++; // increment bit value by 1 final int delta = intKey - codeLength; // new length? codeValue = codeValue << delta; // pad with 0's code.bitCode = codeValue; // calculated: huffman code codeLength += delta; // adjust current code length if (NumberOfSetBits(codeValue) > intKey) throw new IllegalArgumentException("Symbol out of range"); codes.put(entry, code); // Store HuffmanBitCode } } }
final public long write(final BitOutputStream bitOutputStream, final byte value) { final HuffmanBitCode code = valueToCode[value]; if (code.value != value) throw new RuntimeException(String.format( "Searching for %d but found %s.", value, code.toString())); bitOutputStream.write(code.bitCode, code.bitLength); // System.out.println("Writing: " + code.toString()); return code.bitLength; }