1+ /*
2+ * Copyright (c) 2024, 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
23+ * questions.
24+ */
25+ package java .util .zip ;
26+
27+ import java .io .ByteArrayOutputStream ;
28+ import java .nio .charset .StandardCharsets ;
29+
30+ class Snippets {
31+
32+ void deflaterInflaterExample () {
33+ // @start region="DeflaterInflaterExample"
34+
35+ // Encode a String into bytes
36+ String inputString = "blahblahblah\u20AC \u20AC " ;
37+ byte [] input = inputString .getBytes (StandardCharsets .UTF_8 );
38+
39+ // Compress the bytes
40+ ByteArrayOutputStream compressedBaos = new ByteArrayOutputStream ();
41+ Deflater compressor = new Deflater ();
42+ try {
43+ compressor .setInput (input );
44+ // Let the compressor know that the complete input
45+ // has been made available
46+ compressor .finish ();
47+ // Keep compressing the input till the compressor
48+ // is finished compressing
49+ while (!compressor .finished ()) {
50+ // Use some reasonable size for the temporary buffer
51+ // based on the data being compressed
52+ byte [] tmpBuffer = new byte [100 ];
53+ int numCompressed = compressor .deflate (tmpBuffer );
54+ // Copy over the compressed bytes from the temporary
55+ // buffer into the final byte array
56+ compressedBaos .write (tmpBuffer , 0 , numCompressed );
57+ }
58+ } finally {
59+ // Release the resources held by the compressor
60+ compressor .end ();
61+ }
62+
63+ // Decompress the bytes
64+ Inflater decompressor = new Inflater ();
65+ ByteArrayOutputStream decompressedBaos = new ByteArrayOutputStream ();
66+ try {
67+ byte [] compressed = compressedBaos .toByteArray ();
68+ decompressor .setInput (compressed , 0 , compressed .length );
69+ while (!decompressor .finished ()) {
70+ // Use some reasonable size for the temporary buffer,
71+ // based on the data being decompressed; in this example,
72+ // we use a small buffer size
73+ byte [] tmpBuffer = new byte [100 ];
74+ int numDecompressed = 0 ;
75+ try {
76+ numDecompressed = decompressor .inflate (tmpBuffer );
77+ } catch (DataFormatException dfe ) {
78+ // Handle the exception suitably, in this example
79+ // we just rethrow it
80+ throw new RuntimeException (dfe );
81+ }
82+ // Copy over the decompressed bytes from the temporary
83+ // buffer into the final byte array
84+ decompressedBaos .write (tmpBuffer , 0 , numDecompressed );
85+ }
86+ } finally {
87+ // Release the resources held by the decompressor
88+ decompressor .end ();
89+ }
90+ // Decode the bytes into a String
91+ String outputString = decompressedBaos .toString (StandardCharsets .UTF_8 );
92+
93+ // @end
94+ }
95+
96+ }
0 commit comments