@Test public void encodeByteWithZeroValuedRemainingBitsYieldsCorrectResult() { final ByteBuffer byteBuffer = ByteBuffer.allocate(1); byteBuffer .put((byte) 0xF8) .flip(); assertThat(encoder.encode(byteBuffer), is("9A")); }
@Override public String encode(final ByteBuffer unencodedData) { final int maxBytes = unencodedData.limit(); final StringBuilder sb = new StringBuilder(); int bitSource = nextByte(unencodedData); int unencodedBits = bitSource >> 2; int bitIndex = 1; int bitsRead = 6; boolean carryOverLsb = false; while (!bitsExhausted(bitsRead, maxBytes)) { carryOverLsb = appendBitsAsBase62Character(unencodedBits, sb); int bitsForAlignment; if (carryOverLsb) { unencodedBits &= BIT_MASK_LSB; bitsForAlignment = 5; } else { unencodedBits = 0; bitsForAlignment = 6; } for (int i = 0; i < bitsForAlignment; i++) { unencodedBits = appendBit(bitSource, unencodedBits, bitIndex); bitsRead++; bitIndex--; if (bitIndex == -1) { if (bitsExhausted(bitsRead, maxBytes)) { break; } bitSource = nextByte(unencodedData); bitIndex = 7; } } } while (bitIndex >= 0) { unencodedBits = appendBit(bitSource, unencodedBits, bitIndex); bitIndex--; } carryOverLsb = appendBitsAsBase62Character(unencodedBits, sb); if (carryOverLsb) { unencodedBits &= BIT_MASK_LSB; appendBitsAsBase62Character(unencodedBits, sb); } return sb.toString(); }
Base62Encoder implements Encoder<ByteBuffer, String> { @Override public String encode(final ByteBuffer unencodedData) { final int maxBytes = unencodedData.limit(); final StringBuilder sb = new StringBuilder(); int bitSource = nextByte(unencodedData); int unencodedBits = bitSource >> 2; int bitIndex = 1; int bitsRead = 6; boolean carryOverLsb = false; while (!bitsExhausted(bitsRead, maxBytes)) { carryOverLsb = appendBitsAsBase62Character(unencodedBits, sb); int bitsForAlignment; if (carryOverLsb) { unencodedBits &= BIT_MASK_LSB; bitsForAlignment = 5; } else { unencodedBits = 0; bitsForAlignment = 6; } for (int i = 0; i < bitsForAlignment; i++) { unencodedBits = appendBit(bitSource, unencodedBits, bitIndex); bitsRead++; bitIndex--; if (bitIndex == -1) { if (bitsExhausted(bitsRead, maxBytes)) { break; } bitSource = nextByte(unencodedData); bitIndex = 7; } } } while (bitIndex >= 0) { unencodedBits = appendBit(bitSource, unencodedBits, bitIndex); bitIndex--; } carryOverLsb = appendBitsAsBase62Character(unencodedBits, sb); if (carryOverLsb) { unencodedBits &= BIT_MASK_LSB; appendBitsAsBase62Character(unencodedBits, sb); } return sb.toString(); } }
Base62Encoder implements Encoder<ByteBuffer, String> { @Override public String encode(final ByteBuffer unencodedData) { final int maxBytes = unencodedData.limit(); final StringBuilder sb = new StringBuilder(); int bitSource = nextByte(unencodedData); int unencodedBits = bitSource >> 2; int bitIndex = 1; int bitsRead = 6; boolean carryOverLsb = false; while (!bitsExhausted(bitsRead, maxBytes)) { carryOverLsb = appendBitsAsBase62Character(unencodedBits, sb); int bitsForAlignment; if (carryOverLsb) { unencodedBits &= BIT_MASK_LSB; bitsForAlignment = 5; } else { unencodedBits = 0; bitsForAlignment = 6; } for (int i = 0; i < bitsForAlignment; i++) { unencodedBits = appendBit(bitSource, unencodedBits, bitIndex); bitsRead++; bitIndex--; if (bitIndex == -1) { if (bitsExhausted(bitsRead, maxBytes)) { break; } bitSource = nextByte(unencodedData); bitIndex = 7; } } } while (bitIndex >= 0) { unencodedBits = appendBit(bitSource, unencodedBits, bitIndex); bitIndex--; } carryOverLsb = appendBitsAsBase62Character(unencodedBits, sb); if (carryOverLsb) { unencodedBits &= BIT_MASK_LSB; appendBitsAsBase62Character(unencodedBits, sb); } return sb.toString(); }  }
Base62Encoder implements Encoder<ByteBuffer, String> { @Override public String encode(final ByteBuffer unencodedData) { final int maxBytes = unencodedData.limit(); final StringBuilder sb = new StringBuilder(); int bitSource = nextByte(unencodedData); int unencodedBits = bitSource >> 2; int bitIndex = 1; int bitsRead = 6; boolean carryOverLsb = false; while (!bitsExhausted(bitsRead, maxBytes)) { carryOverLsb = appendBitsAsBase62Character(unencodedBits, sb); int bitsForAlignment; if (carryOverLsb) { unencodedBits &= BIT_MASK_LSB; bitsForAlignment = 5; } else { unencodedBits = 0; bitsForAlignment = 6; } for (int i = 0; i < bitsForAlignment; i++) { unencodedBits = appendBit(bitSource, unencodedBits, bitIndex); bitsRead++; bitIndex--; if (bitIndex == -1) { if (bitsExhausted(bitsRead, maxBytes)) { break; } bitSource = nextByte(unencodedData); bitIndex = 7; } } } while (bitIndex >= 0) { unencodedBits = appendBit(bitSource, unencodedBits, bitIndex); bitIndex--; } carryOverLsb = appendBitsAsBase62Character(unencodedBits, sb); if (carryOverLsb) { unencodedBits &= BIT_MASK_LSB; appendBitsAsBase62Character(unencodedBits, sb); } return sb.toString(); }  @Override String encode(final ByteBuffer unencodedData); }
Base62Encoder implements Encoder<ByteBuffer, String> { @Override public String encode(final ByteBuffer unencodedData) { final int maxBytes = unencodedData.limit(); final StringBuilder sb = new StringBuilder(); int bitSource = nextByte(unencodedData); int unencodedBits = bitSource >> 2; int bitIndex = 1; int bitsRead = 6; boolean carryOverLsb = false; while (!bitsExhausted(bitsRead, maxBytes)) { carryOverLsb = appendBitsAsBase62Character(unencodedBits, sb); int bitsForAlignment; if (carryOverLsb) { unencodedBits &= BIT_MASK_LSB; bitsForAlignment = 5; } else { unencodedBits = 0; bitsForAlignment = 6; } for (int i = 0; i < bitsForAlignment; i++) { unencodedBits = appendBit(bitSource, unencodedBits, bitIndex); bitsRead++; bitIndex--; if (bitIndex == -1) { if (bitsExhausted(bitsRead, maxBytes)) { break; } bitSource = nextByte(unencodedData); bitIndex = 7; } } } while (bitIndex >= 0) { unencodedBits = appendBit(bitSource, unencodedBits, bitIndex); bitIndex--; } carryOverLsb = appendBitsAsBase62Character(unencodedBits, sb); if (carryOverLsb) { unencodedBits &= BIT_MASK_LSB; appendBitsAsBase62Character(unencodedBits, sb); } return sb.toString(); }  @Override String encode(final ByteBuffer unencodedData);  }