1 26 27 package it.stefanochizzolini.clown.bytes.filters; 28 29 import java.io.ByteArrayInputStream ; 30 import java.io.ByteArrayOutputStream ; 31 import java.io.InputStream ; 32 import java.io.IOException ; 33 import java.io.OutputStream ; 34 import java.util.zip.DeflaterOutputStream ; 35 import java.util.zip.InflaterInputStream ; 36 37 44 public class FlateFilter 45 extends Filter 46 { 47 FlateFilter( 51 ) 52 {} 53 55 public byte[] decode( 58 byte[] data, 59 int offset, 60 int length 61 ) 62 { 63 ByteArrayOutputStream outputStream = new ByteArrayOutputStream (); 64 try 65 { 66 InflaterInputStream inputFilter = new InflaterInputStream ( 67 new ByteArrayInputStream (data,offset,length) 68 ); 69 70 transform(inputFilter,outputStream); 71 } 72 catch(IOException e) 73 {throw new RuntimeException ("Decoding failed.",e);} 74 75 return outputStream.toByteArray(); 76 } 77 78 public byte[] encode( 79 byte[] data, 80 int offset, 81 int length 82 ) 83 { 84 ByteArrayOutputStream outputStream = new ByteArrayOutputStream (); 85 try 86 { 87 DeflaterOutputStream outputFilter = new DeflaterOutputStream (outputStream); 88 ByteArrayInputStream inputStream = new ByteArrayInputStream (data,offset,length); 89 90 transform(inputStream,outputFilter); 91 } 92 catch(IOException e) 93 {throw new RuntimeException ("Encoding failed.",e);} 94 95 return outputStream.toByteArray(); 96 } 97 99 private void transform( 101 InputStream input, 102 OutputStream output 103 ) 104 throws IOException 105 { 106 byte[] buffer = new byte[8192]; int bufferLength; 107 while((bufferLength = input.read(buffer, 0, buffer.length)) != -1) 108 {output.write(buffer, 0, bufferLength);} 109 110 input.close(); output.close(); 111 } 112 } | Popular Tags |