GZIPOutputStream.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* GZIPOutputStream.java - Create a file in gzip format
  2. Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.util.zip;
  32. import java.io.IOException;
  33. import java.io.OutputStream;
  34. /**
  35. * This filter stream is used to compress a stream into a "GZIP" stream.
  36. * The "GZIP" format is described in RFC 1952.
  37. *
  38. * @author John Leuner
  39. * @author Tom Tromey
  40. * @since JDK 1.1
  41. */
  42. /* Written using on-line Java Platform 1.2 API Specification
  43. * and JCL book.
  44. * Believed complete and correct.
  45. */
  46. public class GZIPOutputStream extends DeflaterOutputStream
  47. {
  48. /**
  49. * CRC-32 value for uncompressed data
  50. */
  51. protected CRC32 crc;
  52. /**
  53. * Creates a GZIPOutputStream with the default buffer size
  54. *
  55. * @param out The stream to read data (to be compressed) from
  56. *
  57. */
  58. public GZIPOutputStream(OutputStream out) throws IOException
  59. {
  60. this(out, 4096);
  61. }
  62. /**
  63. * Creates a GZIPOutputStream with the specified buffer size
  64. *
  65. * @param out The stream to read compressed data from
  66. * @param size Size of the buffer to use
  67. */
  68. public GZIPOutputStream(OutputStream out, int size) throws IOException
  69. {
  70. super(out, new Deflater(Deflater.DEFAULT_COMPRESSION, true), size);
  71. crc = new CRC32();
  72. int mod_time = (int) (System.currentTimeMillis() / 1000L);
  73. byte[] gzipHeader =
  74. {
  75. /* The two magic bytes */
  76. (byte) GZIPInputStream.GZIP_MAGIC,
  77. (byte) (GZIPInputStream.GZIP_MAGIC >> 8),
  78. /* The compression type */
  79. (byte) Deflater.DEFLATED,
  80. /* The flags (not set) */
  81. 0,
  82. /* The modification time */
  83. (byte) mod_time, (byte) (mod_time >> 8),
  84. (byte) (mod_time >> 16), (byte) (mod_time >> 24),
  85. /* The extra flags */
  86. 0,
  87. /* The OS type (unknown) */
  88. (byte) 255
  89. };
  90. out.write(gzipHeader);
  91. // System.err.println("wrote GZIP header (" + gzipHeader.length + " bytes )");
  92. }
  93. public synchronized void write(byte[] buf, int off, int len)
  94. throws IOException
  95. {
  96. super.write(buf, off, len);
  97. crc.update(buf, off, len);
  98. }
  99. /**
  100. * Writes remaining compressed output data to the output stream
  101. * and closes it.
  102. */
  103. public void close() throws IOException
  104. {
  105. finish();
  106. out.close();
  107. }
  108. public void finish() throws IOException
  109. {
  110. super.finish();
  111. int totalin = def.getTotalIn();
  112. int crcval = (int) (crc.getValue() & 0xffffffff);
  113. // System.err.println("CRC val is " + Integer.toHexString( crcval ) + " and length " + Integer.toHexString(totalin));
  114. byte[] gzipFooter =
  115. {
  116. (byte) crcval, (byte) (crcval >> 8),
  117. (byte) (crcval >> 16), (byte) (crcval >> 24),
  118. (byte) totalin, (byte) (totalin >> 8),
  119. (byte) (totalin >> 16), (byte) (totalin >> 24)
  120. };
  121. out.write(gzipFooter);
  122. // System.err.println("wrote GZIP trailer (" + gzipFooter.length + " bytes )");
  123. }
  124. }