JavaSerializer.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.ByteArrayOutputStream;
  22. import java.io.ObjectOutputStream;
  23. import java.io.OutputStream;
  24. import java.io.StringWriter;
  25. import java.io.Writer;


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

  33. public class JavaSerializer implements ISerializer {

  34.     @Override
  35.     public String getObject(Object o) throws IOException {
  36.         try{
  37.             StringWriter writer = new StringWriter();
  38.             writeObject(o,writer);
  39.             writer.flush();
  40.             writer.close();
  41.             return writer.toString();
  42.         }catch(Exception e){
  43.             throw new IOException(e.getMessage(),e);
  44.         }
  45.     }

  46.     @Override
  47.     public void writeObject(Object o, OutputStream out) throws IOException {
  48.         ObjectOutputStream oos = null;
  49.         try{
  50.             oos = new ObjectOutputStream(out);
  51.             oos.writeObject(o);
  52.             oos.flush();
  53.         }catch(Exception e){
  54.             throw new IOException(e.getMessage(),e);
  55.         }finally{
  56.             try{
  57.                 if(oos!=null){
  58.                     oos.close();
  59.                 }
  60.             }catch(Exception eClose){
  61.                 // close
  62.             }
  63.         }
  64.     }

  65.     @Override
  66.     public void writeObject(Object o, Writer out) throws IOException {
  67.         try{
  68.             ByteArrayOutputStream bout = new ByteArrayOutputStream();
  69.             writeObject(o,bout);
  70.             bout.flush();
  71.             bout.close();
  72.             byte[]array = bout.toByteArray();
  73.             for(int i=0; i<bout.size();i++){
  74.                 out.write(array[i]);
  75.             }
  76.            
  77.         }catch(Exception e){
  78.             throw new IOException(e.getMessage(),e);
  79.         }
  80.     }

  81. }