Ascii85 { public static byte[] decode(String chars) { if (chars == null || chars.length() == 0) { throw new IllegalArgumentException("You must provide a non-zero length input"); } BigDecimal decodedLength = BigDecimal.valueOf(chars.length()).multiply(BigDecimal.valueOf(4)) .divide(BigDecimal.valueOf(5)); ByteBuffer bytebuff = ByteBuffer.allocate(decodedLength.intValue()); chars = REMOVE_WHITESPACE.matcher(chars).replaceAll(""); byte[] payload = chars.getBytes(StandardCharsets.US_ASCII); byte[] chunk = new byte[5]; int chunkIndex = 0; for (int i = 0; i < payload.length; i++) { byte currByte = payload[i]; if (currByte == 'z') { if (chunkIndex > 0) { throw new IllegalArgumentException("The payload is not base 85 encoded."); } chunk[chunkIndex++] = '!'; chunk[chunkIndex++] = '!'; chunk[chunkIndex++] = '!'; chunk[chunkIndex++] = '!'; chunk[chunkIndex++] = '!'; } else { chunk[chunkIndex++] = currByte; } if (chunkIndex == 5) { bytebuff.put(decodeChunk(chunk)); Arrays.fill(chunk, (byte) 0); chunkIndex = 0; } } if (chunkIndex > 0) { int numPadded = chunk.length - chunkIndex; Arrays.fill(chunk, chunkIndex, chunk.length, (byte) 'u'); byte[] paddedDecode = decodeChunk(chunk); for (int i = 0; i < paddedDecode.length - numPadded; i++) { bytebuff.put(paddedDecode[i]); } } bytebuff.flip(); return Arrays.copyOf(bytebuff.array(), bytebuff.limit()); } private  Ascii85(); static String encode(byte[] payload); static byte[] decode(String chars);  }
@Test public void testDecode() { assertEquals("This is a test", new String(Ascii85.decode("<+oue+DGm>@3BZ'F*%"))); assertEquals("Some other test value", new String(Ascii85.decode(";f?Ma+E)@8ATAo8ATMr9G%#30AH"))); }