GlueGen v2.6.0-rc-20250712
GlueGen, Native Binding Generator for Java™ (public API).
CustomCompress.java
Go to the documentation of this file.
1/**
2 * Copyright 2015 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28package com.jogamp.common.util;
29
30import java.io.DataInputStream;
31import java.io.DataOutputStream;
32import java.io.IOException;
33import java.io.InputStream;
34import java.io.OutputStream;
35import java.util.zip.DataFormatException;
36import java.util.zip.Deflater;
37import java.util.zip.Inflater;
38
39/**
40 * All in memory inflater / deflator for small chunks using streams
41 * <p>
42 * Stream header of deflated data:
43 * <ul>
44 * <li>4 bytes magic 0xDEF1A7E0 (Big Endian)</li>
45 * <li>4 bytes integer deflated-size (Big Endian)</li>
46 * <li>4 bytes integer inflated-size (Big Endian)</li>
47 * <li>deflated bytes</li>
48 * </ul>
49 * </p>
50 */
51public class CustomCompress {
52 /** Start of stream header for deflated data */
53 public static final int MAGIC = 0xDEF1A7E0;
54
55 /**
56 *
57 * @param in {@link InputStream} at start of stream header, i.e. position {@link #MAGIC}.
58 * @return the inflated bytes from the stream
59 * @throws IOException if an I/O or deflation exception occurs
60 * @throws IllegalArgumentException if {@code inLen} &le; 0 or {@code outLen} &le; 0, as read from header
61 */
62 public static byte[] inflateFromStream(final InputStream in)
63 throws IOException, ArrayIndexOutOfBoundsException, IllegalArgumentException
64 {
65 final int inLen;
66 final int outLen;
67 {
68 final DataInputStream din = new DataInputStream(in);
69 final int _magic = din.readInt();
70 if( _magic != MAGIC ) {
71 throw new IOException("wrong magic: "+Integer.toHexString(_magic)+", expected "+Integer.toHexString(MAGIC));
72 }
73 inLen = din.readInt();
74 outLen = din.readInt();
75 }
76 return inflateFromStream(in, inLen, outLen, new byte[outLen], 0);
77 }
78
79 /**
80 *
81 * @param in {@link InputStream} at start of deflated bytes, i.e. after the stream header.
82 * @param inLen number of deflated bytes in stream {@code in}
83 * @param outLen number of inflated {@code output} bytes at {@code outOff}
84 * @param output sink for deflated bytes
85 * @param outOff offset to {@code output}
86 * @return the inflated bytes from the stream, passing {@code output} for chaining
87 * @throws IOException if an I/O or deflation exception occurs
88 * @throws ArrayIndexOutOfBoundsException if {@code outOff} and {@code outLen} exceeds {@code output}
89 * @throws IllegalArgumentException if {@code inLen} &le; 0 or {@code outLen} &le; 0
90 */
91 public static byte[] inflateFromStream(final InputStream in, final int inLen, final int outLen,
92 final byte[] output, final int outOff)
93 throws IOException, ArrayIndexOutOfBoundsException, IllegalArgumentException
94 {
95 if (inLen <= 0 || outLen <= 0 ) {
96 throw new IllegalArgumentException("Length[input "+inLen+", output "+outLen+"]");
97 }
98 if (outOff < 0 || output.length < outOff + outLen) {
99 throw new ArrayIndexOutOfBoundsException("output.length "+output.length+", offset "+outOff+", length "+outLen);
100 }
101 final byte[] input = new byte[inLen];
102 int numBytes = 0;
103 try {
104 while (true) {
105 final int remBytes = inLen - numBytes;
106 int count;
107 if ( 0 >= remBytes || (count = in.read(input, numBytes, remBytes)) == -1 ) {
108 break;
109 }
110 numBytes += count;
111 }
112 } finally {
113 in.close();
114 }
115 if( inLen != numBytes ) {
116 throw new IOException("Got "+numBytes+" bytes != expected "+inLen);
117 }
118 try {
119 final Inflater inflater = new Inflater();
120 inflater.setInput(input, 0, inLen);
121 final int outSize = inflater.inflate(output, outOff, outLen);
122 inflater.end();
123 if( outLen != outSize ) {
124 throw new IOException("Got inflated "+outSize+" bytes != expected "+outLen);
125 }
126 } catch(final DataFormatException dfe) {
127 throw new IOException(dfe);
128 }
129 return output;
130 }
131
132 /**
133 * @param input raw input bytes
134 * @param inOff offset to {@code input}
135 * @param inLen number of {@code input} bytes at {@code inOff}
136 * @param level compression level 0-9 or {@link Deflater#DEFAULT_COMPRESSION}
137 * @param out sink for deflated bytes
138 * @return number of deflated bytes written, not including the header.
139 * @throws IOException if an I/O or deflation exception occurs
140 * @throws ArrayIndexOutOfBoundsException if {@code inOff} and {@code inLen} exceeds {@code input}
141 * @throws IllegalArgumentException if {@code inLen} &le; 0
142 */
143 public static int deflateToStream(final byte[] input, final int inOff, final int inLen,
144 final int level, final OutputStream out) throws IOException, ArrayIndexOutOfBoundsException, IllegalArgumentException {
145 if (inLen <= 0 ) {
146 throw new IllegalArgumentException("Length[input "+inLen+"]");
147 }
148 if (inOff < 0 || input.length < inOff + inLen) {
149 throw new ArrayIndexOutOfBoundsException("input.length "+input.length+", offset "+inOff+", length "+inLen);
150 }
151 final byte[] output = new byte[inLen];
152 final Deflater deflater = new Deflater(level);
153 deflater.setInput(input, inOff, inLen);
154 deflater.finish();
155 final int outSize = deflater.deflate(output, 0, inLen);
156 deflater.end();
157 {
158 final DataOutputStream dout = new DataOutputStream(out);
159 dout.writeInt(CustomCompress.MAGIC);
160 dout.writeInt(outSize);
161 dout.writeInt(inLen);
162 }
163 out.write(output, 0, outSize);
164 return outSize;
165 }
166
167}
All in memory inflater / deflator for small chunks using streams.
static byte[] inflateFromStream(final InputStream in)
static byte[] inflateFromStream(final InputStream in, final int inLen, final int outLen, final byte[] output, final int outOff)
static int deflateToStream(final byte[] input, final int inOff, final int inLen, final int level, final OutputStream out)
static final int MAGIC
Start of stream header for deflated data.