KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > nutch > io > WritableUtils


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.io;
5
6 import java.io.*;
7
8 import java.util.zip.GZIPInputStream JavaDoc;
9 import java.util.zip.GZIPOutputStream JavaDoc;
10
11 public final class WritableUtils {
12
13   public static byte[] readCompressedByteArray(DataInput in) throws IOException {
14     int length = in.readInt();
15     byte[] buffer = new byte[length];
16     in.readFully(buffer); // could/should use readFully(buffer,0,length)?
17
GZIPInputStream JavaDoc gzi = new GZIPInputStream JavaDoc(new ByteArrayInputStream(buffer, 0, buffer.length));
18     byte[] outbuf = new byte[length];
19     ByteArrayOutputStream bos = new ByteArrayOutputStream();
20      int len;
21      while((len=gzi.read(outbuf,0,outbuf.length)) != -1){
22        bos.write(outbuf,0,len);
23      }
24      byte[] decompressed = bos.toByteArray();
25      bos.close();
26      gzi.close();
27      return decompressed;
28   }
29
30   public static int writeCompressedByteArray(DataOutput out, byte[] bytes) throws IOException {
31     ByteArrayOutputStream bos = new ByteArrayOutputStream();
32     GZIPOutputStream JavaDoc gzout = new GZIPOutputStream JavaDoc(bos);
33     gzout.write(bytes,0,bytes.length);
34     gzout.close();
35     byte[] buffer = bos.toByteArray();
36     int len = buffer.length;
37     out.writeInt(len);
38     out.write(buffer,0,len);
39     /* debug only! Once we have confidence, can lose this. */
40     return ((bytes.length != 0) ? (100*buffer.length)/bytes.length : 0);
41   }
42
43
44   /* Ugly utility, maybe someone else can do this better */
45   public static String JavaDoc readCompressedString(DataInput in) throws IOException {
46     return new String JavaDoc(readCompressedByteArray(in),"UTF-8");
47   }
48
49
50   public static int writeCompressedString(DataOutput out, String JavaDoc s) throws IOException {
51     return writeCompressedByteArray(out, s.getBytes("UTF-8"));
52   }
53
54   /*
55    *
56    * Write a String as a Network Int n, followed by n Bytes
57    * Alternative to 16 bit read/writeUTF.
58    * Encoding standard is... ?
59    *
60    */

61   public static void writeString(DataOutput out, String JavaDoc s) throws IOException {
62     byte[] buffer = s.getBytes("UTF-8");
63     int len = buffer.length;
64     out.writeInt(len);
65     out.write(buffer,0,len);
66   }
67
68   /*
69    * Read a String as a Network Int n, followed by n Bytes
70    * Alternative to 16 bit read/writeUTF.
71    * Encoding standard is... ?
72    *
73    */

74   public static String JavaDoc readString(DataInput in) throws IOException{
75     int length = in.readInt();
76     byte[] buffer = new byte[length];
77     in.readFully(buffer); // could/should use readFully(buffer,0,length)?
78
return new String JavaDoc(buffer,"UTF-8");
79   }
80
81
82   /*
83    * Write a String array as a Nework Int N, followed by Int N Byte Array Strings.
84    * Could be generalised using introspection.
85    *
86    */

87   public static void writeStringArray(DataOutput out, String JavaDoc[] s) throws IOException{
88     out.writeInt(s.length);
89     for(int i=0;i < s.length;i++){
90       writeString(out,s[i]);
91     }
92   }
93
94   /*
95    * Write a String array as a Nework Int N, followed by Int N Byte Array Strings.
96    * Could be generalised using introspection. Actually this bit couldn't...
97    *
98    */

99   public static String JavaDoc[] readStringArray(DataInput in) throws IOException {
100     int len = in.readInt();
101     String JavaDoc[] s = new String JavaDoc[len];
102     for(int i=0;i < len;i++){
103       s[i] = readString(in);
104     }
105     return s;
106   }
107
108
109   /*
110    *
111    * Test Utility Method Display Byte Array.
112    *
113    */

114   public static void displayByteArray(byte[] record){
115     int i;
116     for(i=0;i < record.length -1 ; i++){
117       if (i % 16 == 0) { System.out.println(); }
118       System.out.print(Integer.toHexString(record[i] >> 4 & 0x0F));
119       System.out.print(Integer.toHexString(record[i] & 0x0F));
120       System.out.print(",");
121     }
122     System.out.print(Integer.toHexString(record[i] >> 4 & 0x0F));
123     System.out.print(Integer.toHexString(record[i] & 0x0F));
124     System.out.println();
125   }
126
127
128 }
129
Popular Tags