OpenSPCoop2InputStreamDynamicContent.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.message;

  21. import java.io.IOException;
  22. import java.io.OutputStream;

  23. import org.openspcoop2.utils.io.DumpByteArrayOutputStream;

  24. /**
  25.  * OpenSPCoop2InputStreamDynamicContent
  26.  *
  27.  * @author Andrea Poli (apoli@link.it)
  28.  * @author $Author$
  29.  * @version $Rev$, $Date$
  30.  */
  31. public class OpenSPCoop2InputStreamDynamicContent extends org.apache.commons.io.input.CountingInputStream {

  32.     private org.apache.commons.io.input.CountingInputStream is;

  33.     private DumpByteArrayOutputStream buffer;
  34.    
  35.     private boolean isReadAll = false;
  36.    
  37.     public OpenSPCoop2InputStreamDynamicContent(org.apache.commons.io.input.CountingInputStream is, DumpByteArrayOutputStream buffer) {
  38.         super(null);
  39.         this.is = is;
  40.         this.buffer = buffer;
  41.     }

  42.     public org.apache.commons.io.input.CountingInputStream getWrappedInputStream() {
  43.         return this.is;
  44.     }
  45.    
  46.     public DumpByteArrayOutputStream getBuffer() {
  47.         return this.buffer;
  48.     }
  49.    
  50.     public boolean isInputStreamConsumed() {
  51.         return this.isReadAll;
  52.     }

  53.     // implementati con buffer
  54.     @Override
  55.     public int read() throws IOException {
  56.         int r = this.is.read();
  57.         if(this.buffer!=null && r!=-1) {
  58.             this.buffer.write(r);
  59.         }
  60.         //System.out.println("read(); [readAll:"+this.isReadAll+"] (r:"+r+") buffer size: "+((this.buffer!=null) ? this.buffer.size() : "--n.d.--"));
  61.         if(r==-1) {
  62.             this.isReadAll = true;
  63.         }
  64.         return r;
  65.     }

  66.     @Override
  67.     public int read(byte[] b, int off, int len) throws IOException {
  68.         int r = this.is.read(b, off, len);
  69.         if(this.buffer!=null && r!=-1) {
  70.             this.buffer.write(b, off, r);
  71.         }
  72.         //System.out.println("read(byte[],off:"+off+",len:"+len+"); [readAll:"+this.isReadAll+"] (r:"+r+") buffer size: "+((this.buffer!=null) ? this.buffer.size() : "--n.d.--"));
  73.         //System.out.println("\t\t (leng:"+b.length+") ["+new String(b, off, r)+"]");
  74.         if(r==-1) {
  75.             this.isReadAll = true;
  76.         }
  77.         return r;
  78.     }

  79.     @Override
  80.     public int read(byte[] b) throws IOException {
  81.         int r = this.is.read(b);
  82.         if(this.buffer!=null && r!=-1) {
  83.             this.buffer.write(b, 0, r);
  84.         }
  85.         //System.out.println("read(byte[]); [readAll:"+this.isReadAll+"] (r:"+r+") buffer size: "+((this.buffer!=null) ? this.buffer.size() : "--n.d.--"));
  86.         //System.out.println("\t\t (leng:"+b.length+") ["+new String(b)+"]");
  87.         if(r==-1) {
  88.             this.isReadAll = true;
  89.         }
  90.         return r;
  91.     }
  92.    
  93.     // wrapped only
  94.    
  95. //  @Override
  96. //  protected synchronized void afterRead(int n) {
  97. //      this.is.afterRead(n);
  98. //  }

  99.     @Override
  100.     public synchronized long getByteCount() {
  101.         return this.is.getByteCount();
  102.     }

  103.     @Override
  104.     public int getCount() {
  105.         return this.is.getCount();
  106.     }

  107.     @Override
  108.     public synchronized long resetByteCount() {
  109.         return this.is.resetByteCount();
  110.     }

  111.     @Override
  112.     public int resetCount() {
  113.         return this.is.resetCount();
  114.     }

  115.     @Override
  116.     public synchronized long skip(long length) throws IOException {
  117.         return this.is.skip(length);
  118.     }

  119.     @Override
  120.     public int available() throws IOException {
  121.         return this.is.available();
  122.     }

  123. //  @Override
  124. //  protected void beforeRead(int n) throws IOException {
  125. //      this.is.beforeRead(n);
  126. //  }

  127.     @Override
  128.     public void close() throws IOException {
  129.         this.is.close();
  130.     }

  131. //  @Override
  132. //  protected void handleIOException(IOException e) throws IOException {
  133. //      this.is.handleIOException(e);
  134. //  }

  135.     @Override
  136.     public synchronized void mark(int readlimit) {
  137.         this.is.mark(readlimit);
  138.     }

  139.     @Override
  140.     public boolean markSupported() {
  141.         return this.is.markSupported();
  142.     }

  143.     @Override
  144.     public synchronized void reset() throws IOException {
  145.         this.is.reset();
  146.     }

  147.     @Override
  148.     public byte[] readAllBytes() throws IOException {
  149.         return this.is.readAllBytes();
  150.     }

  151.     @Override
  152.     public byte[] readNBytes(int len) throws IOException {
  153.         return this.is.readNBytes(len);
  154.     }

  155.     @Override
  156.     public int readNBytes(byte[] b, int off, int len) throws IOException {
  157.         return this.is.readNBytes(b, off, len);
  158.     }

  159.     @Override
  160.     public long transferTo(OutputStream out) throws IOException {
  161.         return this.is.transferTo(out);
  162.     }

  163.     @Override
  164.     public int hashCode() {
  165.         return this.is.hashCode();
  166.     }

  167.     @Override
  168.     public boolean equals(Object obj) {
  169.         return this.is.equals(obj);
  170.     }

  171. //  @Override
  172. //  protected Object clone() throws CloneNotSupportedException {
  173. //      return this.is.clone();
  174. //  }

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

  179. //  @Override
  180. //  protected void finalize() throws Throwable {
  181. //      this.is.finalize();
  182. //  }
  183. }