CopyStream.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;

  21. import java.io.File;
  22. import java.io.FileInputStream;
  23. import java.io.FileOutputStream;
  24. import java.io.InputStream;
  25. import java.io.OutputStream;
  26. import java.nio.ByteBuffer;
  27. import java.nio.channels.Channels;
  28. import java.nio.channels.ReadableByteChannel;
  29. import java.nio.channels.WritableByteChannel;
  30. import java.nio.file.CopyOption;
  31. import java.nio.file.Files;

  32. import org.apache.commons.io.IOUtils;

  33. import com.google.common.io.ByteStreams;

  34. /**
  35.  * Libreria contenente metodi per copiare gli stream
  36.  *
  37.  *
  38.  * @author Poli Andrea (apoli@link.it)
  39.  * @author $Author$
  40.  * @version $Rev$, $Date$
  41.  */
  42. public class CopyStream {
  43.    
  44.     /** Timeout Utilities */
  45.     public static InputStream buildTimeoutInputStream(InputStream isParam, int timeout) throws UtilsException {
  46.         InputStream is = isParam;
  47.         if(timeout>0 && !(is instanceof TimeoutInputStream)) {
  48.             try {
  49.                 is = new TimeoutInputStream(isParam, timeout);
  50.             }catch(Exception e) {
  51.                 throw new UtilsException(e.getMessage(),e);
  52.             }
  53.         }
  54.         return is;
  55.     }
  56.    
  57.     /** Limited Utilities */
  58.     public static InputStream buildLimitedInputStream(InputStream isParam, long limitBytes) throws UtilsException {
  59.         InputStream is = isParam;
  60.         if(limitBytes>0 && !(is instanceof LimitedInputStream)) {
  61.             try {
  62.                 is = new LimitedInputStream(isParam, limitBytes);
  63.             }catch(Exception e) {
  64.                 throw new UtilsException(e.getMessage(),e);
  65.             }
  66.         }
  67.         return is;
  68.     }
  69.    
  70.    
  71.     /** Copy Stream */
  72.    
  73.     public static void copy(InputStream is,OutputStream os) throws UtilsException{
  74.         copy(CopyStreamMethod.AUTO, is, os, -1, -1);
  75.     }
  76.     public static void copy(CopyStreamMethod method, InputStream is,OutputStream os) throws UtilsException{
  77.         copy(method, is, os, -1, -1);
  78.     }
  79.    
  80.     public static void copy(InputStream is,OutputStream os, int timeout) throws UtilsException{
  81.         copy(CopyStreamMethod.AUTO, is, os, timeout);
  82.     }
  83.     public static void copy(CopyStreamMethod method, InputStream isParam,OutputStream os, int timeout) throws UtilsException{
  84.         copy(method, isParam, os, timeout, -1);
  85.     }
  86.    
  87.     public static void copy(InputStream is,OutputStream os, long limitBytes) throws UtilsException{
  88.         copy(CopyStreamMethod.AUTO, is, os, limitBytes);
  89.     }
  90.     public static void copy(CopyStreamMethod method, InputStream isParam,OutputStream os, long limitBytes) throws UtilsException{
  91.         copy(method, isParam, os, -1, limitBytes);
  92.     }
  93.    
  94.     public static void copy(InputStream isParam,OutputStream os, int timeout, long limitBytes) throws UtilsException{
  95.         copy(CopyStreamMethod.AUTO, isParam, os, timeout, limitBytes);
  96.     }
  97.     public static void copy(CopyStreamMethod method, InputStream isParam,OutputStream os, int timeout, long limitBytes) throws UtilsException{
  98.        
  99.         InputStream is = isParam;
  100.         if(limitBytes>0) {
  101.             is = buildLimitedInputStream(is, limitBytes);
  102.         }
  103.         if(timeout>0) {
  104.             is = buildTimeoutInputStream(is, timeout);
  105.         }
  106.        
  107.         switch (method) {
  108.         case JAVA:
  109.             copyBuffer(is,os);
  110.             break;
  111.         case JAVA_TRANSFER_TO:
  112.             transferTo(is,os);
  113.             break;
  114.         case JAVA_NIO:
  115.             copyChannels(is,os);
  116.             break;
  117.         case GUAVA:
  118.             copyGuava(is,os);
  119.             break;
  120.         case COMMONS_IO:
  121.             copyCommonsIO(is,os);
  122.             break;
  123.         case AUTO:
  124.             boolean timeoutInputStream = false;
  125.             InputStream checkIs = is;
  126.             OutputStream checkOs = os;
  127.             if(is instanceof TimeoutInputStream) {
  128.                 checkIs = ((TimeoutInputStream) is).getIsWrapped();
  129.                 timeoutInputStream = true;
  130.             }
  131.             if(checkOs instanceof FileOutputStream) {
  132.                 //System.out.println("CHANNEL");
  133.                 copyChannels(is,os);    
  134.             }
  135.             else if(checkIs instanceof FileInputStream) {
  136.                 if(timeoutInputStream) {
  137.                     //System.out.println("TRANSFER");
  138.                     transferTo(is,os);
  139.                 }
  140.                 else {
  141.                     //System.out.println("CHANNEL");
  142.                     copyChannels(is,os);
  143.                 }
  144.             }
  145.             else {
  146.                 //System.out.println("TRANSFER");
  147.                 transferTo(is,os);
  148.             }
  149.             break;
  150.         }
  151.     }
  152.    
  153.     public static void copyBuffer(InputStream is,OutputStream os) throws UtilsException{
  154.         copyBuffer(is,os, Utilities.DIMENSIONE_BUFFER);
  155.     }
  156.     public static void copyBuffer(InputStream is,OutputStream os, int sizeBuffer) throws UtilsException{
  157.         try{
  158.             byte [] buffer = new byte[sizeBuffer];
  159.             int letti = 0;
  160.             while( (letti=is.read(buffer)) != -1 ){
  161.                 os.write(buffer, 0, letti);
  162.             }
  163.             os.flush();
  164.         }catch(Exception e){
  165.             throw new UtilsException(e.getMessage(),e);
  166.         }
  167.     }
  168.    
  169.     public static void transferTo(InputStream is,OutputStream os) throws UtilsException{
  170.         try{
  171.             is.transferTo(os);
  172.         }catch(Exception e){
  173.             throw new UtilsException(e.getMessage(),e);
  174.         }
  175.     }
  176.    
  177.     public static void copyGuava(InputStream is,OutputStream os) throws UtilsException{
  178.         try{
  179.             ByteStreams.copy(is, os);
  180.         }catch(Exception e){
  181.             throw new UtilsException(e.getMessage(),e);
  182.         }
  183.     }
  184.    
  185.     public static void copyCommonsIO(InputStream is,OutputStream os) throws UtilsException{
  186.         try{
  187.             IOUtils.copy(is, os);
  188.         }catch(Exception e){
  189.             throw new UtilsException(e.getMessage(),e);
  190.         }
  191.     }
  192.    
  193.     public static void copyChannels(final InputStream src, final OutputStream dest) throws UtilsException {
  194.         final ReadableByteChannel inputChannel = Channels.newChannel(src);
  195.         final WritableByteChannel outputChannel = Channels.newChannel(dest);
  196.         copyChannels(inputChannel, outputChannel);
  197.     }
  198.     public static void copyChannels(final ReadableByteChannel src, final WritableByteChannel dest) throws UtilsException {
  199.         try{
  200.             final ByteBuffer buffer = ByteBuffer.allocateDirect(Utilities.DIMENSIONE_BUFFER);
  201.                
  202.             while(src.read(buffer) != -1) {
  203.                 buffer.flip();
  204.                 dest.write(buffer);
  205.                 buffer.compact();
  206.             }
  207.                
  208.             buffer.flip();
  209.            
  210.             while(buffer.hasRemaining()) {
  211.                 dest.write(buffer);
  212.             }
  213.         }catch(Exception e){
  214.             throw new UtilsException(e.getMessage(),e);
  215.         }
  216.     }
  217.    
  218.     public static void copy(File from,OutputStream os) throws UtilsException{
  219.         try{
  220.             Files.copy(from.toPath(), os);
  221.         }catch(Exception e){
  222.             throw new UtilsException(e.getMessage(),e);
  223.         }
  224.     }
  225.     public static void copy(File from,File to) throws UtilsException{
  226.         try{
  227.             Files.copy(from.toPath(), to.toPath());
  228.         }catch(Exception e){
  229.             throw new UtilsException(e.getMessage(),e);
  230.         }
  231.     }
  232.     public static void copy(File from,File to, CopyOption ... options) throws UtilsException{
  233.         try{
  234.             Files.copy(from.toPath(), to.toPath(), options);
  235.         }catch(Exception e){
  236.             throw new UtilsException(e.getMessage(),e);
  237.         }
  238.     }
  239.     public static void copy(InputStream from, File to) throws UtilsException{
  240.         copy(from, to, -1, -1);
  241.     }
  242.     public static void copy(InputStream from, File to, int timeout) throws UtilsException{
  243.         copy(from, to, timeout, -1);
  244.     }
  245.     public static void copy(InputStream from, File to, long limitBytes) throws UtilsException{
  246.         copy(from, to, -1, limitBytes);
  247.     }
  248.     public static void copy(InputStream from, File to, int timeout, long limitBytes) throws UtilsException{
  249.         try{
  250.             InputStream is = from;
  251.             if(limitBytes>0) {
  252.                 is = buildLimitedInputStream(is, limitBytes);
  253.             }
  254.             if(timeout>0) {
  255.                 is = buildTimeoutInputStream(is, timeout);
  256.             }
  257.             Files.copy(is, to.toPath());
  258.         }catch(Exception e){
  259.             throw new UtilsException(e.getMessage(),e);
  260.         }
  261.     }
  262.     public static void copy(InputStream from, File to, CopyOption ... options) throws UtilsException{
  263.         copy(from, to, -1, -1, options);
  264.     }
  265.     public static void copy(InputStream from, File to, int timeout, CopyOption ... options) throws UtilsException{
  266.         copy(from, to, timeout, -1, options);
  267.     }
  268.     public static void copy(InputStream from, File to, long limitBytes, CopyOption ... options) throws UtilsException{
  269.         copy(from, to, -1, limitBytes, options);
  270.     }
  271.     public static void copy(InputStream from, File to, int timeout, long limitBytes, CopyOption ... options) throws UtilsException{
  272.         try{
  273.             InputStream is = from;
  274.             if(limitBytes>0) {
  275.                 is = buildLimitedInputStream(is, limitBytes);
  276.             }
  277.             if(timeout>0) {
  278.                 is = buildTimeoutInputStream(is, timeout);
  279.             }
  280.             Files.copy(is, to.toPath(), options);
  281.         }catch(Exception e){
  282.             throw new UtilsException(e.getMessage(),e);
  283.         }
  284.     }

  285.    
  286. }