1 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
2 /*
3 Copyright (c) 2011 ymnk, JCraft,Inc. All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7 
8   1. Redistributions of source code must retain the above copyright notice,
9      this list of conditions and the following disclaimer.
10 
11   2. Redistributions in binary form must reproduce the above copyright
12      notice, this list of conditions and the following disclaimer in
13      the documentation and/or other materials provided with the distribution.
14 
15   3. The names of the authors may not be used to endorse or promote products
16      derived from this software without specific prior written permission.
17 
18 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
19 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
21 INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
22 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 package com.jcraft.jzlib;
31 import java.io.*;
32 
33 public class GZIPOutputStream extends DeflaterOutputStream {
34 
GZIPOutputStream(OutputStream out)35   public GZIPOutputStream(OutputStream out) throws IOException {
36     this(out, DEFAULT_BUFSIZE);
37   }
38 
GZIPOutputStream(OutputStream out, int size)39   public GZIPOutputStream(OutputStream out, int size) throws IOException {
40     this(out, size, true);
41   }
42 
GZIPOutputStream(OutputStream out, int size, boolean close_out)43   public GZIPOutputStream(OutputStream out,
44                           int size,
45                           boolean close_out) throws IOException {
46     this(out,
47          new Deflater(JZlib.Z_DEFAULT_COMPRESSION, 15+16),
48          size, close_out);
49     mydeflater=true;
50   }
51 
GZIPOutputStream(OutputStream out, Deflater deflater, int size, boolean close_out)52   public GZIPOutputStream(OutputStream out,
53                           Deflater deflater,
54                           int size,
55                           boolean close_out) throws IOException{
56     super(out, deflater, size, close_out);
57   }
58 
59 
check()60   private void check() throws GZIPException {
61     if(deflater.dstate.status != 42 /*INIT_STATUS*/)
62       throw new GZIPException("header is already written.");
63   }
64 
setModifiedTime(long mtime)65   public void setModifiedTime(long mtime) throws GZIPException {
66     check();
67     deflater.dstate.getGZIPHeader().setModifiedTime(mtime);
68   }
69 
setOS(int os)70   public void setOS(int os) throws GZIPException {
71     check();
72     deflater.dstate.getGZIPHeader().setOS(os);
73   }
74 
setName(String name)75   public void setName(String name) throws GZIPException {
76     check();
77     deflater.dstate.getGZIPHeader().setName(name);
78   }
79 
setComment(String comment)80   public void setComment(String comment) throws GZIPException {
81     check();
82     deflater.dstate.getGZIPHeader().setComment(comment);
83   }
84 
getCRC()85   public long getCRC() throws GZIPException {
86     if(deflater.dstate.status != 666 /*FINISH_STATE*/)
87       throw new GZIPException("checksum is not calculated yet.");
88     return deflater.dstate.getGZIPHeader().getCRC();
89   }
90 }
91