KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > nutch > util > GZIPUtils


1 /* Copyright (c) 2003 The Nutch Organization. All rights reserved. */
2 /* Use subject to the conditions in http://www.nutch.org/LICENSE.txt. */
3
4 package net.nutch.util;
5
6 import java.io.ByteArrayOutputStream JavaDoc;
7 import java.io.ByteArrayInputStream JavaDoc;
8 import java.io.IOException JavaDoc;
9 import java.util.zip.GZIPInputStream JavaDoc;
10 import java.util.zip.GZIPOutputStream JavaDoc;
11
12 /**
13  * A collection of utility methods for working on GZIPed data.
14  */

15 public class GZIPUtils {
16
17   private static final int EXPECTED_COMPRESSION_RATIO= 5;
18   private static final int BUF_SIZE= 4096;
19
20   /**
21    * Returns an gunzipped copy of the input array. If the gzipped
22    * input has been truncated or corrupted, a best-effort attempt is
23    * made to unzip as much as possible. If no data can be extracted
24    * <code>null</code> is returned.
25    */

26   public static final byte[] unzipBestEffort(byte[] in) {
27     return unzipBestEffort(in, Integer.MAX_VALUE);
28   }
29
30   /**
31    * Returns an gunzipped copy of the input array, truncated to
32    * <code>sizeLimit</code> bytes, if necessary. If the gzipped input
33    * has been truncated or corrupted, a best-effort attempt is made to
34    * unzip as much as possible. If no data can be extracted
35    * <code>null</code> is returned.
36    */

37   public static final byte[] unzipBestEffort(byte[] in, int sizeLimit) {
38     try {
39       // decompress using GZIPInputStream
40
ByteArrayOutputStream JavaDoc outStream =
41         new ByteArrayOutputStream JavaDoc(EXPECTED_COMPRESSION_RATIO * in.length);
42
43       GZIPInputStream JavaDoc inStream =
44         new GZIPInputStream JavaDoc ( new ByteArrayInputStream JavaDoc(in) );
45
46       byte[] buf = new byte[BUF_SIZE];
47       int written = 0;
48       while (true) {
49         try {
50           int size = inStream.read(buf);
51           if (size <= 0)
52             break;
53           if ((written + size) > sizeLimit) {
54             outStream.write(buf, 0, sizeLimit - written);
55             break;
56           }
57           outStream.write(buf, 0, size);
58           written+= size;
59         } catch (Exception JavaDoc e) {
60           break;
61         }
62       }
63       try {
64         outStream.close();
65       } catch (IOException JavaDoc e) {
66       }
67
68       return outStream.toByteArray();
69
70     } catch (IOException JavaDoc e) {
71       return null;
72     }
73   }
74
75
76   /**
77    * Returns an gunzipped copy of the input array.
78    * @throws IOException if the input cannot be properly decompressed
79    */

80   public static final byte[] unzip(byte[] in) throws IOException JavaDoc {
81     // decompress using GZIPInputStream
82
ByteArrayOutputStream JavaDoc outStream =
83       new ByteArrayOutputStream JavaDoc(EXPECTED_COMPRESSION_RATIO * in.length);
84
85     GZIPInputStream JavaDoc inStream =
86       new GZIPInputStream JavaDoc ( new ByteArrayInputStream JavaDoc(in) );
87
88     byte[] buf = new byte[BUF_SIZE];
89     while (true) {
90       int size = inStream.read(buf);
91       if (size <= 0)
92         break;
93       outStream.write(buf, 0, size);
94     }
95     outStream.close();
96
97     return outStream.toByteArray();
98   }
99
100   /**
101    * Returns an gzipped copy of the input array.
102    */

103   public static final byte[] zip(byte[] in) {
104     try {
105       // compress using GZIPOutputStream
106
ByteArrayOutputStream JavaDoc byteOut=
107         new ByteArrayOutputStream JavaDoc(in.length / EXPECTED_COMPRESSION_RATIO);
108
109       GZIPOutputStream JavaDoc outStream= new GZIPOutputStream JavaDoc(byteOut);
110
111       try {
112         outStream.write(in);
113       } catch (Exception JavaDoc e) {
114         e.printStackTrace();
115       }
116
117       try {
118         outStream.close();
119       } catch (IOException JavaDoc e) {
120         e.printStackTrace();
121       }
122
123       return byteOut.toByteArray();
124
125     } catch (IOException JavaDoc e) {
126       e.printStackTrace();
127       return null;
128     }
129   }
130     
131 }
132
Popular Tags