1 /** 2 * Authors: kntroh <knt.roh@gmail.com> 3 */ 4 module java.util.zip.DeflaterOutputStream; 5 6 static import java.io.OutputStream; 7 8 version (Tango) { 9 version (Windows) { 10 pragma (lib, "zlib.lib"); 11 } else { 12 pragma (lib, "zlib"); 13 } 14 15 import tango.io.stream.Zlib; 16 import tango.io.model.IConduit; 17 18 class OutputStreamWrapper : tango.io.model.IConduit.OutputStream { 19 20 java.io.OutputStream.OutputStream _ostr; 21 22 this (java.io.OutputStream.OutputStream ostr) { 23 _ostr = ostr; 24 } 25 26 override 27 size_t write (void[] src) { 28 _ostr.write(cast(byte[]) src); 29 return src.length; 30 } 31 override 32 IOStream flush() { 33 _ostr.flush(); 34 return this; 35 } 36 override 37 void close() { 38 _ostr.close(); 39 } 40 41 override 42 long seek(long offset, Anchor anchor = Anchor.Begin) { 43 implMissing(__FILE__,__LINE__); 44 return 0; 45 } 46 override 47 IConduit conduit() { 48 implMissing(__FILE__,__LINE__); 49 return null; 50 } 51 override 52 OutputStream copy(tango.io.model.IConduit.InputStream src, size_t max = -1) { 53 implMissing(__FILE__,__LINE__); 54 return null; 55 } 56 override 57 OutputStream output() { 58 implMissing(__FILE__,__LINE__); 59 return null; 60 } 61 } 62 } else { // Phobos 63 import std.zlib; 64 } 65 66 import java.lang.all; 67 68 class DeflaterOutputStream : java.io.OutputStream.OutputStream { 69 70 version (Tango) { 71 private ZlibOutput _ostr; 72 } else { // Phobos 73 private java.io.OutputStream.OutputStream _ostr; 74 private Compress _compress; 75 } 76 77 protected byte[] buf; 78 79 this (java.io.OutputStream.OutputStream ostr) { 80 version (Tango) { 81 _ostr = new ZlibOutput(new OutputStreamWrapper(ostr)); 82 } else { // Phobos 83 _ostr = ostr; 84 _compress = new Compress(); 85 } 86 } 87 88 protected void deflate() { 89 version (Tango) { 90 _ostr.write(buf); 91 } else { // Phobos 92 auto bytes = _compress.compress(buf); 93 if (0 < bytes.length) { 94 _ostr.write(cast(byte[]) bytes); 95 } 96 } 97 buf.length = 0; 98 } 99 100 void finish() { 101 version (Tango) { 102 _ostr.commit(); 103 } else { // Phobos 104 auto bytes = _compress.flush(Z_FINISH); 105 if (0 < bytes.length) { 106 _ostr.write(cast(byte[]) bytes); 107 _ostr.flush(); 108 } 109 } 110 } 111 112 override 113 public void write(int b) { 114 byte[1] bytes; 115 bytes[0] = cast(byte) (b & 0xFF); 116 write(bytes, 0, bytes.length); 117 } 118 119 override 120 public void write(in byte[] b) { 121 write(b, 0, cast(int)b.length); 122 } 123 124 override 125 void write(in byte[] b, ptrdiff_t off, ptrdiff_t len) { 126 buf ~= b[off .. off + len]; 127 deflate(); 128 } 129 130 override 131 void flush() { 132 deflate(); 133 _ostr.flush(); 134 } 135 136 override 137 void close() { 138 finish(); 139 _ostr.close(); 140 } 141 }