Base64Utilities.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 org.apache.commons.codec.binary.StringUtils;

  22. /**
  23.  * Base64Utilities
  24.  *
  25.  * @author Poli Andrea (apoli@link.it)
  26.  * @author $Author$
  27.  * @version $Rev$, $Date$
  28.  */
  29. public class Base64Utilities {

  30.     /**
  31.      * Characters for Base64 transformation.
  32.      */
  33.     public static final String B64_STRING = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

  34.     /**
  35.      * Characters for Base64 transformation.
  36.      */
  37.     public static final char[] B64_ARRAY = B64_STRING.toCharArray();
  38.    
  39.    
  40.     public static byte[] decode(byte [] data){
  41.         return org.apache.commons.codec.binary.Base64.decodeBase64(data);
  42.     }
  43.     public static byte[] decode(String data){
  44.         return org.apache.commons.codec.binary.Base64.decodeBase64(data);
  45.     }
  46.    
  47.     public static byte[] encode(byte [] data){
  48.         return org.apache.commons.codec.binary.Base64.encodeBase64(data);
  49.     }
  50.     public static byte[] encodeChunked(byte [] data){
  51.         return org.apache.commons.codec.binary.Base64.encodeBase64Chunked(data);
  52.     }
  53.     public static byte[] encode(byte [] data, boolean isChunked){
  54.         return org.apache.commons.codec.binary.Base64.encodeBase64(data, isChunked);
  55.     }
  56.     public static byte[] encode(byte [] data, boolean isChunked, boolean urlSafe){
  57.         return org.apache.commons.codec.binary.Base64.encodeBase64(data, isChunked, urlSafe);
  58.     }
  59.     public static byte[] encode(byte [] data, boolean isChunked, boolean urlSafe, int maxResultSize){
  60.         return org.apache.commons.codec.binary.Base64.encodeBase64(data, isChunked, urlSafe, maxResultSize);
  61.     }
  62.    
  63.     public static String encodeAsString(byte [] data){
  64.         return org.apache.commons.codec.binary.Base64.encodeBase64String(data);
  65.     }
  66.     public static String encodeChunkedAsString(byte [] data){
  67.         return StringUtils.newStringUsAscii(encodeChunked(data));
  68.     }
  69.     public static String encodeBase64URLSafeString(byte [] data){
  70.         return org.apache.commons.codec.binary.Base64.encodeBase64URLSafeString(data);
  71.     }
  72.     public static String encodeAsString(byte [] data, boolean isChunked){
  73.         return StringUtils.newStringUsAscii(encode(data, isChunked));
  74.     }
  75.     public static String encodeAsString(byte [] data, boolean isChunked, boolean urlSafe){
  76.         return StringUtils.newStringUsAscii(encode(data, isChunked, urlSafe));
  77.     }
  78.     public static String encodeAsString(byte [] data, boolean isChunked, boolean urlSafe, int maxResultSize){
  79.         return StringUtils.newStringUsAscii(encode(data, isChunked, urlSafe, maxResultSize));
  80.     }
  81.    
  82. }