### Question:
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);  }### Answer:
@Test public void encodeWithNoRemainingBitsYieldsCorrectResult() { final ByteBuffer byteBuffer = ByteBuffer.allocate(6); byteBuffer .put((byte) 0x00) .put((byte) 0x00) .put((byte) 0x00) .put((byte) 0x00) .put((byte) 0x00) .put((byte) 0x01) .flip(); assertThat(encoder.encode(byteBuffer), is("AAAAAAAB")); }
@Test public void encodeShouldYieldValidBase62StringForPaperExample() { final ByteBuffer byteBuffer = ByteBuffer.allocate(3); byteBuffer .put((byte) 0x53) .put((byte) 0xFE) .put((byte) 0x92) .flip(); assertThat(encoder.encode(byteBuffer), is("U98kC")); }
@Test public void encodeWithSpecialCharacterAtEndPadsCorrectlyToNextCharacter() { final ByteBuffer byteBuffer = ByteBuffer.allocate(6); byteBuffer .put((byte) 0x07) .put((byte) 0x59) .put((byte) 0x06) .put((byte) 0x61) .put((byte) 0x47) .put((byte) 0xFE) .flip(); assertThat(encoder.encode(byteBuffer), is("B1kGYUf9A")); }
@Test public void encodeShouldYieldValidBase62CharacterSimpleSubstitution() { final ByteBuffer byteBuffer = ByteBuffer.allocate(1); byteBuffer .put((byte) 0x53) .flip(); assertThat(encoder.encode(byteBuffer), is("UD")); }
@Test public void encodeByteWithFiveLeadingSetOnesYieldsCorrectResult() { final ByteBuffer byteBuffer = ByteBuffer.allocate(1); byteBuffer .put((byte) 0xF9) .flip(); assertThat(encoder.encode(byteBuffer), is("9B")); final ByteBuffer byteBuffer2 = ByteBuffer.allocate(1); byteBuffer2 .put((byte) 0xF8) .flip(); assertThat(encoder.encode(byteBuffer2), is("9A")); }
@Test public void encodeByteWithFourLeadingOnesYieldsCorrectResult() { final ByteBuffer byteBuffer = ByteBuffer.allocate(1); byteBuffer .put((byte) 0xF0) .flip(); assertThat(encoder.encode(byteBuffer), is("8A")); }
@Test public void encodeByteWithZeroValuedRemainingBitsYieldsCorrectResult() { final ByteBuffer byteBuffer = ByteBuffer.allocate(1); byteBuffer .put((byte) 0xF8) .flip(); assertThat(encoder.encode(byteBuffer), is("9A")); }