Skip to content

Commit d8af589

Browse files
committedJun 7, 2024
8026127: Deflater/Inflater documentation incomplete/misleading
Reviewed-by: lancea
1 parent 6238bc8 commit d8af589

File tree

3 files changed

+98
-59
lines changed

3 files changed

+98
-59
lines changed
 

‎src/java.base/share/classes/java/util/zip/Deflater.java

+1-30
Original file line numberDiff line numberDiff line change
@@ -57,36 +57,7 @@
5757
* The following code fragment demonstrates a trivial compression
5858
* and decompression of a string using {@code Deflater} and
5959
* {@code Inflater}.
60-
*
61-
* <blockquote><pre>
62-
* try {
63-
* // Encode a String into bytes
64-
* String inputString = "blahblahblah";
65-
* byte[] input = inputString.getBytes("UTF-8");
66-
*
67-
* // Compress the bytes
68-
* byte[] output = new byte[100];
69-
* Deflater compresser = new Deflater();
70-
* compresser.setInput(input);
71-
* compresser.finish();
72-
* int compressedDataLength = compresser.deflate(output);
73-
* compresser.end();
74-
*
75-
* // Decompress the bytes
76-
* Inflater decompresser = new Inflater();
77-
* decompresser.setInput(output, 0, compressedDataLength);
78-
* byte[] result = new byte[100];
79-
* int resultLength = decompresser.inflate(result);
80-
* decompresser.end();
81-
*
82-
* // Decode the bytes into a String
83-
* String outputString = new String(result, 0, resultLength, "UTF-8");
84-
* } catch (java.io.UnsupportedEncodingException ex) {
85-
* // handle
86-
* } catch (java.util.zip.DataFormatException ex) {
87-
* // handle
88-
* }
89-
* </pre></blockquote>
60+
* {@snippet id="compdecomp" lang="java" class="Snippets" region="DeflaterInflaterExample"}
9061
*
9162
* @apiNote
9263
* To release resources used by this {@code Deflater}, the {@link #end()} method

‎src/java.base/share/classes/java/util/zip/Inflater.java

+1-29
Original file line numberDiff line numberDiff line change
@@ -56,35 +56,7 @@
5656
* The following code fragment demonstrates a trivial compression
5757
* and decompression of a string using {@code Deflater} and
5858
* {@code Inflater}.
59-
*
60-
* <blockquote><pre>
61-
* try {
62-
* // Encode a String into bytes
63-
* String inputString = "blahblahblah\u20AC\u20AC";
64-
* byte[] input = inputString.getBytes("UTF-8");
65-
*
66-
* // Compress the bytes
67-
* byte[] output = new byte[100];
68-
* Deflater compresser = new Deflater();
69-
* compresser.setInput(input);
70-
* compresser.finish();
71-
* int compressedDataLength = compresser.deflate(output);
72-
*
73-
* // Decompress the bytes
74-
* Inflater decompresser = new Inflater();
75-
* decompresser.setInput(output, 0, compressedDataLength);
76-
* byte[] result = new byte[100];
77-
* int resultLength = decompresser.inflate(result);
78-
* decompresser.end();
79-
*
80-
* // Decode the bytes into a String
81-
* String outputString = new String(result, 0, resultLength, "UTF-8");
82-
* } catch (java.io.UnsupportedEncodingException ex) {
83-
* // handle
84-
* } catch (java.util.zip.DataFormatException ex) {
85-
* // handle
86-
* }
87-
* </pre></blockquote>
59+
* {@snippet id="compdecomp" lang="java" class="Snippets" region="DeflaterInflaterExample"}
8860
*
8961
* @apiNote
9062
* To release resources used by this {@code Inflater}, the {@link #end()} method
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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

Comments
 (0)
Please sign in to comment.