JavaDeserializer.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.serialization;

  21. import java.io.ByteArrayInputStream;
  22. import java.io.ByteArrayOutputStream;
  23. import java.io.InputStream;
  24. import java.io.ObjectInputStream;
  25. import java.io.Reader;
  26. import java.io.StringReader;


  27. /**
  28.  * Contiene utility per effettuare la serializzazione di un oggetto
  29.  *
  30.  * @author Poli Andrea (apoli@link.it)
  31.  * @author $Author$
  32.  * @version $Rev$, $Date$
  33.  */

  34. public class JavaDeserializer implements IDeserializer {

  35.     @Override
  36.     public Object getObject(String s, Class<?> classType) throws IOException {
  37.         StringReader reader = new StringReader(s);
  38.         return this.readObject(reader, classType);
  39.     }

  40.     @Override
  41.     public Object readObject(InputStream is, Class<?> classType)
  42.             throws IOException {
  43.         ObjectInputStream ois = null;
  44.         try{
  45.             ois = new ObjectInputStream(is);
  46.             Object o = ois.readObject();
  47.             if(classType.isInstance(o)){
  48.                 return o;
  49.             }else{
  50.                 throw new Exception("Oggetto deserializzato non e' di tipo ["+classType.getName()+"] bensi' di tipo ["+o.getClass().getName()+"]");
  51.             }
  52.         }catch(Exception e){
  53.             throw new IOException(e.getMessage(),e);
  54.         }finally{
  55.             try{
  56.                 if(ois!=null){
  57.                     ois.close();
  58.                 }
  59.             }catch(Exception eClose){
  60.                 // close
  61.             }
  62.         }
  63.     }

  64.     @Override
  65.     public Object readObject(Reader reader, Class<?> classType)
  66.             throws IOException {
  67.         ObjectInputStream ois = null;
  68.         try{
  69.             ByteArrayOutputStream bout = new ByteArrayOutputStream();
  70.             int letti = 0;
  71.             char[]buffer = new char[org.openspcoop2.utils.Utilities.DIMENSIONE_BUFFER];
  72.             while( (letti = reader.read(buffer)) != -1 ){
  73.                 for(int i=0; i<letti; i++){
  74.                     bout.write(buffer[i]);
  75.                 }
  76.             }
  77.             // NON FUNZIONA: org.openspcoop2.utils.CopyCharStream.copy(reader, bout);
  78.             bout.flush();
  79.             bout.close();
  80.            
  81.             ois = new ObjectInputStream(new ByteArrayInputStream(bout.toByteArray()));
  82.             Object o = ois.readObject();
  83.             if(classType.isInstance(o)){
  84.                 return o;
  85.             }else{
  86.                 throw new Exception("Oggetto deserializzato non e' di tipo ["+classType.getName()+"] bensi' di tipo ["+o.getClass().getName()+"]");
  87.             }
  88.         }catch(Exception e){
  89.             throw new IOException(e.getMessage(),e);
  90.         }finally{
  91.             try{
  92.                 if(ois!=null){
  93.                     ois.close();
  94.                 }
  95.             }catch(Exception eClose){
  96.                 // close
  97.             }
  98.         }
  99.     }

  100.    

  101. }