CopyCharStream.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.OutputStream;
  23. import java.io.OutputStreamWriter;
  24. import java.io.Reader;
  25. import java.io.Writer;
  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 org.apache.commons.io.input.ReaderInputStream;
  34. import org.apache.commons.io.output.WriterOutputStream;
  35. import org.apache.commons.lang.CharEncoding;

  36. import com.google.common.io.CharStreams;

  37. /**
  38.  * Libreria contenente metodi per copiare gli stream di stringhe (Reader e Writer)
  39.  *
  40.  *
  41.  * @author Poli Andrea (apoli@link.it)
  42.  * @author $Author$
  43.  * @version $Rev$, $Date$
  44.  */
  45. public class CopyCharStream {


  46.    
  47.     /** Copy Sring */
  48.    
  49.     public static void copy(Reader reader, OutputStream out) throws UtilsException{
  50.         copy(CopyStreamMethod.AUTO, reader, out);
  51.     }
  52.     public static void copy(CopyStreamMethod method, Reader reader, OutputStream out) throws UtilsException{
  53.         try {
  54.             Writer writer = new OutputStreamWriter(out);
  55.             CopyCharStream.copy(method, reader, writer);
  56.             out.flush();
  57.             writer.close();
  58.         }catch(Exception e) {
  59.             throw new UtilsException(e.getMessage(),e);
  60.         }
  61.     }
  62.    
  63.     public static void copy(Reader reader, Writer writer) throws UtilsException{
  64.         copy(CopyStreamMethod.AUTO, reader, writer);
  65.     }
  66.     public static void copy(CopyStreamMethod method, Reader reader,Writer writer) throws UtilsException{
  67.         switch (method) {
  68.         case JAVA:
  69.             copyBuffer(reader,writer);
  70.             break;
  71.         case JAVA_TRANSFER_TO:
  72.             transferTo(reader,writer);
  73.             break;
  74.         case JAVA_NIO:
  75.             copyChannels(reader,writer);
  76.             break;
  77.         case GUAVA:
  78.             copyGuava(reader,writer);
  79.             break;
  80.         case COMMONS_IO:
  81.             copyCommonsIO(reader,writer);
  82.             break;
  83.         case AUTO:
  84. //          if(reader instanceof java.io.FileReader || writer instanceof java.io.FileWriter) {
  85. //              copyChannels(reader,writer);    
  86. //          }
  87. //          else {
  88. //              transferTo(reader,writer);
  89. //          }
  90.             // Nel caso di char e' sempre piu' efficiente il transferTo
  91.             transferTo(reader,writer);
  92.             break;
  93.         }
  94.     }
  95.    
  96.     public static void copyBuffer(Reader reader,Writer writer) throws UtilsException{
  97.         try{
  98.             char [] buffer = new char[Utilities.DIMENSIONE_BUFFER];
  99.             int letti = 0;
  100.             while( (letti=reader.read(buffer)) != -1 ){
  101.                 writer.write(buffer, 0, letti);
  102.             }
  103.             writer.flush();
  104.         }catch(Exception e){
  105.             throw new UtilsException(e.getMessage(),e);
  106.         }
  107.     }
  108.    
  109.     public static void transferTo(Reader reader,Writer writer) throws UtilsException{
  110.         try{
  111.             reader.transferTo(writer);
  112.         }catch(Exception e){
  113.             throw new UtilsException(e.getMessage(),e);
  114.         }
  115.     }
  116.    
  117.     public static void copyGuava(Reader reader,Writer writer) throws UtilsException{
  118.         try{
  119.             CharStreams.copy(reader, writer);
  120.         }catch(Exception e){
  121.             throw new UtilsException(e.getMessage(),e);
  122.         }
  123.     }
  124.    
  125.     public static void copyCommonsIO(Reader reader,Writer writer) throws UtilsException{
  126.         try{
  127.             IOUtils.copy(reader, writer);
  128.         }catch(Exception e){
  129.             throw new UtilsException(e.getMessage(),e);
  130.         }
  131.     }
  132.    
  133.     public static void copyChannels(final Reader reader, final Writer writer) throws UtilsException {
  134.         try {
  135.             try(
  136.                     ReaderInputStream src = ReaderInputStream.builder()
  137.                                .setReader(reader)
  138.                                .setCharset(CharEncoding.UTF_8)
  139.                                .get();
  140.                     WriterOutputStream dest = WriterOutputStream.builder()
  141.                                 .setWriter(writer)
  142.                                 .setCharset(CharEncoding.UTF_8)
  143.                                 .get();
  144.                 ){
  145.             final ReadableByteChannel inputChannel = Channels.newChannel(src);
  146.             final WritableByteChannel outputChannel = Channels.newChannel(dest);
  147.             copyChannels(inputChannel, outputChannel);
  148.             }
  149.         }catch(Exception e) {
  150.             throw new UtilsException(e.getMessage(),e);
  151.         }
  152.     }
  153.     public static void copyChannels(final ReadableByteChannel src, final WritableByteChannel dest) throws UtilsException {
  154.         try{
  155.             final ByteBuffer buffer = ByteBuffer.allocateDirect(Utilities.DIMENSIONE_BUFFER);
  156.                
  157.             while(src.read(buffer) != -1) {
  158.                 buffer.flip();
  159.                 dest.write(buffer);
  160.                 buffer.compact();
  161.             }
  162.                
  163.             buffer.flip();
  164.            
  165.             while(buffer.hasRemaining()) {
  166.                 dest.write(buffer);
  167.             }
  168.         }catch(Exception e){
  169.             throw new UtilsException(e.getMessage(),e);
  170.         }
  171.     }
  172.    
  173.     public static void copy(File from,Writer writer) throws UtilsException{
  174.         try{
  175.             try(
  176.                     WriterOutputStream dest = WriterOutputStream.builder()
  177.                             .setWriter(writer)
  178.                             .setCharset(CharEncoding.UTF_8)
  179.                             .get();
  180.                 ){
  181.                 Files.copy(from.toPath(), dest);
  182.             }
  183.         }catch(Exception e){
  184.             throw new UtilsException(e.getMessage(),e);
  185.         }
  186.     }
  187.     public static void copy(File from,File to) throws UtilsException{
  188.         try{
  189.             Files.copy(from.toPath(), to.toPath());
  190.         }catch(Exception e){
  191.             throw new UtilsException(e.getMessage(),e);
  192.         }
  193.     }
  194.     public static void copy(File from,File to, CopyOption ... options) throws UtilsException{
  195.         try{
  196.             Files.copy(from.toPath(), to.toPath(), options);
  197.         }catch(Exception e){
  198.             throw new UtilsException(e.getMessage(),e);
  199.         }
  200.     }
  201.     public static void copy(Reader reader, File to) throws UtilsException{
  202.         try{
  203.             try(
  204.                     ReaderInputStream from = ReaderInputStream.builder()
  205.                        .setReader(reader)
  206.                        .setCharset(CharEncoding.UTF_8)
  207.                        .get();
  208.                 ){
  209.                 Files.copy(from, to.toPath());
  210.             }
  211.         }catch(Exception e){
  212.             throw new UtilsException(e.getMessage(),e);
  213.         }
  214.     }
  215.     public static void copy(Reader reader, File to, CopyOption ... options) throws UtilsException{
  216.         try{
  217.             try(
  218.                     ReaderInputStream from = ReaderInputStream.builder()
  219.                        .setReader(reader)
  220.                        .setCharset(CharEncoding.UTF_8)
  221.                        .get();
  222.                 ){
  223.                 Files.copy(from, to.toPath(), options);
  224.             }
  225.         }catch(Exception e){
  226.             throw new UtilsException(e.getMessage(),e);
  227.         }
  228.     }

  229. }