1 /*
2 * #%L
3 * Nuiton Utils
4 * %%
5 * Copyright (C) 2004 - 2010 CodeLutin
6 * %%
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Lesser Public License for more details.
16 *
17 * You should have received a copy of the GNU General Lesser Public
18 * License along with this program. If not, see
19 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
20 * #L%
21 */
22
23 /* *
24 * GZUtil.java
25 *
26 * Created: 3 nov. 2004
27 *
28 * @author Benjamin Poussin - poussin@codelutin.com
29 *
30 *
31 * Mise a jour: $Date$
32 * par : */
33
34 package org.nuiton.util;
35
36 import java.io.BufferedInputStream;
37 import java.io.BufferedOutputStream;
38 import java.io.BufferedReader;
39 import java.io.ByteArrayInputStream;
40 import java.io.ByteArrayOutputStream;
41 import java.io.IOException;
42 import java.io.InputStream;
43 import java.io.Reader;
44 import java.io.StringReader;
45 import java.util.zip.GZIPInputStream;
46 import java.util.zip.GZIPOutputStream;
47
48 public class GZUtil { // GZUtil
49
50 /**
51 * Tests if an inputStream is GZipped.
52 * <p>
53 * <b>Note:</b> the stream is not closed and is reset.
54 *
55 * @param inputStream inputStream to test
56 * @return {@code true} if inputStream is gzipped, {@code false} otherwise
57 * @throws IOException if any io errors while reading inputStream
58 * @since 3.0
59 */
60 public static boolean isGzipStream(InputStream inputStream) throws IOException {
61
62 inputStream.mark(2);
63 // read header to see if is compressed file
64 int b = inputStream.read();
65 // redundant cast : int magic = ((int) in.read() << 8) | b;
66 int magic = inputStream.read() << 8 | b;
67 inputStream.reset();
68 boolean gzip = magic == GZIPInputStream.GZIP_MAGIC;
69 return gzip;
70
71 }
72
73 /**
74 * @param in FIXME
75 * @return Retourne la string decompressee
76 */
77 public static StringBuffer bytesToStringBuffer(byte[] in) {
78 try {
79 if (in == null || in.length == 0) {
80 return new StringBuffer("");
81 }
82 GZIPInputStream gz = new GZIPInputStream(new BufferedInputStream(new ByteArrayInputStream(in)));
83 StringBuffer sb = new StringBuffer();
84 int c;
85
86 while ((c = gz.read()) != -1) {
87 sb.append((char) c);
88 }
89 gz.close();
90 return sb;
91 } catch (IOException eee) {
92 throw new GZUtilException("Probleme dans la decompression", eee);
93 }
94 }
95
96 /**
97 * @param in FIXME
98 * @return la string decompressee
99 */
100 public static String bytesToString(byte[] in) {
101 return bytesToStringBuffer(in).toString();
102 }
103
104 /**
105 * @param elem FIXME
106 * @return Retourne la string compressee
107 */
108 public static byte[] stringBufferToBytes(StringBuffer elem) {
109 return stringToBytes(elem.toString());
110 }
111
112 /**
113 * @param elem FIXME
114 * @return Retourne la string compressee
115 */
116 public static byte[] stringToBytes(String elem) {
117 try {
118 ByteArrayOutputStream baos = new ByteArrayOutputStream();
119 GZIPOutputStream gz = new GZIPOutputStream(new BufferedOutputStream(baos));
120 Reader sr = new BufferedReader(new StringReader(elem));
121 int c;
122 while ((c = sr.read()) != -1) {
123 gz.write((char) c);
124 }
125 gz.close();
126
127 return baos.toByteArray();
128 } catch (IOException eee) {
129 throw new GZUtilException("Probleme dans la compression", eee);
130 }
131 }
132
133 } // GZUtil
134