DumpByteArrayOutputStream.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 java.io.File;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import java.io.OutputStream;
  25. import java.io.UnsupportedEncodingException;
  26. import java.nio.charset.Charset;
  27. import java.text.SimpleDateFormat;
  28. import java.util.Date;

  29. import org.openspcoop2.utils.date.DateManager;
  30. import org.openspcoop2.utils.date.DateUtils;
  31. import org.openspcoop2.utils.resources.FileSystemUtilities;
  32. import org.openspcoop2.utils.resources.Loader;

  33. /**
  34. * DumpByteArrayOutputStream
  35. *
  36. * @author Andrea Poli (apoli@link.it)
  37.  * @author $Author$
  38.  * @version $Rev$, $Date$
  39. */
  40. public class DumpByteArrayOutputStream extends OutputStream {

  41.     private static String classImpl = DumpByteArrayOutputStream_FastImpl.class.getName();
  42.     public static String getClassImpl() {
  43.         return classImpl;
  44.     }
  45.     public static void setClassImpl(String classImpl) {
  46.         DumpByteArrayOutputStream.classImpl = classImpl;
  47.     }

  48.     private OutputStream impl;
  49.     private IDumpByteArrayOutputStream iImpl;
  50.    
  51.     public DumpByteArrayOutputStream() {
  52.         // non e' attivo alcun controllo di soglia
  53.         try {  
  54.             Loader l = new Loader();
  55.             this.impl = (OutputStream) l.newInstance_declaredConstructor(classImpl);
  56.             this.iImpl = (IDumpByteArrayOutputStream) this.impl;
  57.         }
  58.         catch(Exception e) {
  59.             throw new RuntimeException(e.getMessage(), e);
  60.         }
  61.     }
  62.     public DumpByteArrayOutputStream(int soglia, File repositoryFile, String idTransazione, String tipoMessaggio) {
  63.         try {
  64.             Loader l = new Loader();
  65.             Integer s = soglia;
  66.             if(s>0) {
  67.                 if(repositoryFile==null) {
  68.                     throw new Exception("RepositoryFile undefined");
  69.                 }
  70.                 this.impl = (OutputStream) l.newInstance_declaredConstructor(classImpl, s, repositoryFile,
  71.                         idTransazione!=null ? idTransazione : getUniqueSerial(),
  72.                         tipoMessaggio!=null ? tipoMessaggio : "dump");
  73.             }
  74.             else {
  75.                 // non e' attivo alcun controllo di soglia
  76.                 this.impl = (OutputStream) l.newInstance_declaredConstructor(classImpl);
  77.             }
  78.             this.iImpl = (IDumpByteArrayOutputStream) this.impl;
  79.         }
  80.         catch(Exception e) {
  81.             throw new RuntimeException(e.getMessage(), e);
  82.         }
  83.     }

  84.     public static DumpByteArrayOutputStream newInstance(byte[] content) {
  85.         DumpByteArrayOutputStream out = null;
  86.         if(content!=null && content.length>0) {
  87.             out = new DumpByteArrayOutputStream();
  88.             if(content!=null && content.length>0) {
  89.                 try {
  90.                     out.write(content);
  91.                 }catch(Exception e) {
  92.                     throw new RuntimeException(e.getMessage(), e);
  93.                 }
  94.             }
  95.         }
  96.         return out;
  97.     }
  98.    
  99.     private static long uniqueSerialNumber = 0;
  100.     private static synchronized long getUniqueSerialNumber(){
  101.         if((uniqueSerialNumber+1) > Long.MAX_VALUE){
  102.             uniqueSerialNumber = 0;
  103.         }
  104.         uniqueSerialNumber++;
  105.         return uniqueSerialNumber;
  106.     }
  107.     private static final String SIMPLE_DATE_FORMAT_MINUTE = "yyyyMMddHHmm";
  108.     private static String getUniqueSerial() {
  109.         return getUniqueSerialNumber()+""+DateUtils.getSimpleDateFormat(SIMPLE_DATE_FORMAT_MINUTE).format(DateManager.getDate());
  110.     }
  111.    
  112.     private static final String formatDir = "yyyyMMdd"; // compatibile con windows S.O.
  113.     private static final String formatNew = "yyyyMMdd_HHmmssSSS"; // compatibile con windows S.O.
  114.     private static final String prefix = "dump";
  115.     protected static File newFile(File repositoryFile, String tipoMessaggio, String idTransazione) throws Exception {
  116.         Date d = DateManager.getDate();
  117.         SimpleDateFormat dateformatDir = DateUtils.getDefaultDateTimeFormatter(formatDir);
  118.         String dateFormatDir = dateformatDir.format(d);
  119.         File fData = new File(repositoryFile, dateFormatDir);          
  120.        
  121.         SimpleDateFormat dateformat = DateUtils.getDefaultDateTimeFormatter(formatNew);
  122.         String dateFormat = dateformat.format(d);
  123.         String nomeFile = prefix+tipoMessaggio+"_"+dateFormat+"_"+idTransazione.replaceAll("-", "_")+".bin";
  124.         File f = new File(fData, nomeFile);
  125.         FileSystemUtilities.mkdirParentDirectory(f);
  126.         return f;
  127.     }
  128.    
  129.    
  130.     public boolean isSerializedOnFileSystem() {
  131.         return this.iImpl.isSerializedOnFileSystem();
  132.     }
  133.     public File getSerializedFile() {
  134.         return this.iImpl.getSerializedFile();
  135.     }

  136.     public void lock() {
  137.         this.iImpl.lock();
  138.     }
  139.     public void unlock() {
  140.         this.iImpl.unlock();
  141.     }
  142.    
  143.     @Override
  144.     public void write(int b) throws IOException {
  145.         this.iImpl.writeInBuffer(b);
  146.     }

  147.     @Override
  148.     public void write(byte[] b, int off, int len) throws IOException {
  149.         this.iImpl.writeInBuffer(b, off, len);
  150.     }

  151.     @Override
  152.     public void write(byte[] b) throws IOException {
  153.         this.iImpl.writeInBuffer(b);
  154.     }

  155.    
  156.     public void reset() {
  157.         this.iImpl.reset();
  158.     }
  159.     public void clearResources() {
  160.         this.iImpl.clearResources();
  161.     }

  162.    
  163.     public int size() {
  164.         return this.iImpl.size();
  165.     }
  166.    
  167.    
  168.    
  169.     public void writeTo(OutputStream out) throws IOException {
  170.         this.iImpl.writeTo(out);
  171.     }

  172.     public byte[] toByteArray() {
  173.         return this.iImpl.serializeToByteArray();
  174.     }

  175.     @Override
  176.     public String toString() {
  177.         return this.iImpl.toString();
  178.     }

  179.     public String toString(String charsetName) throws UnsupportedEncodingException {
  180.         return this.iImpl.serializeToString(charsetName);
  181.     }

  182.     public String toString(Charset charset) throws UnsupportedEncodingException {
  183.         return this.iImpl.serializeToString(charset);
  184.     }


  185.     @Override
  186.     public void close() throws IOException {
  187.         this.iImpl.close();
  188.     }


  189.     @Override
  190.     public void flush() throws IOException {
  191.         this.iImpl.flush();
  192.     }
  193.    
  194.     public InputStream getInputStream() {
  195.         return this.iImpl.getInputStream();
  196.     }
  197. }