1 2 3 4 package net.nutch.io; 5 6 import java.io.*; 7 8 import java.util.zip.GZIPInputStream ; 9 import java.util.zip.GZIPOutputStream ; 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); GZIPInputStream gzi = new GZIPInputStream (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 gzout = new GZIPOutputStream (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 40 return ((bytes.length != 0) ? (100*buffer.length)/bytes.length : 0); 41 } 42 43 44 45 public static String readCompressedString(DataInput in) throws IOException { 46 return new String (readCompressedByteArray(in),"UTF-8"); 47 } 48 49 50 public static int writeCompressedString(DataOutput out, String s) throws IOException { 51 return writeCompressedByteArray(out, s.getBytes("UTF-8")); 52 } 53 54 61 public static void writeString(DataOutput out, String 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 74 public static String readString(DataInput in) throws IOException{ 75 int length = in.readInt(); 76 byte[] buffer = new byte[length]; 77 in.readFully(buffer); return new String (buffer,"UTF-8"); 79 } 80 81 82 87 public static void writeStringArray(DataOutput out, String [] 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 99 public static String [] readStringArray(DataInput in) throws IOException { 100 int len = in.readInt(); 101 String [] s = new String [len]; 102 for(int i=0;i < len;i++){ 103 s[i] = readString(in); 104 } 105 return s; 106 } 107 108 109 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 |