DeflateUtilities.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */


  20. package org.openspcoop2.utils.io;

  21. import java.io.ByteArrayOutputStream;
  22. import java.util.zip.DataFormatException;
  23. import java.util.zip.Deflater;
  24. import java.util.zip.Inflater;

  25. import org.openspcoop2.utils.UtilsException;

  26. /**
  27.  * DeflateUtilities
  28.  *
  29.  * @author Poli Andrea (apoli@link.it)
  30.  * @author $Author$
  31.  * @version $Rev$, $Date$
  32.  */
  33. public class DeflateUtilities {

  34.     private static final int BUFFER_SIZE = 1024;

  35.     public static byte[] decode(String data) throws UtilsException{
  36.         return decode(data.getBytes());
  37.     }
  38.     public static byte[] decode(byte[] data) throws UtilsException{
  39.         ByteArrayOutputStream baos = null;

  40.         try {
  41.             Deflater d = new Deflater(Deflater.BEST_COMPRESSION);
  42.             d.setInput(data);
  43.             d.finish();
  44.    
  45.             byte[] bytesCompressed = new byte[BUFFER_SIZE];
  46.    
  47.             int deflate = d.deflate(bytesCompressed);
  48.             baos = new ByteArrayOutputStream();
  49.    
  50.             while(deflate > 0) {
  51.                 baos.write(bytesCompressed, 0 ,deflate);
  52.                 deflate = d.deflate(bytesCompressed);
  53.             }
  54.    
  55.             return baos.toByteArray();
  56.         } finally {
  57.             if(baos != null) {
  58.                 try {baos.flush();} catch(Exception e) {}
  59.                 try {baos.close();} catch(Exception e) {}
  60.             }
  61.         }

  62.     }

  63.     public static byte[] encode(byte [] data) throws UtilsException{
  64.         ByteArrayOutputStream baos = null;
  65.         try {
  66.             Inflater inflater = new Inflater();
  67.             inflater.setInput(data);
  68.             byte[] result = new byte[BUFFER_SIZE];
  69.             int inflate = inflater.inflate(result);
  70.             baos = new ByteArrayOutputStream();
  71.             while(inflate > 0) {
  72.                 baos.write(result, 0, inflate);
  73.                 inflate = inflater.inflate(result);
  74.             }
  75.             inflater.end();
  76.             return baos.toByteArray();
  77.         } catch (DataFormatException e) {
  78.             throw new UtilsException(e);
  79.         } finally {
  80.             if(baos != null) {
  81.                 try {baos.flush();} catch(Exception e) {}
  82.                 try {baos.close();} catch(Exception e) {}
  83.             }
  84.         }
  85.     }
  86.    
  87.     public static String encodeAsString(byte [] data) throws UtilsException{
  88.         return new String(encode(data));
  89.     }

  90. }