< prev index next >

src/java.base/share/classes/java/util/zip/DeflaterOutputStream.java

Print this page
rev 56290 : 8230648: Replace @exception tag with @throws in java.base
Summary: Minor coding style update of javadoc tag in any file in java.base
Reviewed-by: prappo, lancea
   1 /*
   2  * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   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
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  84             throw new NullPointerException();
  85         } else if (size <= 0) {
  86             throw new IllegalArgumentException("buffer size <= 0");
  87         }
  88         this.def = def;
  89         this.buf = new byte[size];
  90         this.syncFlush = syncFlush;
  91     }
  92 
  93 
  94     /**
  95      * Creates a new output stream with the specified compressor and
  96      * buffer size.
  97      *
  98      * <p>The new output stream instance is created as if by invoking
  99      * the 4-argument constructor DeflaterOutputStream(out, def, size, false).
 100      *
 101      * @param out the output stream
 102      * @param def the compressor ("deflater")
 103      * @param size the output buffer size
 104      * @exception IllegalArgumentException if {@code size <= 0}
 105      */
 106     public DeflaterOutputStream(OutputStream out, Deflater def, int size) {
 107         this(out, def, size, false);
 108     }
 109 
 110     /**
 111      * Creates a new output stream with the specified compressor, flush
 112      * mode and a default buffer size.
 113      *
 114      * @param out the output stream
 115      * @param def the compressor ("deflater")
 116      * @param syncFlush
 117      *        if {@code true} the {@link #flush()} method of this
 118      *        instance flushes the compressor with flush mode
 119      *        {@link Deflater#SYNC_FLUSH} before flushing the output
 120      *        stream, otherwise only flushes the output stream
 121      *
 122      * @since 1.7
 123      */
 124     public DeflaterOutputStream(OutputStream out,


 163         usesDefaultDeflater = true;
 164     }
 165 
 166     /**
 167      * Creates a new output stream with a default compressor and buffer size.
 168      *
 169      * <p>The new output stream instance is created as if by invoking
 170      * the 2-argument constructor DeflaterOutputStream(out, false).
 171      *
 172      * @param out the output stream
 173      */
 174     public DeflaterOutputStream(OutputStream out) {
 175         this(out, false);
 176         usesDefaultDeflater = true;
 177     }
 178 
 179     /**
 180      * Writes a byte to the compressed output stream. This method will
 181      * block until the byte can be written.
 182      * @param b the byte to be written
 183      * @exception IOException if an I/O error has occurred
 184      */
 185     public void write(int b) throws IOException {
 186         byte[] buf = new byte[1];
 187         buf[0] = (byte)(b & 0xff);
 188         write(buf, 0, 1);
 189     }
 190 
 191     /**
 192      * Writes an array of bytes to the compressed output stream. This
 193      * method will block until all the bytes are written.
 194      * @param b the data to be written
 195      * @param off the start offset of the data
 196      * @param len the length of the data
 197      * @exception IOException if an I/O error has occurred
 198      */
 199     public void write(byte[] b, int off, int len) throws IOException {
 200         if (def.finished()) {
 201             throw new IOException("write beyond end of stream");
 202         }
 203         if ((off | len | (off + len) | (b.length - (off + len))) < 0) {
 204             throw new IndexOutOfBoundsException();
 205         } else if (len == 0) {
 206             return;
 207         }
 208         if (!def.finished()) {
 209             def.setInput(b, off, len);
 210             while (!def.needsInput()) {
 211                 deflate();
 212             }
 213         }
 214     }
 215 
 216     /**
 217      * Finishes writing compressed data to the output stream without closing
 218      * the underlying stream. Use this method when applying multiple filters
 219      * in succession to the same output stream.
 220      * @exception IOException if an I/O error has occurred
 221      */
 222     public void finish() throws IOException {
 223         if (!def.finished()) {
 224             def.finish();
 225             while (!def.finished()) {
 226                 deflate();
 227             }
 228         }
 229     }
 230 
 231     /**
 232      * Writes remaining compressed data to the output stream and closes the
 233      * underlying stream.
 234      * @exception IOException if an I/O error has occurred
 235      */
 236     public void close() throws IOException {
 237         if (!closed) {
 238             finish();
 239             if (usesDefaultDeflater)
 240                 def.end();
 241             out.close();
 242             closed = true;
 243         }
 244     }
 245 
 246     /**
 247      * Writes next block of compressed data to the output stream.
 248      * @throws IOException if an I/O error has occurred
 249      */
 250     protected void deflate() throws IOException {
 251         int len = def.deflate(buf, 0, buf.length);
 252         if (len > 0) {
 253             out.write(buf, 0, len);
 254         }


   1 /*
   2  * Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   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
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  84             throw new NullPointerException();
  85         } else if (size <= 0) {
  86             throw new IllegalArgumentException("buffer size <= 0");
  87         }
  88         this.def = def;
  89         this.buf = new byte[size];
  90         this.syncFlush = syncFlush;
  91     }
  92 
  93 
  94     /**
  95      * Creates a new output stream with the specified compressor and
  96      * buffer size.
  97      *
  98      * <p>The new output stream instance is created as if by invoking
  99      * the 4-argument constructor DeflaterOutputStream(out, def, size, false).
 100      *
 101      * @param out the output stream
 102      * @param def the compressor ("deflater")
 103      * @param size the output buffer size
 104      * @throws    IllegalArgumentException if {@code size <= 0}
 105      */
 106     public DeflaterOutputStream(OutputStream out, Deflater def, int size) {
 107         this(out, def, size, false);
 108     }
 109 
 110     /**
 111      * Creates a new output stream with the specified compressor, flush
 112      * mode and a default buffer size.
 113      *
 114      * @param out the output stream
 115      * @param def the compressor ("deflater")
 116      * @param syncFlush
 117      *        if {@code true} the {@link #flush()} method of this
 118      *        instance flushes the compressor with flush mode
 119      *        {@link Deflater#SYNC_FLUSH} before flushing the output
 120      *        stream, otherwise only flushes the output stream
 121      *
 122      * @since 1.7
 123      */
 124     public DeflaterOutputStream(OutputStream out,


 163         usesDefaultDeflater = true;
 164     }
 165 
 166     /**
 167      * Creates a new output stream with a default compressor and buffer size.
 168      *
 169      * <p>The new output stream instance is created as if by invoking
 170      * the 2-argument constructor DeflaterOutputStream(out, false).
 171      *
 172      * @param out the output stream
 173      */
 174     public DeflaterOutputStream(OutputStream out) {
 175         this(out, false);
 176         usesDefaultDeflater = true;
 177     }
 178 
 179     /**
 180      * Writes a byte to the compressed output stream. This method will
 181      * block until the byte can be written.
 182      * @param b the byte to be written
 183      * @throws    IOException if an I/O error has occurred
 184      */
 185     public void write(int b) throws IOException {
 186         byte[] buf = new byte[1];
 187         buf[0] = (byte)(b & 0xff);
 188         write(buf, 0, 1);
 189     }
 190 
 191     /**
 192      * Writes an array of bytes to the compressed output stream. This
 193      * method will block until all the bytes are written.
 194      * @param b the data to be written
 195      * @param off the start offset of the data
 196      * @param len the length of the data
 197      * @throws    IOException if an I/O error has occurred
 198      */
 199     public void write(byte[] b, int off, int len) throws IOException {
 200         if (def.finished()) {
 201             throw new IOException("write beyond end of stream");
 202         }
 203         if ((off | len | (off + len) | (b.length - (off + len))) < 0) {
 204             throw new IndexOutOfBoundsException();
 205         } else if (len == 0) {
 206             return;
 207         }
 208         if (!def.finished()) {
 209             def.setInput(b, off, len);
 210             while (!def.needsInput()) {
 211                 deflate();
 212             }
 213         }
 214     }
 215 
 216     /**
 217      * Finishes writing compressed data to the output stream without closing
 218      * the underlying stream. Use this method when applying multiple filters
 219      * in succession to the same output stream.
 220      * @throws    IOException if an I/O error has occurred
 221      */
 222     public void finish() throws IOException {
 223         if (!def.finished()) {
 224             def.finish();
 225             while (!def.finished()) {
 226                 deflate();
 227             }
 228         }
 229     }
 230 
 231     /**
 232      * Writes remaining compressed data to the output stream and closes the
 233      * underlying stream.
 234      * @throws    IOException if an I/O error has occurred
 235      */
 236     public void close() throws IOException {
 237         if (!closed) {
 238             finish();
 239             if (usesDefaultDeflater)
 240                 def.end();
 241             out.close();
 242             closed = true;
 243         }
 244     }
 245 
 246     /**
 247      * Writes next block of compressed data to the output stream.
 248      * @throws IOException if an I/O error has occurred
 249      */
 250     protected void deflate() throws IOException {
 251         int len = def.deflate(buf, 0, buf.length);
 252         if (len > 0) {
 253             out.write(buf, 0, len);
 254         }


< prev index next >