170 */
171 public static final int FULL_FLUSH = 3;
172
173 static {
174 ZipUtils.loadLibrary();
175 initIDs();
176 }
177
178 /**
179 * Creates a new compressor using the specified compression level.
180 * If 'nowrap' is true then the ZLIB header and checksum fields will
181 * not be used in order to support the compression format used in
182 * both GZIP and PKZIP.
183 * @param level the compression level (0-9)
184 * @param nowrap if true then use GZIP compatible compression
185 */
186 public Deflater(int level, boolean nowrap) {
187 this.level = level;
188 this.strategy = DEFAULT_STRATEGY;
189 this.zsRef = DeflaterZStreamRef.get(this,
190 init(level, DEFAULT_STRATEGY, nowrap));
191 }
192
193 /**
194 * Creates a new compressor using the specified compression level.
195 * Compressed data will be generated in ZLIB format.
196 * @param level the compression level (0-9)
197 */
198 public Deflater(int level) {
199 this(level, false);
200 }
201
202 /**
203 * Creates a new compressor with the default compression level.
204 * Compressed data will be generated in ZLIB format.
205 */
206 public Deflater() {
207 this(DEFAULT_COMPRESSION, false);
208 }
209
210 /**
587
588 private static native void initIDs();
589 private static native long init(int level, int strategy, boolean nowrap);
590 private static native void setDictionary(long addr, byte[] b, int off, int len);
591 private native int deflateBytes(long addr, byte[] b, int off, int len,
592 int flush);
593 private static native int getAdler(long addr);
594 private static native void reset(long addr);
595 private static native void end(long addr);
596
597 /**
598 * A reference to the native zlib's z_stream structure. It also
599 * serves as the "cleaner" to clean up the native resource when
600 * the Deflater is ended, closed or cleaned.
601 */
602 static class DeflaterZStreamRef implements Runnable {
603
604 private long address;
605 private final Cleanable cleanable;
606
607 private DeflaterZStreamRef(Deflater owner, long addr) {
608 this.cleanable = (owner != null) ? CleanerFactory.cleaner().register(owner, this) : null;
609 this.address = addr;
610 }
611
612 long address() {
613 return address;
614 }
615
616 void clean() {
617 cleanable.clean();
618 }
619
620 public synchronized void run() {
621 long addr = address;
622 address = 0;
623 if (addr != 0) {
624 end(addr);
625 }
626 }
627
628 /*
629 * If {@code Deflater} has been subclassed and the {@code end} method is
630 * overridden, uses {@code finalizer} mechanism for resource cleanup. So
631 * {@code end} method can be called when the {@code Deflater} is unreachable.
632 * This mechanism will be removed when the {@code finalize} method is
633 * removed from {@code Deflater}.
634 */
635 static DeflaterZStreamRef get(Deflater owner, long addr) {
636 Class<?> clz = owner.getClass();
637 while (clz != Deflater.class) {
638 try {
639 clz.getDeclaredMethod("end");
640 return new FinalizableZStreamRef(owner, addr);
641 } catch (NoSuchMethodException nsme) {}
642 clz = clz.getSuperclass();
643 }
644 return new DeflaterZStreamRef(owner, addr);
645 }
646
647 private static class FinalizableZStreamRef extends DeflaterZStreamRef {
648 final Deflater owner;
649
650 FinalizableZStreamRef (Deflater owner, long addr) {
651 super(null, addr);
652 this.owner = owner;
653 }
654
655 @Override
656 void clean() {
657 run();
658 }
659
660 @Override
661 @SuppressWarnings("deprecation")
662 protected void finalize() {
663 owner.end();
664 }
665 }
666 }
667 }
|
170 */
171 public static final int FULL_FLUSH = 3;
172
173 static {
174 ZipUtils.loadLibrary();
175 initIDs();
176 }
177
178 /**
179 * Creates a new compressor using the specified compression level.
180 * If 'nowrap' is true then the ZLIB header and checksum fields will
181 * not be used in order to support the compression format used in
182 * both GZIP and PKZIP.
183 * @param level the compression level (0-9)
184 * @param nowrap if true then use GZIP compatible compression
185 */
186 public Deflater(int level, boolean nowrap) {
187 this.level = level;
188 this.strategy = DEFAULT_STRATEGY;
189 this.zsRef = DeflaterZStreamRef.get(this,
190 level, DEFAULT_STRATEGY, nowrap);
191 }
192
193 /**
194 * Creates a new compressor using the specified compression level.
195 * Compressed data will be generated in ZLIB format.
196 * @param level the compression level (0-9)
197 */
198 public Deflater(int level) {
199 this(level, false);
200 }
201
202 /**
203 * Creates a new compressor with the default compression level.
204 * Compressed data will be generated in ZLIB format.
205 */
206 public Deflater() {
207 this(DEFAULT_COMPRESSION, false);
208 }
209
210 /**
587
588 private static native void initIDs();
589 private static native long init(int level, int strategy, boolean nowrap);
590 private static native void setDictionary(long addr, byte[] b, int off, int len);
591 private native int deflateBytes(long addr, byte[] b, int off, int len,
592 int flush);
593 private static native int getAdler(long addr);
594 private static native void reset(long addr);
595 private static native void end(long addr);
596
597 /**
598 * A reference to the native zlib's z_stream structure. It also
599 * serves as the "cleaner" to clean up the native resource when
600 * the Deflater is ended, closed or cleaned.
601 */
602 static class DeflaterZStreamRef implements Runnable {
603
604 private long address;
605 private final Cleanable cleanable;
606
607 private DeflaterZStreamRef(Deflater owner, int level, int strategy, boolean nowrap) {
608 this.cleanable = (owner != null) ? CleanerFactory.cleaner().register(owner, this) : null;
609 this.address = init(level, strategy, nowrap);
610 }
611
612 long address() {
613 return address;
614 }
615
616 void clean() {
617 cleanable.clean();
618 }
619
620 public synchronized void run() {
621 long addr = address;
622 address = 0;
623 if (addr != 0) {
624 end(addr);
625 }
626 }
627
628 /*
629 * If {@code Deflater} has been subclassed and the {@code end} method is
630 * overridden, uses {@code finalizer} mechanism for resource cleanup. So
631 * {@code end} method can be called when the {@code Deflater} is unreachable.
632 * This mechanism will be removed when the {@code finalize} method is
633 * removed from {@code Deflater}.
634 */
635 static DeflaterZStreamRef get(Deflater owner, int level, int strategy, boolean nowrap) {
636 Class<?> clz = owner.getClass();
637 while (clz != Deflater.class) {
638 try {
639 clz.getDeclaredMethod("end");
640 return new FinalizableZStreamRef(owner, level, strategy, nowrap);
641 } catch (NoSuchMethodException nsme) {}
642 clz = clz.getSuperclass();
643 }
644 return new DeflaterZStreamRef(owner, level, strategy, nowrap);
645 }
646
647 private static class FinalizableZStreamRef extends DeflaterZStreamRef {
648 final Deflater owner;
649
650 FinalizableZStreamRef (Deflater owner, int level, int strategy, boolean nowrap) {
651 super(null, level, strategy, nowrap);
652 this.owner = owner;
653 }
654
655 @Override
656 void clean() {
657 run();
658 }
659
660 @Override
661 @SuppressWarnings("deprecation")
662 protected void finalize() {
663 owner.end();
664 }
665 }
666 }
667 }
|