forked from geodmms/xdgnjobs

?? ?
2008-04-14 7840e31dd97db0efe77bdf859ac2fe9767d3dd1a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package com.ximple.eofms.util;
 
//~--- JDK imports ------------------------------------------------------------
 
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;
 
/**
 * ByteArrayCompressor
 * User: Ulysses
 * Date: 2007/6/15
 * Time: 下午 02:21:00
 * To change this template use File | Settings | File Templates.
 */
public final class ByteArrayCompressor
{
    public static byte[] decompressByteArray(byte[] raw)
    {
        // Create the decompressor and give it the data to compress
        Inflater decompressor = new Inflater();
 
        decompressor.setInput(raw);
 
        // Create an expandable byte array to hold the decompressed data
        ByteArrayOutputStream bos = new ByteArrayOutputStream(raw.length);
 
        // Decompress the data
        byte[] buf = new byte[1024];
 
        while (!decompressor.finished())
        {
            try
            {
                int count = decompressor.inflate(buf);
 
                bos.write(buf, 0, count);
            } catch (DataFormatException e)
            {
            }
        }
 
        try
        {
            bos.close();
        } catch (IOException e)
        {
        }
 
        // Get the decompressed data
        byte[] decompressedData = bos.toByteArray();
 
        return decompressedData;
    }
 
    public static byte[] compressByteArray(byte[] raw)
    {
        // Create the compressor with highest level of compression
        Deflater compressor = new Deflater();
 
        compressor.setLevel(Deflater.BEST_SPEED);
 
        // Give the compressor the data to compress
        compressor.setInput(raw);
        compressor.finish();
 
        // Create an expandable byte array to hold the compressed data.
        // You cannot use an array that's the same size as the orginal because
        // there is no guarantee that the compressed data will be smaller than
        // the uncompressed data.
        ByteArrayOutputStream bos = new ByteArrayOutputStream(raw.length);
 
        // Compress the data
        byte[] buf = new byte[1024];
 
        while (!compressor.finished())
        {
            int count = compressor.deflate(buf);
 
            bos.write(buf, 0, count);
        }
 
        try
        {
            bos.close();
        } catch (IOException e)
        {
        }
 
        // Get the compressed data
        byte[] compressedData = bos.toByteArray();
 
        return compressedData;
    }
}