RuneHive-Game
Loading...
Searching...
No Matches
CompressionUtil.java
Go to the documentation of this file.
1package com.runehive.fs.util;
2
3import com.runehive.fs.cache.FileSystem;
4import org.apache.tools.bzip2.CBZip2InputStream;
5
6import java.io.ByteArrayInputStream;
7import java.io.IOException;
8import java.util.zip.GZIPInputStream;
9
10import static com.google.common.io.ByteStreams.toByteArray;
11
12/**
13 * A static-utility class containing containing extension or helper methods for
14 * <b>co</b>mpressor-<b>dec</b>compressor<b>'s</b>.
15 * @author Ryley Kimmel <ryley.kimmel@live.com>
16 */
17public final class CompressionUtil {
18
19 /**
20 * Uncompresses a {@code byte} array of g-zipped data.
21 * @param data The compressed, g-zipped data.
22 * @return The uncompressed data.
23 * @throws IOException If some I/O exception occurs.
24 */
25 public static byte[] gunzip(byte[] data) throws IOException {
26 return toByteArray(new GZIPInputStream(new ByteArrayInputStream(data)));
27 }
28
29 /**
30 * Uncompresses a {@code byte} array of b-zipped data that does not contain
31 * a header.
32 * <p>
33 * <p>
34 * A b-zip header block consists of <tt>2</tt> {@code byte}s, they are
35 * replaced with 'h' and '1' as that is what our {@link FileSystem file
36 * system} compresses the header as.
37 * </p>
38 * @param data The compressed, b-zipped data.
39 * @param offset The offset position of the data.
40 * @param length The length of the data.
41 * @return The uncompressed data.
42 * @throws IOException If some I/O exception occurs.
43 */
44 public static byte[] unbzip2Headerless(byte[] data, int offset, int length) throws IOException {
45 /* Strip the header from the data. */
46 byte[] bzip2 = new byte[length + 2];
47 bzip2[0] = 'h';
48 bzip2[1] = '1';
49 System.arraycopy(data, offset, bzip2, 2, length);
50
51 /* Uncompress the headerless data */
52 return unbzip2(bzip2);
53 }
54
55 /**
56 * Uncompresses a {@code byte} array of b-zipped data.
57 * @param data The compressed, b-zipped data.
58 * @return The uncompressed data.
59 * @throws IOException If some I/O exception occurs.
60 */
61 public static byte[] unbzip2(byte[] data) throws IOException {
62 return toByteArray(new CBZip2InputStream(new ByteArrayInputStream(data)));
63 }
64
65 /**
66 * Suppresses the default-public constructor preventing this class from
67 * being instantiated by other classes.
68 * @throws UnsupportedOperationException If this class is instantiated
69 * within itself.
70 */
71 private CompressionUtil() {
72 throw new UnsupportedOperationException("static-utility classes may not be instantiated.");
73 }
74
75}
CompressionUtil()
Suppresses the default-public constructor preventing this class from being instantiated by other clas...
static byte[] gunzip(byte[] data)
Uncompresses a byte array of g-zipped data.
static byte[] unbzip2(byte[] data)
Uncompresses a byte array of b-zipped data.
static byte[] unbzip2Headerless(byte[] data, int offset, int length)
Uncompresses a byte array of b-zipped data that does not contain a header.