IOUtilities.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.generic_project.io;

  21. import java.io.ByteArrayOutputStream;
  22. import java.io.File;
  23. import java.io.FileInputStream;
  24. import java.io.FileOutputStream;
  25. import java.io.IOException;
  26. import java.io.InputStream;
  27. import java.io.OutputStream;

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

  36.     public static void copy(String in, String out)
  37.     throws IOException {
  38.         FileInputStream fin = null;
  39.         FileOutputStream fout = null;
  40.         try{
  41.             fin = new FileInputStream(in);
  42.             fout = new FileOutputStream(out);
  43.             IOUtilities.copy(fin,fout);
  44.             fout.flush();
  45.         }finally{
  46.             try{
  47.                 if(fin!=null)
  48.                     fin.close();
  49.             }catch(Exception e){}
  50.             try{
  51.                 if(fout!=null)
  52.                     fout.close();
  53.             }catch(Exception e){
  54.                 // close
  55.             }
  56.         }
  57.     }

  58.     public static void copy(InputStream in, OutputStream out)
  59.     throws IOException {

  60.         byte[] buffer = new byte[256];
  61.         while (true) {
  62.             int bytesRead = in.read(buffer);
  63.             if (bytesRead == -1) break;
  64.             out.write(buffer, 0, bytesRead);
  65.         }

  66.     }

  67.     public static void copyDirectory(String srcPath, String dstPath)
  68.     throws IOException{
  69.         IOUtilities.copyDirectory(new File(srcPath), new File(dstPath));
  70.     }
  71.     public static void copyDirectory(File srcPath, File dstPath)
  72.     throws IOException{

  73.         if (srcPath.isDirectory()){
  74.             if (!dstPath.exists()){
  75.                 dstPath.mkdir();
  76.             }

  77.             String files[] = srcPath.list();

  78.             if(files!=null) {
  79.                 for(int i = 0; i < files.length; i++){
  80.                         IOUtilities.copyDirectory(new File(srcPath, files[i]),
  81.                                 new File(dstPath, files[i]));
  82.                 }
  83.             }
  84.         }
  85.         else{
  86.             if(!srcPath.exists()){
  87.                 //System.out.println("File or directory does not exist.");
  88.                 return;
  89.             }
  90.             else {
  91.                 InputStream in = null;
  92.                 OutputStream out = null;
  93.                 try {
  94.                     in = new FileInputStream(srcPath);
  95.                     out = new FileOutputStream(dstPath);
  96.                     // Transfer bytes from in to out
  97.                     byte[] buf = new byte[1024];
  98.                     int len;
  99.                     while ((len = in.read(buf)) > 0) {
  100.                         out.write(buf, 0, len);
  101.                     }
  102.                     out.flush();
  103.                 }finally {
  104.                     try {
  105.                         if(in!=null) {
  106.                             in.close();
  107.                         }
  108.                     }catch(Throwable t) {
  109.                         // ignore
  110.                     }
  111.                     try {
  112.                         if(out!=null) {
  113.                             out.close();
  114.                         }
  115.                     }catch(Throwable t) {
  116.                         // ignore
  117.                     }
  118.                 }
  119.             }
  120.         }
  121.         //System.out.println("Directory copied.");
  122.     }
  123.    
  124.    
  125.     public static String readFile(File f) throws Exception{
  126.         FileInputStream fis = null;
  127.         ByteArrayOutputStream byteInputBuffer = null;
  128.         try {
  129.             fis = new FileInputStream(f);
  130.             byteInputBuffer = new ByteArrayOutputStream();
  131.             byte [] readB = new byte[8192];
  132.             int readByte = 0;
  133.             while((readByte = fis.read(readB))!= -1){
  134.                 byteInputBuffer.write(readB,0,readByte);
  135.             }
  136.             byteInputBuffer.flush();
  137.         }finally {
  138.             try {
  139.                 if(fis!=null) {
  140.                     fis.close();
  141.                 }
  142.             }catch(Throwable t) {
  143.                 // ignore
  144.             }
  145.             try {
  146.                 if(byteInputBuffer!=null) {
  147.                     byteInputBuffer.close();
  148.                 }
  149.             }catch(Throwable t) {
  150.                 // ignore
  151.             }
  152.         }
  153.        
  154.         return  byteInputBuffer.toString();
  155.     }
  156.     public static byte[] readBytesFromFile(File f) throws Exception{
  157.         FileInputStream fis = null;
  158.         ByteArrayOutputStream byteInputBuffer = null;
  159.         try {
  160.             fis =new FileInputStream(f);
  161.             byteInputBuffer = new ByteArrayOutputStream();
  162.             byte [] readB = new byte[8192];
  163.             int readByte = 0;
  164.             while((readByte = fis.read(readB))!= -1){
  165.                 byteInputBuffer.write(readB,0,readByte);
  166.             }
  167.             byteInputBuffer.flush();
  168.         }finally {
  169.             try {
  170.                 if(fis!=null) {
  171.                     fis.close();
  172.                 }
  173.             }catch(Throwable t) {
  174.                 // ignore
  175.             }
  176.             try {
  177.                 if(byteInputBuffer!=null) {
  178.                     byteInputBuffer.close();
  179.                 }
  180.             }catch(Throwable t) {
  181.                 // ignore
  182.             }
  183.         }
  184.        
  185.         return  byteInputBuffer.toByteArray();
  186.     }
  187.    
  188.    
  189.     public static void writeFile(File f,byte[] contenuto)throws Exception{
  190.         FileOutputStream fos = null;
  191.         try {
  192.             fos = new FileOutputStream(f);
  193.             fos.write(contenuto);
  194.             fos.flush();
  195.         }finally {
  196.             try {
  197.                 if(fos!=null) {
  198.                     fos.close();
  199.                 }
  200.             }catch(Throwable t) {
  201.                 // ignore
  202.             }
  203.         }
  204.     }
  205.     public static void writeFile(File f,byte[] ... args)throws Exception{
  206.         if(args!=null){
  207.             FileOutputStream fos = null;
  208.             try {
  209.                 fos = new FileOutputStream(f);
  210.                 for(int i=0; i<args.length; i++){
  211.                     fos.write(args[i]);
  212.                 }
  213.                 fos.flush();
  214.             }finally {
  215.                 try {
  216.                     if(fos!=null) {
  217.                         fos.close();
  218.                     }
  219.                 }catch(Throwable t) {
  220.                     // ignore
  221.                 }
  222.             }
  223.         }
  224.     }
  225.    
  226.     public static void copyFileAndReplaceAllKeywords(File read,File write,String keyword,String values) throws Exception{
  227.         String[]k = new String[1];
  228.         k[0] = keyword;
  229.         String[]v = new String[1];
  230.         v[0] = values;
  231.         IOUtilities.copyFileAndReplaceAllKeywords(read, write, k, v);
  232.     }
  233.     /*public static void copyFileAndReplaceAllKeywords(File read,File write,String[] keyword,String[] values) throws Exception{
  234.         String file = IOUtilities.readFile(read);
  235.         for(int i=0; i<keyword.length; i++){
  236.            
  237.             while(file.contains(keyword[i])){
  238.                 file = file.replace(keyword[i], values[i]);
  239.             }
  240.         }
  241.         IOUtilities.writeFile(write, file.getBytes());
  242.     }*/
  243.     public static void copyFileAndReplaceAllKeywords(File read,File write,String[] keyword,String[] values) throws Exception{
  244.         String file = IOUtilities.readFile(read);
  245.         for(int i=0; i<keyword.length; i++){
  246.            
  247.             //System.out.println("FILE ["+file+"] contains ["+keyword[i]+"] (value:"+file.indexOf(keyword[i])+")");
  248.             int indexOf = file.indexOf(keyword[i]);
  249.             while(indexOf>=0){
  250.                 //System.out.println("REPLACE ["+keyword[i]+"] ["+values[i]+"]");
  251.                 file = file.replace(keyword[i], values[i]);
  252.                 indexOf = file.indexOf(keyword[i],indexOf+values[i].length());
  253.                 //System.out.println("INDEX OF ["+indexOf+"]");
  254.             }
  255.         }
  256.         IOUtilities.writeFile(write, file.getBytes());
  257.     }
  258.    
  259.     public static void copyFileAndReplaceKeywords(File read,File write,String keyword,String values) throws Exception{
  260.         String[]k = new String[1];
  261.         k[0] = keyword;
  262.         String[]v = new String[1];
  263.         v[0] = values;
  264.         IOUtilities.copyFileAndReplaceKeywords(read, write, k, v);
  265.     }
  266.     public static void copyFileAndReplaceKeywords(File read,File write,String[] keyword,String[] values) throws Exception{
  267.         String file = IOUtilities.readFile(read);
  268.         for(int i=0; i<keyword.length; i++){
  269.             file = file.replace(keyword[i], values[i]);
  270.         }
  271.         IOUtilities.writeFile(write, file.getBytes());
  272.     }


  273.    
  274.    
  275.     public static boolean deleteDir(File dir) {
  276.          if (dir.isDirectory()) {
  277.              String[] children = dir.list();
  278.              if(children!=null) {
  279.                  for (int i=0; i<children.length; i++) {
  280.                      boolean success = IOUtilities.deleteDir(new File(dir, children[i]));
  281.                      if (!success) {
  282.                          return false;
  283.                      }
  284.                  }
  285.              }
  286.          }

  287.          // The directory is now empty so now it can be smoked
  288.          boolean delete = dir.delete();
  289.          return delete;
  290.     }

  291.    
  292.     public static void moveToDir(String src,String destDir) throws Exception{
  293.          // File (or directory) to be moved
  294.         File file = new File(src);
  295.        
  296.         // Destination directory
  297.         File dir = new File(destDir);
  298.        
  299.         IOUtilities.moveToDir(file,dir);
  300.     }
  301.     public static void moveToDir(File src,File destDir) throws Exception{

  302.         // Move file to new directory
  303.         IOUtilities.copy(src.getAbsolutePath(), destDir.getAbsolutePath()+File.separatorChar+src.getName());
  304.         if(src.isDirectory()){
  305.             if(IOUtilities.deleteDir(src)==false){
  306.                 throw new Exception("Directory ["+src.getAbsolutePath()+"] non eliminabile");
  307.             }
  308.         }else{
  309.             if(src.delete()==false){
  310.                 throw new Exception("File ["+src.getAbsolutePath()+"] non eliminabile");
  311.             }
  312.         }
  313.     }
  314.    
  315.     public static void moveToFile(String src,String destFile)throws Exception{
  316.      // File (or directory) to be moved
  317.        File file = new File(src);
  318.        
  319.        // Destination directory
  320.        File dir = new File(destFile);
  321.        
  322.        IOUtilities.moveToFile(file,dir);
  323.    }
  324.    public static void moveToFile(File src,File destFile)throws Exception{

  325.        // Move file to new directory
  326.        IOUtilities.copy(src.getAbsolutePath(), destFile.getAbsolutePath());
  327.        if(src.isDirectory()){
  328.             if(IOUtilities.deleteDir(src)==false){
  329.                 throw new Exception("Directory ["+src.getAbsolutePath()+"] non eliminabile");
  330.             }
  331.         }else{
  332.             if(src.delete()==false){
  333.                 throw new Exception("File ["+src.getAbsolutePath()+"] non eliminabile");
  334.             }
  335.         }
  336.    }
  337.    
  338.    
  339.    
  340.    public static void mkdirParentDirectory(String file) throws Exception {
  341.         try{
  342.             File p = new File(file);
  343.             if(p.getParentFile()==null){
  344.                 return;
  345.             }
  346.             if(p.getParentFile().exists()){
  347.                 return;
  348.             }
  349.             IOUtilities.mkdirParentDirectory(p.getParentFile().getAbsolutePath());
  350.             if(p.getParentFile().mkdir()==false){
  351.                 throw new Exception("Directory ["+p.getParentFile().getAbsolutePath()+"] non esistente e creazione non riuscita");
  352.             }
  353.         }catch(Exception e){
  354.             throw new Exception("mkdirParentDirectory non riuscito: "+e.getMessage(),e);
  355.         }
  356.     }
  357. }