@Override public byte[] fromString(final String str) { if (StringUtils.isEmpty(str) == true || XmlConstants.NULL_IDENTIFIER.equals(str) == true) { return null; } try { final byte[] bytes = (byte[]) Base64Helper.decodeObject(str); return bytes; } catch (final IOException ex) { log.error("Error while uncompressing string: " + ex.getMessage(), ex); return null; } catch (final ClassNotFoundException ex) { log.error("Error while uncompressing string: " + ex.getMessage(), ex); return null; } }
/** * @param str * @return Base64 encoded byte array. */ public static String compress(final String str) { if (str == null || str.length() == 0) { return str; } final ByteArrayOutputStream out = new ByteArrayOutputStream(); try { final GZIPOutputStream gzip = new GZIPOutputStream(out); gzip.write(str.getBytes()); gzip.close(); return Base64Helper.encodeObject(out.toByteArray()); } catch (final IOException ex) { log.error("Error while compressing string: " + ex.getMessage(), ex); return null; } }
/** * @param base64ByteArray * @return */ public static String uncompress(final String base64ByteArray) { if (base64ByteArray == null || base64ByteArray.length() == 0) { return base64ByteArray; } try { final byte[] byteArray = (byte[]) Base64Helper.decodeObject(base64ByteArray); final ByteArrayInputStream in = new ByteArrayInputStream(byteArray); final GZIPInputStream gzip = new GZIPInputStream(in); final ByteArrayOutputStream out = new ByteArrayOutputStream(); IOUtils.copy(gzip, out); gzip.close(); return out.toString(); } catch (final IOException ex) { log.error("Error while uncompressing string: " + ex.getMessage(), ex); return null; } catch (final ClassNotFoundException ex) { log.error("Error while uncompressing string: " + ex.getMessage(), ex); return null; } } }