001    package jcolibri.method.retrieve.NNretrieval.similarity.local.textual.compressionbased;
002    
003    import java.io.BufferedOutputStream;
004    import java.io.ByteArrayOutputStream;
005    import java.util.zip.GZIPOutputStream;
006    
007    /**
008     * Class that implements the ICompressor interface, i.e. it defines a function
009     * for returning the size of an object (in this case, a String) after
010     * compression (in this casse, by GZip).
011     * 
012     * @author Derek Bridge 18/05/07
013     * 
014     */
015    public class GZipCompressor implements ICompressor {
016            /**
017             * Returns the size of a string after compression by GZip.
018             * 
019             * @param s
020             *            the string to be compressed
021             * @return the size of the string after compression by GZip
022             */
023            public int getCompressedSize(String s) {
024    
025                    ByteArrayOutputStream baos = null;
026                    BufferedOutputStream out = null;
027                    try {
028                            baos = new ByteArrayOutputStream();
029                            out = new BufferedOutputStream(new GZIPOutputStream(baos));
030                            byte[] bytes = s.getBytes();
031                            for (int i = 0; i < bytes.length; i++) {
032                                    out.write(bytes[i]);
033                            }
034                            out.flush();
035                    } catch (Exception e) {
036    
037                    }
038                    try {
039                            if (out != null)
040                                    out.close();
041                    } catch (Exception e) {
042                    }
043                    return baos.size();
044                    
045            }
046    }