Piotr Gabryanczyk’s Blog

Java, Refactoring, AOP, Spring, DDD, TDD, etc.

  • Blogroll

    • I have joined Anti-IF Campaign

Archive for June, 2009

How to download JavaDocs or sources in Maven 2?

Posted by Piotr Gabryanczyk on June 25, 2009

Problem

You want to download javadocs for your depenencies into local repository to be able to use them ie. in IntelliJ.

Solution


mvn dependency:resolve -Dclassifier=javadoc
mvn dependency:resolve -Dclassifier=sources

Enjoy!

Advertisements

Posted in java | Tagged: | 4 Comments »

Howto compress or decompress byte array in Java?

Posted by Piotr Gabryanczyk on June 8, 2009

Check this class out:

Compressor.java

import java.io.*;
import java.util.zip.*;

import org.apache.commons.io.IOUtils;

public class Compressor{
    public static byte[] compress(byte[] content){
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        try{
            GZIPOutputStream gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream);
            gzipOutputStream.write(content);
            gzipOutputStream.close();
        } catch(IOException e){
            throw new RuntimeException(e);
        }
        System.out.printf("Compression ratio %f\n", (1.0f * content.length/byteArrayOutputStream.size()));
        return byteArrayOutputStream.toByteArray();
    }

    public static byte[] decompress(byte[] contentBytes){
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try{
            IOUtils.copy(new GZIPInputStream(new ByteArrayInputStream(contentBytes)), out);
        } catch(IOException e){
            throw new RuntimeException(e);
        }
        return out.toByteArray();
    }

    public static boolean notWorthCompressing(String contentType){
        return contentType.contains("jpeg")
                || contentType.contains("pdf")
                || contentType.contains("zip")
                || contentType.contains("mpeg")
                || contentType.contains("avi");
    }
}

Dependencies: Apache commons-io.jar

Posted in Uncategorized | 12 Comments »