InputStreamDataSource.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.dch;

  21. import java.io.ByteArrayInputStream;
  22. import java.io.ByteArrayOutputStream;
  23. import java.io.IOException;
  24. import java.io.InputStream;
  25. import java.io.OutputStream;

  26. import javax.activation.DataSource;

  27. /**
  28.  * InputStreamDataSource
  29.  *
  30.  * @author Poli Andrea (apoli@link.it)
  31.  * @author $Author$
  32.  * @version $Rev$, $Date$
  33.  */

  34. public class InputStreamDataSource implements DataSource {

  35.     private String name;
  36.     private String contentType;
  37.     private byte[] bytes;

  38.     public InputStreamDataSource(String name, String contentType, InputStream inputStream) throws IOException {
  39.         int read;          
  40.         this.name = name;
  41.         this.contentType = contentType;

  42.         ByteArrayOutputStream baos = new ByteArrayOutputStream();

  43.         byte[] buff = new byte[256];            
  44.         while ((read = inputStream.read(buff)) != -1) {
  45.             baos.write(buff, 0, read);
  46.         }
  47.         baos.flush();
  48.         baos.close();
  49.         this.bytes = baos.toByteArray();
  50.     }
  51.     public InputStreamDataSource(String name, String contentType, byte[] b) throws IOException {
  52.         this.name = name;
  53.         this.contentType = contentType;
  54.         this.bytes = b;
  55.     }

  56.     @Override
  57.     public String getContentType() {
  58.         return this.contentType;
  59.     }

  60.     @Override
  61.     public InputStream getInputStream() throws IOException {
  62.         return new ByteArrayInputStream(this.bytes);
  63.     }

  64.     @Override
  65.     public String getName() {
  66.         return this.name;
  67.     }

  68.     @Override
  69.     public OutputStream getOutputStream() throws IOException {
  70.         throw new IOException("Cannot write to this read-only resource");
  71.     }

  72. }