public static byte[] decodeDecompressBinary(@Nonnull final String content) throws IOException { final boolean useCompression = content.charAt(0) == 'Z'; final byte[] bytes = Base43.decode(content.substring(1)); InputStream is = new ByteArrayInputStream(bytes); if (useCompression) is = new GZIPInputStream(is); final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final byte[] buf = new byte[4096]; int read; while (-1 != (read = is.read(buf))) baos.write(buf, 0, read); baos.close(); is.close(); return baos.toByteArray(); }
public static String encodeCompressBinary(@Nonnull final byte[] bytes) { try { final ByteArrayOutputStream bos = new ByteArrayOutputStream(bytes.length); final GZIPOutputStream gos = new GZIPOutputStream(bos); gos.write(bytes); gos.close(); final byte[] gzippedBytes = bos.toByteArray(); final boolean useCompressioon = gzippedBytes.length < bytes.length; final StringBuilder str = new StringBuilder(); str.append(useCompressioon ? 'Z' : '-'); str.append(Base43.encode(useCompressioon ? gzippedBytes : bytes)); return str.toString(); } catch (final IOException x) { throw new RuntimeException(x); } }
public static String encodeBinary(@Nonnull final byte[] bytes) { return Base43.encode(bytes); }
public static byte[] decodeBinary(@Nonnull final String content) throws IOException { return Base43.decode(content); }