1 52 53 package com.go.trove.util; 54 55 64 public class Deflater { 65 public static final int 66 DEFLATED = 8, 67 NO_COMPRESSION = 0, 68 BEST_SPEED = 1, 69 BEST_COMPRESSION = 9, 70 DEFAULT_COMPRESSION = -1, 71 FILTERED = 1, 72 HUFFMAN_ONLY = 2, 73 DEFAULT_STRATEGY = 0; 74 75 private static final int 76 NO_FLUSH = 0, 77 SYNC_FLUSH = 2, 78 FULL_FLUSH = 3, 79 FINISH = 4; 80 81 static { 82 System.loadLibrary("TroveZip"); 83 initIDs(); 84 } 85 86 private long mStream; 88 private boolean mNoWrap; 89 90 private int mStrategy; 91 private int mLevel; 92 private boolean mSetParams; 93 94 private int mFlushOption = NO_FLUSH; 95 private boolean mFinished; 96 97 private byte[] mInputBuf; 98 private int mInputOffset; 99 private int mInputLength; 100 101 public Deflater(int level, boolean nowrap) { 102 mStream = init(DEFAULT_STRATEGY, level, nowrap); 103 mStrategy = DEFAULT_STRATEGY; 104 mLevel = level; 105 mNoWrap = nowrap; 106 } 107 108 public Deflater(int level) { 109 this(level, false); 110 } 111 112 public Deflater() { 113 this(DEFAULT_COMPRESSION, false); 114 } 115 116 public boolean isNoWrap() { 117 return mNoWrap; 118 } 119 120 public synchronized void setInput(byte[] b, int off, int len) { 121 boundsCheck(b, off, len); 122 mInputLength = len; 123 mInputOffset = off; 124 mInputBuf = b; 125 } 126 127 public synchronized void setInput(byte[] b) { 128 mInputLength = b.length; 129 mInputOffset = 0; 130 mInputBuf = b; 131 } 132 133 public synchronized void setDictionary(byte[] b, int off, int len) { 134 boundsCheck(b, off, len); 135 setDictionary(mStream, b, off, len); 136 } 137 138 public synchronized void setDictionary(byte[] b) { 139 setDictionary(mStream, b, 0, b.length); 140 } 141 142 public synchronized void setStrategy(int strategy) { 143 mStrategy = strategy; 144 mSetParams = true; 145 } 146 147 public synchronized void setLevel(int level) { 148 mLevel = level; 149 mSetParams = true; 150 } 151 152 public boolean needsInput() { 153 return mInputLength <= 0; 154 } 155 156 160 public void flush() { 161 mFlushOption = SYNC_FLUSH; 162 } 163 164 169 public void fullFlush() { 170 mFlushOption = FULL_FLUSH; 171 } 172 173 178 public void finish() { 179 mFlushOption = FINISH; 180 } 181 182 public synchronized boolean finished() { 183 return mFinished; 184 } 185 186 public int deflate(byte[] b, int off, int len) { 187 boundsCheck(b, off, len); 188 return deflate0(b, off, len); 189 } 190 191 public int deflate(byte[] b) { 192 return deflate0(b, 0, b.length); 193 } 194 195 private synchronized int deflate0(byte[] b, int off, int len) { 196 int amt = deflate(mStream, mFlushOption, mSetParams, 197 mInputBuf, mInputOffset, mInputLength, 198 b, off, len); 199 if (amt < len) { 200 if (mFlushOption == SYNC_FLUSH || mFlushOption == FULL_FLUSH) { 201 mFlushOption = NO_FLUSH; 202 } 203 } 204 return amt; 205 } 206 207 public synchronized int getAdler() { 208 return getAdler(mStream); 209 } 210 211 public synchronized int getTotalIn() { 212 return getTotalIn(mStream); 213 } 214 215 public synchronized int getTotalOut() { 216 return getTotalOut(mStream); 217 } 218 219 private int mResetCount; 220 221 public synchronized void reset() { 222 mFinished = false; 223 mFlushOption = NO_FLUSH; 224 mInputBuf = null; 225 mInputLength = 0; 226 reset(mStream); 227 } 228 229 public synchronized void end() { 230 end(mStream); 231 } 232 233 protected void finalize() { 234 end(); 235 } 236 237 private void boundsCheck(byte[] b, int off, int len) { 238 if (off < 0 || len < 0 || off + len > b.length) { 239 throw new ArrayIndexOutOfBoundsException (); 240 } 241 } 242 243 private static native void initIDs(); 244 245 private native long init(int strategy, int level, boolean nowrap); 246 247 private native void setDictionary(long strm, byte[] b, int off, int len); 248 249 private native int deflate(long strm, int flushOpt, boolean setParams, 250 byte[] inBuf, int inOff, int inLen, 251 byte[] outBuf, int outOff, int outLen); 252 253 private native int getAdler(long strm); 254 private native int getTotalIn(long strm); 255 private native int getTotalOut(long strm); 256 private native void reset(long strm); 257 private native void end(long strm); 258 } 259 | Popular Tags |