Rev | Line | |
---|
[44] | 1 | package omq.common.util; |
---|
| 2 | |
---|
| 3 | import java.io.ByteArrayInputStream; |
---|
| 4 | import java.io.ByteArrayOutputStream; |
---|
| 5 | import java.io.IOException; |
---|
| 6 | import java.util.zip.GZIPInputStream; |
---|
| 7 | import java.util.zip.GZIPOutputStream; |
---|
| 8 | |
---|
[83] | 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 | */ |
---|
[44] | 16 | public 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); |
---|
[83] | 25 | } finally { |
---|
| 26 | if (zos != null) { |
---|
[44] | 27 | zos.close(); |
---|
| 28 | } |
---|
[83] | 29 | |
---|
[44] | 30 | baos.close(); |
---|
| 31 | } |
---|
[83] | 32 | |
---|
[44] | 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); |
---|
[83] | 43 | |
---|
[44] | 44 | byte[] tmpBuffer = new byte[256]; |
---|
| 45 | int n; |
---|
| 46 | while ((n = zis.read(tmpBuffer)) >= 0) { |
---|
| 47 | baos.write(tmpBuffer, 0, n); |
---|
| 48 | } |
---|
[83] | 49 | } finally { |
---|
| 50 | if (zis != null) { |
---|
[44] | 51 | zis.close(); |
---|
| 52 | } |
---|
[83] | 53 | |
---|
[44] | 54 | bais.close(); |
---|
| 55 | baos.close(); |
---|
[83] | 56 | } |
---|
| 57 | |
---|
[44] | 58 | return baos.toByteArray(); |
---|
[83] | 59 | } |
---|
[44] | 60 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.