Compress.hx 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright (C)2005-2012 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package haxe.zip;
  23. import java.util.zip.Deflater;
  24. class Compress
  25. {
  26. var deflater:Deflater;
  27. var mode:Int;
  28. var finish:Bool = false;
  29. public function new( level : Int )
  30. {
  31. throw "Not implemented for this platform"; //FIXME: Add unit tests for Compress/Uncompress and check current implementation
  32. this.deflater = new Deflater(level);
  33. this.mode = Deflater.NO_FLUSH;
  34. }
  35. public function execute( src : haxe.io.Bytes, srcPos : Int, dst : haxe.io.Bytes, dstPos : Int ) : { done : Bool, read : Int, write : Int }
  36. {
  37. deflater.setInput(src.getData(), srcPos, src.length - srcPos);
  38. if (finish)
  39. deflater.finish();
  40. finish = false;
  41. var written = deflater.deflate(dst.getData(), dstPos, dst.length - dstPos);
  42. var read = deflater.getTotalIn();
  43. return { done: deflater.finished(), read: read, write: written };
  44. }
  45. public function setFlushMode( f : FlushMode )
  46. {
  47. this.mode = switch (f) {
  48. case NO:
  49. Deflater.NO_FLUSH;
  50. case SYNC:
  51. Deflater.SYNC_FLUSH;
  52. case FULL:
  53. Deflater.FULL_FLUSH;
  54. case FINISH:
  55. this.finish = true;
  56. Deflater.FULL_FLUSH;
  57. case BLOCK:
  58. throw "Not Implemented";
  59. }
  60. }
  61. public function close()
  62. {
  63. deflater.end();
  64. }
  65. public static function run( s : haxe.io.Bytes, level : Int ) : haxe.io.Bytes
  66. {
  67. var deflater = new java.util.zip.Deflater(level);
  68. deflater.setInput(s.getData());
  69. var outputStream = new java.io.ByteArrayOutputStream(s.length);
  70. deflater.finish();
  71. var buffer = haxe.io.Bytes.alloc(1024).getData();
  72. while (!deflater.finished()) {
  73. var count = deflater.deflate(buffer);
  74. outputStream.write(buffer, 0, count);
  75. }
  76. outputStream.close();
  77. return haxe.io.Bytes.ofData(outputStream.toByteArray());
  78. }
  79. }