source: branches/supervisor/src/main/java/omq/common/util/Zipper.java @ 91

Last change on this file since 91 was 83, checked in by stoda, 11 years ago

J

File size: 1.3 KB
Line 
1package omq.common.util;
2
3import java.io.ByteArrayInputStream;
4import java.io.ByteArrayOutputStream;
5import java.io.IOException;
6import java.util.zip.GZIPInputStream;
7import java.util.zip.GZIPOutputStream;
8
9/**
10 * This class enables the compression of the information sent through the
11 * rabbitmq server.
12 *
13 * @author Sergi Toda <sergi.toda@estudiants.urv.cat>
14 *
15 */
16public class Zipper {
17
18        public static byte[] zip(byte[] b) throws IOException {
19                ByteArrayOutputStream baos = new ByteArrayOutputStream();
20
21                GZIPOutputStream zos = null;
22                try {
23                        zos = new GZIPOutputStream(baos);
24                        zos.write(b);
25                } finally {
26                        if (zos != null) {
27                                zos.close();
28                        }
29
30                        baos.close();
31                }
32
33                return baos.toByteArray();
34        }
35
36        public static byte[] unzip(byte[] b) throws IOException {
37                ByteArrayOutputStream baos = new ByteArrayOutputStream();
38                ByteArrayInputStream bais = new ByteArrayInputStream(b);
39
40                GZIPInputStream zis = null;
41                try {
42                        zis = new GZIPInputStream(bais);
43
44                        byte[] tmpBuffer = new byte[256];
45                        int n;
46                        while ((n = zis.read(tmpBuffer)) >= 0) {
47                                baos.write(tmpBuffer, 0, n);
48                        }
49                } finally {
50                        if (zis != null) {
51                                zis.close();
52                        }
53
54                        bais.close();
55                        baos.close();
56                }
57
58                return baos.toByteArray();
59        }
60}
Note: See TracBrowser for help on using the repository browser.