DumpByteArrayOutputStream_FastImpl.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.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. import java.io.UnsupportedEncodingException;
  29. import java.nio.charset.Charset;

  30. import org.openspcoop2.utils.CopyStream;
  31. import org.openspcoop2.utils.resources.FileSystemUtilities;
  32. import org.springframework.util.FastByteArrayOutputStream;

  33. /**
  34. * DumpByteArrayOutputStream_FastImpl
  35. *
  36. * @author Andrea Poli (apoli@link.it)
  37.  * @author $Author$
  38.  * @version $Rev$, $Date$
  39. */
  40. public class DumpByteArrayOutputStream_FastImpl extends FastByteArrayOutputStream 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_FastImpl() {
  51.         // non e' attivo alcun controllo di soglia
  52.     }
  53.     DumpByteArrayOutputStream_FastImpl(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.     private void checkInitFile() throws Exception {
  60.         if(this.f==null) {
  61.             this._checkInitFile();
  62.         }
  63.     }
  64.     private synchronized void _checkInitFile() throws Exception {
  65.         if(this.f==null) {
  66.             this.f = DumpByteArrayOutputStream.newFile(this.repositoryFile, this.tipoMessaggio, this.idTransazione);
  67.             super.flush();
  68.             super.close();
  69.             this.fout = new FileOutputStream(this.f);
  70.             if(super.size()>0) {
  71.                 // NOTA i metodi super.toString(), super.toByteArray() e super.toByteArrayUnsafe() non ritornano nulla.
  72.                 super.writeTo(this.fout);
  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 void writeInBuffer(int b) throws IOException {
  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 void writeInBuffer(byte[] b, int off, int len) throws IOException {
  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.    
  171.     @Override
  172.     public void reset() {
  173.         clearResources();
  174.     }
  175.     @Override
  176.     public void clearResources() {
  177.         if(this.f!=null) {
  178.             _clearResources();
  179.         }
  180.     }
  181.     private synchronized void _clearResources() {
  182.         if(this.f!=null && !this.fLocked) {
  183.             try {
  184.                 if(this.fout!=null) {
  185.                     this.fout.flush();
  186.                     this.fout.close();
  187.                     this.fout = null;
  188.                 }
  189.                 if(!this.f.delete()) {
  190.                     // ignore
  191.                 }
  192.                 this.f = null;
  193.             }catch(Throwable e) {
  194.                 throw new RuntimeException(e.getMessage(),e);
  195.             }
  196.         }
  197.     }

  198.    
  199.     @Override
  200.     public int size() {
  201.         if(this.f!=null) {
  202.             try {
  203.                 return (int) this.f.length();
  204.             }catch(Throwable e) {
  205.                 throw new RuntimeException(e.getMessage(),e);
  206.             }
  207.         }
  208.         else {
  209.             return super.size();
  210.         }
  211.     }
  212.    
  213.    
  214.    
  215.     @Override
  216.     public void writeTo(OutputStream out) throws IOException {
  217.         try {
  218.             this.close();
  219.         }catch(Throwable e) {
  220.             throw new RuntimeException(e.getMessage(),e);
  221.         }
  222.         if(this.f!=null) {
  223.             try {
  224.                 CopyStream.copy(this.f, out);
  225.             }catch(Throwable e) {
  226.                 throw new RuntimeException(e.getMessage(),e);
  227.             }
  228.         }
  229.         else {
  230.             super.writeTo(out);
  231.         }
  232.     }

  233.     private byte[] _toByteArrayFromFastStructure() {
  234.         // NOTA i metodi super.toString(), super.toByteArray() e super.toByteArrayUnsafe() non ritornano nulla.
  235.         try {
  236.             ByteArrayOutputStream bout = new ByteArrayOutputStream();
  237.             super.writeTo(bout);
  238.             bout.flush();
  239.             bout.close();
  240.             return bout.toByteArray();
  241.         }catch(Throwable e) {
  242.             throw new RuntimeException(e.getMessage(),e);
  243.         }
  244.     }
  245.    
  246.     @Override
  247.     public byte[] toByteArray() {
  248.         return serializeToByteArray();
  249.     }
  250.     @Override
  251.     public byte[] serializeToByteArray() {
  252.         try {
  253.             this.close();
  254.         }catch(Throwable e) {
  255.             throw new RuntimeException(e.getMessage(),e);
  256.         }
  257.         if(this.f!=null) {
  258.             try {
  259.                 return FileSystemUtilities.readBytesFromFile(this.f);
  260.             }catch(Throwable e) {
  261.                 throw new RuntimeException(e.getMessage(),e);
  262.             }
  263.         }
  264.         else {
  265.             // NOTA i metodi super.toString(), super.toByteArray() e super.toByteArrayUnsafe() non ritornano nulla.
  266.             //return super.toByteArray();
  267.             return _toByteArrayFromFastStructure();
  268.         }
  269.     }

  270.     @Override
  271.     public String toString() {
  272.         return this.serializeToString();
  273.     }
  274.     @Override
  275.     public String serializeToString() {
  276.         try {
  277.             this.close();
  278.         }catch(Throwable e) {
  279.             throw new RuntimeException(e.getMessage(),e);
  280.         }
  281.         if(this.f!=null) {
  282.             try {
  283.                 return FileSystemUtilities.readFile(this.f);
  284.             }catch(Throwable e) {
  285.                 throw new RuntimeException(e.getMessage(),e);
  286.             }
  287.         }
  288.         else {
  289.             // NOTA i metodi super.toString(), super.toByteArray() e super.toByteArrayUnsafe() non ritornano nulla.
  290.             //return super.toString();
  291.             return new String(this._toByteArrayFromFastStructure());
  292.         }
  293.     }

  294.     @Override
  295.     public String serializeToString(String charsetName) throws UnsupportedEncodingException {
  296.         try {
  297.             this.close();
  298.         }catch(Throwable e) {
  299.             throw new RuntimeException(e.getMessage(),e);
  300.         }
  301.         if(this.f!=null) {
  302.             try {
  303.                 return FileSystemUtilities.readFile(this.f, charsetName);
  304.             }catch(Throwable e) {
  305.                 throw new RuntimeException(e.getMessage(),e);
  306.             }
  307.         }
  308.         else {
  309.             // NOTA i metodi super.toString(), super.toByteArray() e super.toByteArrayUnsafe() non ritornano nulla.
  310.             //return new String(super.toByteArray(), charsetName);
  311.             return new String(this._toByteArrayFromFastStructure(), charsetName);
  312.         }
  313.     }

  314.     @Override
  315.     public String serializeToString(Charset charset) 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, charset);
  324.             }catch(Throwable e) {
  325.                 throw new RuntimeException(e.getMessage(),e);
  326.             }
  327.         }
  328.         else {
  329.             // NOTA i metodi super.toString(), super.toByteArray() e super.toByteArrayUnsafe() non ritornano nulla.
  330.             //return new String(super.toByteArray(), charset.name());
  331.             return new String(this._toByteArrayFromFastStructure(), charset.name());
  332.         }
  333.     }


  334.     @Override
  335.     public void close() {
  336.         if(this.f!=null) {
  337.             try {
  338.                 if(this.fout!=null) {
  339.                     this.fout.flush();
  340.                     this.fout.close();
  341.                     this.fout = null;
  342.                 }
  343.             }catch(Throwable e) {
  344.                 throw new RuntimeException(e.getMessage(),e);
  345.             }
  346.         }
  347.         else {
  348.             try {
  349.                 super.flush();
  350.             }catch(Throwable e) {
  351.                 throw new RuntimeException(e.getMessage(),e);
  352.             }
  353.             super.close();
  354.         }
  355.     }


  356.     @Override
  357.     public void flush() throws IOException {
  358.         if(this.f!=null) {
  359.             try {
  360.                 if(this.fout!=null) {
  361.                     this.fout.flush();
  362.                 }
  363.             }catch(Throwable e) {
  364.                 throw new RuntimeException(e.getMessage(),e);
  365.             }
  366.         }
  367.         else {
  368.             super.flush();
  369.         }
  370.     }
  371.    
  372.     @Override
  373.     public InputStream getInputStream() {
  374.         try {
  375.             this.close();
  376.         }catch(Throwable e) {
  377.             throw new RuntimeException(e.getMessage(),e);
  378.         }
  379.         if(this.f!=null) {
  380.             try {
  381.                 return new FileInputStream(this.f);
  382.             }catch(Throwable e) {
  383.                 throw new RuntimeException(e.getMessage(),e);
  384.             }
  385.         }
  386.         else {
  387.             // NOTA i metodi super.toString(), super.toByteArray() e super.toByteArrayUnsafe() non ritornano nulla.
  388.             //return new ByteArrayInputStream(super.toByteArray());
  389.             return super.getInputStream();
  390.         }
  391.     }
  392. }