jdk/src/share/classes/java/util/zip/GZIPOutputStream.java
changeset 5631 cfb4f43485f3
parent 5627 e636ac7a63a4
child 18156 edb590d448c5
equal deleted inserted replaced
5577:71a9057b19b6 5631:cfb4f43485f3
     1 /*
     1 /*
     2  * Copyright (c) 1996, 2002, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    52      */
    52      */
    53     private final static int TRAILER_SIZE = 8;
    53     private final static int TRAILER_SIZE = 8;
    54 
    54 
    55     /**
    55     /**
    56      * Creates a new output stream with the specified buffer size.
    56      * Creates a new output stream with the specified buffer size.
       
    57      *
       
    58      * <p>The new output stream instance is created as if by invoking
       
    59      * the 3-argument constructor GZIPOutputStream(out, size, false).
       
    60      *
    57      * @param out the output stream
    61      * @param out the output stream
    58      * @param size the output buffer size
    62      * @param size the output buffer size
    59      * @exception IOException If an I/O error has occurred.
    63      * @exception IOException If an I/O error has occurred.
    60      * @exception IllegalArgumentException if size is <= 0
    64      * @exception IllegalArgumentException if size is <= 0
       
    65 
    61      */
    66      */
    62     public GZIPOutputStream(OutputStream out, int size) throws IOException {
    67     public GZIPOutputStream(OutputStream out, int size) throws IOException {
    63         super(out, new Deflater(Deflater.DEFAULT_COMPRESSION, true), size);
    68         this(out, size, false);
       
    69     }
       
    70 
       
    71     /**
       
    72      * Creates a new output stream with the specified buffer size and
       
    73      * flush mode.
       
    74      *
       
    75      * @param out the output stream
       
    76      * @param size the output buffer size
       
    77      * @param syncFlush
       
    78      *        if {@code true} invocation of the inherited
       
    79      *        {@link DeflaterOutputStream#flush() flush()} method of
       
    80      *        this instance flushes the compressor with flush mode
       
    81      *        {@link Deflater#SYNC_FLUSH} before flushing the output
       
    82      *        stream, otherwise only flushes the output stream
       
    83      * @exception IOException If an I/O error has occurred.
       
    84      * @exception IllegalArgumentException if size is <= 0
       
    85      *
       
    86      * @since 1.7
       
    87      */
       
    88     public GZIPOutputStream(OutputStream out, int size, boolean syncFlush)
       
    89         throws IOException
       
    90     {
       
    91         super(out, new Deflater(Deflater.DEFAULT_COMPRESSION, true),
       
    92               size,
       
    93               syncFlush);
    64         usesDefaultDeflater = true;
    94         usesDefaultDeflater = true;
    65         writeHeader();
    95         writeHeader();
    66         crc.reset();
    96         crc.reset();
    67     }
    97     }
    68 
    98 
       
    99 
    69     /**
   100     /**
    70      * Creates a new output stream with a default buffer size.
   101      * Creates a new output stream with a default buffer size.
       
   102      *
       
   103      * <p>The new output stream instance is created as if by invoking
       
   104      * the 2-argument constructor GZIPOutputStream(out, false).
       
   105      *
    71      * @param out the output stream
   106      * @param out the output stream
    72      * @exception IOException If an I/O error has occurred.
   107      * @exception IOException If an I/O error has occurred.
    73      */
   108      */
    74     public GZIPOutputStream(OutputStream out) throws IOException {
   109     public GZIPOutputStream(OutputStream out) throws IOException {
    75         this(out, 512);
   110         this(out, 512, false);
       
   111     }
       
   112 
       
   113     /**
       
   114      * Creates a new output stream with a default buffer size and
       
   115      * the specified flush mode.
       
   116      *
       
   117      * @param out the output stream
       
   118      * @param syncFlush
       
   119      *        if {@code true} invocation of the inherited
       
   120      *        {@link DeflaterOutputStream#flush() flush()} method of
       
   121      *        this instance flushes the compressor with flush mode
       
   122      *        {@link Deflater#SYNC_FLUSH} before flushing the output
       
   123      *        stream, otherwise only flushes the output stream
       
   124      *
       
   125      * @exception IOException If an I/O error has occurred.
       
   126      *
       
   127      * @since 1.7
       
   128      */
       
   129     public GZIPOutputStream(OutputStream out, boolean syncFlush)
       
   130         throws IOException
       
   131     {
       
   132         this(out, 512, syncFlush);
    76     }
   133     }
    77 
   134 
    78     /**
   135     /**
    79      * Writes array of bytes to the compressed output stream. This method
   136      * Writes array of bytes to the compressed output stream. This method
    80      * will block until all the bytes are written.
   137      * will block until all the bytes are written.
   120     }
   177     }
   121 
   178 
   122     /*
   179     /*
   123      * Writes GZIP member header.
   180      * Writes GZIP member header.
   124      */
   181      */
   125 
       
   126     private final static byte[] header = {
       
   127         (byte) GZIP_MAGIC,                // Magic number (short)
       
   128         (byte)(GZIP_MAGIC >> 8),          // Magic number (short)
       
   129         Deflater.DEFLATED,                // Compression method (CM)
       
   130         0,                                // Flags (FLG)
       
   131         0,                                // Modification time MTIME (int)
       
   132         0,                                // Modification time MTIME (int)
       
   133         0,                                // Modification time MTIME (int)
       
   134         0,                                // Modification time MTIME (int)
       
   135         0,                                // Extra flags (XFLG)
       
   136         0                                 // Operating system (OS)
       
   137     };
       
   138 
       
   139     private void writeHeader() throws IOException {
   182     private void writeHeader() throws IOException {
   140         out.write(header);
   183         out.write(new byte[] {
       
   184                       (byte) GZIP_MAGIC,        // Magic number (short)
       
   185                       (byte)(GZIP_MAGIC >> 8),  // Magic number (short)
       
   186                       Deflater.DEFLATED,        // Compression method (CM)
       
   187                       0,                        // Flags (FLG)
       
   188                       0,                        // Modification time MTIME (int)
       
   189                       0,                        // Modification time MTIME (int)
       
   190                       0,                        // Modification time MTIME (int)
       
   191                       0,                        // Modification time MTIME (int)
       
   192                       0,                        // Extra flags (XFLG)
       
   193                       0                         // Operating system (OS)
       
   194                   });
   141     }
   195     }
   142 
   196 
   143     /*
   197     /*
   144      * Writes GZIP member trailer to a byte array, starting at a given
   198      * Writes GZIP member trailer to a byte array, starting at a given
   145      * offset.
   199      * offset.