public static NSData gzipByteArrayAsNSData(byte[] input, int offset, int length) { try { ERXRefByteArrayOutputStream bos = new ERXRefByteArrayOutputStream(length); if (input != null) { GZIPOutputStream out = new GZIPOutputStream(bos); out.write(input, offset, length); out.finish(); out.close(); } return bos.toNSData(); } catch (IOException e) { log.error("Failed to gzip byte array.", e); return null; } }
/** * Returns an NSData containing the gzipped version of the given input stream. * * @param input the input stream to compress * @param length the length of the input stream * @return gzipped NSData */ public static NSData gzipInputStreamAsNSData(InputStream input, int length) { try { ERXRefByteArrayOutputStream bos = new ERXRefByteArrayOutputStream(length); if (input != null) { GZIPOutputStream out = new GZIPOutputStream(bos); try { ERXFileUtilities.writeInputStreamToOutputStream(input, true, out, false); } finally { try { out.finish(); } finally { out.close(); } } } return bos.toNSData(); } catch (IOException e) { log.error("Failed to gzip byte array.", e); return null; } }