DumpByteArrayOutputStream_DefaultImpl.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.ByteArrayInputStream;
  22. import java.io.ByteArrayOutputStream;
  23. import java.io.File;
  24. import java.io.FileInputStream;
  25. import java.io.FileOutputStream;
  26. import java.io.IOException;
  27. import java.io.InputStream;
  28. import java.io.OutputStream;
  29. import java.io.UnsupportedEncodingException;
  30. import java.nio.charset.Charset;

  31. import org.openspcoop2.utils.CopyStream;
  32. import org.openspcoop2.utils.resources.FileSystemUtilities;

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

  41.     private int soglia = -1; // -1 senza soglia
  42.     private int attuale = 0;
  43.     private File repositoryFile = null;
  44.     private String idTransazione;
  45.     private String tipoMessaggio;
  46.     private File f = null;
  47.     private boolean fLocked = false;

  48.     private FileOutputStream fout = null;
  49.    
  50.     DumpByteArrayOutputStream_DefaultImpl() {
  51.         // non e' attivo alcun controllo di soglia
  52.     }
  53.     DumpByteArrayOutputStream_DefaultImpl(Integer soglia, File repositoryFile, String idTransazione, String tipoMessaggio) {
  54.         this.soglia = soglia;
  55.         this.repositoryFile = repositoryFile;
  56.         this.idTransazione = idTransazione;
  57.         this.tipoMessaggio = tipoMessaggio;
  58.     }
  59.    

  60.     private void checkInitFile() throws Exception {
  61.         if(this.f==null) {
  62.             this._checkInitFile();
  63.         }
  64.     }
  65.     private synchronized void _checkInitFile() throws Exception {
  66.         if(this.f==null) {
  67.             this.f = DumpByteArrayOutputStream.newFile(this.repositoryFile, this.tipoMessaggio, this.idTransazione);
  68.             super.flush();
  69.             super.close();
  70.             this.fout = new FileOutputStream(this.f);
  71.             if(super.size()>0) {
  72.                 this.fout.write(super.toByteArray());
  73.             }
  74.         }
  75.     }
  76.    
  77.     @Override
  78.     public boolean isSerializedOnFileSystem() {
  79.         return this.f!=null;
  80.     }
  81.     @Override
  82.     public File getSerializedFile() {
  83.         return this.f;
  84.     }

  85.     @Override
  86.     public void lock() {
  87.         this.fLocked = true;
  88.     }
  89.     @Override
  90.     public void unlock() {
  91.         this.fLocked = false;
  92.     }
  93.    
  94.     @Override
  95.     public synchronized void writeInBuffer(int b) {
  96.         if(this.soglia>0) {
  97.             if(this.attuale>this.soglia) {
  98.                 try {
  99.                     this.attuale++;
  100.                    
  101.                     if(this.f==null) {
  102.                         checkInitFile();
  103.                     }
  104.                     if(this.fout!=null) {
  105.                         this.fout.write(b);
  106.                     }
  107.                     else {
  108.                         throw new Exception("FileOutputStream '"+this.f.getAbsolutePath()+"' closed");
  109.                     }
  110.                 }catch(Throwable e) {
  111.                     throw new RuntimeException(e.getMessage(),e);
  112.                 }
  113.                 return;
  114.             }
  115.         }
  116.         this.attuale++;
  117.         super.write(b);
  118.     }

  119.     @Override
  120.     public synchronized void writeInBuffer(byte[] b, int off, int len) {
  121.         if(this.soglia>0) {
  122.             if( (this.attuale>this.soglia) || ((this.attuale+len)>this.soglia) ) {
  123.                 try {
  124.                     this.attuale=this.attuale+len;
  125.                    
  126.                     if(this.f==null) {
  127.                         checkInitFile();
  128.                     }
  129.                     if(this.fout!=null) {
  130.                         this.fout.write(b, off, len);
  131.                     }
  132.                     else {
  133.                         throw new Exception("FileOutputStream '"+this.f.getAbsolutePath()+"' closed");
  134.                     }
  135.                 }catch(Throwable e) {
  136.                     throw new RuntimeException(e.getMessage(),e);
  137.                 }
  138.                 return;
  139.             }
  140.         }
  141.         this.attuale=this.attuale+len;
  142.         super.write(b, off, len);
  143.     }


  144.     @Override
  145.     public void writeInBuffer(byte[] b) throws IOException {
  146.         if(this.soglia>0) {
  147.             if( (this.attuale>this.soglia) || ((this.attuale+b.length)>this.soglia) ) {
  148.                 try {
  149.                     this.attuale=this.attuale+b.length;
  150.                    
  151.                     if(this.f==null) {
  152.                         checkInitFile();
  153.                     }
  154.                     if(this.fout!=null) {
  155.                         this.fout.write(b);
  156.                     }
  157.                     else {
  158.                         throw new Exception("FileOutputStream '"+this.f.getAbsolutePath()+"' closed");
  159.                     }
  160.                 }catch(Throwable e) {
  161.                     throw new RuntimeException(e.getMessage(),e);
  162.                 }
  163.                 return;
  164.             }
  165.         }
  166.         this.attuale=this.attuale+b.length;
  167.         super.write(b);
  168.     }
  169. //  
  170. //  @Override
  171. //  public void writeBytes(byte[] b) {
  172. //      if(this.soglia>0) {
  173. //          if( (this.attuale>this.soglia) || ((this.attuale+b.length)>this.soglia) ) {
  174. //              try {
  175. //                  if(this.f==null) {
  176. //                      checkInitFile();
  177. //                  }
  178. //                  if(this.fout!=null) {
  179. //                      this.fout.write(b);
  180. //                  }
  181. //                  else {
  182. //                      throw new Exception("FileOutputStream '"+this.f.getAbsolutePath()+"' closed");
  183. //                  }
  184. //              }catch(Throwable e) {
  185. //                  throw new RuntimeException(e.getMessage(),e);
  186. //              }
  187. //              return;
  188. //          }
  189. //      }
  190. //      this.attuale=this.attuale+b.length;
  191. //      super.writeBytes(b);
  192. //  }

  193.     @Override
  194.     public synchronized void reset() {
  195.         clearResources();
  196.     }
  197.     @Override
  198.     public void clearResources() {
  199.         if(this.f!=null) {
  200.             _clearResources();
  201.         }
  202.     }
  203.     private synchronized void _clearResources() {
  204.         if(this.f!=null && !this.fLocked) {
  205.             try {
  206.                 if(this.fout!=null) {
  207.                     this.fout.flush();
  208.                     this.fout.close();
  209.                     this.fout = null;
  210.                 }
  211.                 if(!this.f.delete()) {
  212.                     // ignore
  213.                 }
  214.                 this.f = null;
  215.             }catch(Throwable e) {
  216.                 throw new RuntimeException(e.getMessage(),e);
  217.             }
  218.         }
  219.     }

  220.    
  221.     @Override
  222.     public synchronized int size() {
  223.         if(this.f!=null) {
  224.             try {
  225.                 return (int) this.f.length();
  226.             }catch(Throwable e) {
  227.                 throw new RuntimeException(e.getMessage(),e);
  228.             }
  229.         }
  230.         else {
  231.             return super.size();
  232.         }
  233.     }
  234.    
  235.    
  236.    
  237.     @Override
  238.     public synchronized void writeTo(OutputStream out) throws IOException {
  239.         try {
  240.             this.close();
  241.         }catch(Throwable e) {
  242.             throw new RuntimeException(e.getMessage(),e);
  243.         }
  244.         if(this.f!=null) {
  245.             try {
  246.                 CopyStream.copy(this.f, out);
  247.             }catch(Throwable e) {
  248.                 throw new RuntimeException(e.getMessage(),e);
  249.             }
  250.         }
  251.         else {
  252.             super.writeTo(out);
  253.         }
  254.     }

  255.    
  256.     @Override
  257.     public synchronized byte[] toByteArray() {
  258.         return _serializeToByteArray();
  259.     }
  260.     @Override
  261.     public synchronized byte[] serializeToByteArray() {
  262.         return _serializeToByteArray();
  263.     }
  264.     private synchronized byte[] _serializeToByteArray() {
  265.         try {
  266.             this.close();
  267.         }catch(Throwable e) {
  268.             throw new RuntimeException(e.getMessage(),e);
  269.         }
  270.         if(this.f!=null) {
  271.             try {
  272.                 return FileSystemUtilities.readBytesFromFile(this.f);
  273.             }catch(Throwable e) {
  274.                 throw new RuntimeException(e.getMessage(),e);
  275.             }
  276.         }
  277.         else {
  278.             return super.toByteArray();
  279.         }
  280.     }

  281.    
  282.     @Override
  283.     public synchronized String toString() {
  284.         return this._serializeToString();
  285.     }
  286.     @Override
  287.     public synchronized String serializeToString() {
  288.         return this._serializeToString();
  289.     }
  290.     private synchronized String _serializeToString() {
  291.         try {
  292.             this.close();
  293.         }catch(Throwable e) {
  294.             throw new RuntimeException(e.getMessage(),e);
  295.         }
  296.         if(this.f!=null) {
  297.             try {
  298.                 return FileSystemUtilities.readFile(this.f);
  299.             }catch(Throwable e) {
  300.                 throw new RuntimeException(e.getMessage(),e);
  301.             }
  302.         }
  303.         else {
  304.             return super.toString();
  305.         }
  306.     }

  307.     @Override
  308.     public synchronized String toString(String charsetName) throws UnsupportedEncodingException {
  309.         return this._serializeToString(charsetName);
  310.     }
  311.     @Override
  312.     public synchronized String serializeToString(String charsetName) throws UnsupportedEncodingException {
  313.         return this._serializeToString(charsetName);
  314.     }
  315.     private synchronized String _serializeToString(String charsetName) throws UnsupportedEncodingException {
  316.         try {
  317.             this.close();
  318.         }catch(Throwable e) {
  319.             throw new RuntimeException(e.getMessage(),e);
  320.         }
  321.         if(this.f!=null) {
  322.             try {
  323.                 return FileSystemUtilities.readFile(this.f, charsetName);
  324.             }catch(Throwable e) {
  325.                 throw new RuntimeException(e.getMessage(),e);
  326.             }
  327.         }
  328.         else {
  329.             return super.toString(charsetName);
  330.         }
  331.     }

  332.     @Override
  333.     public synchronized String toString(Charset charset) {
  334.         return this._serializeToString(charset);
  335.     }
  336.     @Override
  337.     public synchronized String serializeToString(Charset charset) {
  338.         return this._serializeToString(charset);
  339.     }
  340.     private synchronized String _serializeToString(Charset charset) {
  341.         try {
  342.             this.close();
  343.         }catch(Throwable e) {
  344.             throw new RuntimeException(e.getMessage(),e);
  345.         }
  346.         if(this.f!=null) {
  347.             try {
  348.                 return FileSystemUtilities.readFile(this.f, charset);
  349.             }catch(Throwable e) {
  350.                 throw new RuntimeException(e.getMessage(),e);
  351.             }
  352.         }
  353.         else {
  354.             return super.toString(charset);
  355.         }
  356.     }

  357.     @SuppressWarnings("deprecation")
  358.     @Override
  359.     public synchronized String toString(int hibyte) {
  360.         throw new RuntimeException("NotImplemented");
  361.     }

  362.     @Override
  363.     public void close() throws IOException {
  364.         if(this.f!=null) {
  365.             try {
  366.                 if(this.fout!=null) {
  367.                     this.fout.flush();
  368.                     this.fout.close();
  369.                     this.fout = null;
  370.                 }
  371.             }catch(Throwable e) {
  372.                 throw new RuntimeException(e.getMessage(),e);
  373.             }
  374.         }
  375.         else {
  376.             super.flush();
  377.             super.close();
  378.         }
  379.     }


  380.     @Override
  381.     public void flush() throws IOException {
  382.         if(this.f!=null) {
  383.             try {
  384.                 if(this.fout!=null) {
  385.                     this.fout.flush();
  386.                 }
  387.             }catch(Throwable e) {
  388.                 throw new RuntimeException(e.getMessage(),e);
  389.             }
  390.         }
  391.         else {
  392.             super.flush();
  393.         }
  394.     }
  395.    
  396.     @Override
  397.     public InputStream getInputStream() {
  398.         try {
  399.             this.close();
  400.         }catch(Throwable e) {
  401.             throw new RuntimeException(e.getMessage(),e);
  402.         }
  403.         if(this.f!=null) {
  404.             try {
  405.                 return new FileInputStream(this.f);
  406.             }catch(Throwable e) {
  407.                 throw new RuntimeException(e.getMessage(),e);
  408.             }
  409.         }
  410.         else {
  411.             return new ByteArrayInputStream(super.toByteArray());
  412.         }
  413.     }
  414. }