July 09, 2018

@Gzip compression annotation in java

package com.belazy.gzip;

import java.io.IOException;

public class Main {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String data = "hello world ";
try {
byte[] s = GzipAlgo.compress(data);
System.out.println(s);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

SamplePojo p =new SamplePojo();
GzipCompressor compressor = new GzipCompressor();
p=compressor.getCompressor(p);

}

}


/**
 *
 */
package com.belazy.gzip;

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * @author belazy
 *
 */
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.FIELD, ElementType.METHOD })
public @interface Gzip {
public String type();
}


package com.belazy.gzip;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class GzipAlgo {

public static byte[] compress(String data) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length());
GZIPOutputStream gzip = new GZIPOutputStream(bos);
gzip.write(data.getBytes());
gzip.close();
byte[] compressed = bos.toByteArray();
bos.close();
return compressed;
}
public static String decompress(byte[] compressed) throws IOException {
ByteArrayInputStream bis = new ByteArrayInputStream(compressed);
GZIPInputStream gis = new GZIPInputStream(bis);
BufferedReader br = new BufferedReader(new InputStreamReader(gis, "UTF-8"));
StringBuilder sb = new StringBuilder();
String line;
while((line = br.readLine()) != null) {
sb.append(line);
}
br.close();
gis.close();
bis.close();
return sb.toString();
}
}


/**
 * 
 */
package com.belazy.gzip;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author consumerfed itsection
 * phone : 8281808029
 * 
 */
public class GzipCompressor {

public SamplePojo getCompressor(SamplePojo axa) {
// TODO Auto-generated method stub
Class refClass = axa.getClass();
Annotation taxAnno;
Method[] methods = refClass.getMethods();

for (Method thisMethod : methods) {
String methodName = thisMethod.getName();
if (thisMethod.getAnnotations().length > 0) {
for (Annotation annotation : thisMethod.getAnnotations()) {
if (annotation instanceof Gzip) {
taxAnno = thisMethod.getAnnotation(Gzip.class);
Gzip zip = (Gzip) taxAnno;
try {
System.out.println(" *** "
+ thisMethod.invoke(axa, new String()));
String data  = (String)thisMethod.invoke(axa, new String());
byte[] b = GzipAlgo.compress(data);
System.out.println(b);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

}
}
return axa;
}

}
/**
 * 
 */
package com.belazy.gzip;

/**
 * @author consumerfed itsection
 * Phone : 0091 8281808029
 *
 */
public class SamplePojo {
private String data;

/**
* @return the data
*/
@Gzip(type="compression")
public String getData() {
return data;
}

/**
* @param data the data to set
*/
public void setData(String data) {
this.data = data;
}

}



Facebook comments