CharsetUtilities.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.resources;

  21. import java.io.ByteArrayInputStream;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import java.io.InputStreamReader;

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

  33.     public static void main(String [] args) throws Exception {
  34.        
  35.         /*
  36.          IMPORTAMTE:
  37.        
  38.            JVM caches value of default character encoding once JVM starts and
  39.            so is the case for default constructors of InputStreamReader and other core Java classes.
  40.            So calling System.setProperty("file.encoding" , "UTF-16") may not have desire effect.

  41.            Read more: https://javarevisited.blogspot.com/2012/01/get-set-default-character-encoding.html#ixzz5pxOQjcal
  42.          * */

  43.        
  44.         System.out.println("defaultCharacterEncoding by property: " + getDefaultCharsetByProperties());
  45.         System.out.println("defaultCharacterEncoding by code: " + getDefaultCharsetByCode());
  46.         System.out.println("defaultCharacterEncoding by charSet: " + getDefaultCharsetByCharset());
  47.        
  48.         // Quanto spiegato sopra si può vedere modificando il charset e si vedra che gli altri due metodi 'ByCode' e 'ByCharset' continuano ad utilizzare il charset originale
  49.        
  50.         System.setProperty("file.encoding" , "ISO-8859-1");
  51.        
  52.         System.out.println("defaultCharacterEncoding by property: " + getDefaultCharsetByProperties());
  53.         System.out.println("defaultCharacterEncoding by code: " + getDefaultCharsetByCode());
  54.         System.out.println("defaultCharacterEncoding by charSet: " + getDefaultCharsetByCharset());
  55.     }
  56.    
  57.     public static String getDefaultCharsetByProperties() {
  58.         String defaultCharacterEncoding = System.getProperty("file.encoding");
  59.         return defaultCharacterEncoding;
  60.     }
  61.    
  62.     public static String getDefaultCharsetByCode() throws IOException {
  63.         byte [] bArray = {'w'};
  64.         try(
  65.             InputStream is = new ByteArrayInputStream(bArray);
  66.             InputStreamReader reader = new InputStreamReader(is);
  67.             ){      
  68.             String defaultCharacterEncoding = reader.getEncoding();
  69.             return defaultCharacterEncoding;
  70.         }

  71.     }
  72.    
  73.     public static String getDefaultCharsetByCharset() {
  74.         return java.nio.charset.Charset.defaultCharset().toString();
  75.     }
  76. }