Code example for GZIPOutputStream

Methods: flushfinishwrite

0
		is = new BufferedInputStream(is, 32768);
		GZIPOutputStream gos = null;
		os = new BufferedOutputStream(os, 32768);
		CountedOutputStream cos = new CountedOutputStream(os);
		try { 
			gos = new GZIPOutputStream(cos);
			long read = 0;
			// Bigger input buffer, so can compress all at once. 
			// Won't hurt on I/O either, although most OSs will only return a page at a time. 
			byte[] buffer = new byte[32768];
			while(true) { 
				int l = (int) Math.min(buffer.length, maxReadLength - read);
				int x = l == 0 ? -1 : is.read(buffer, 0, l);
				if(x <= -1) break;
				if(x == 0) throw new IOException("Returned zero from read()");
				gos.write(buffer, 0, x);
				read += x;
				if(cos.written() > maxWriteLength)
					throw new CompressionOutputSizeException(); 
			} 
			gos.flush();
			gos.finish();
			cos.flush();
			gos = null;
			if(cos.written() > maxWriteLength)
				throw new CompressionOutputSizeException(); 
			return cos.written();
		} finally { 
			if(gos != null) {
				gos.flush();
				gos.finish();
			} 
		} 
	} 
 
	@Override 
Connect your IDE to all the code out there  Get Codota for Java