BrightSide Workbench Full Report + Source Code
elephant-plugins/src/main/java/org/turro/plugin/GenericObject.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.plugin;
19 
20 import java.io.ByteArrayInputStream;
21 import java.io.ByteArrayOutputStream;
22 import java.io.IOException;
23 import java.io.ObjectInputStream;
24 import java.io.ObjectOutputStream;
25 import java.io.Serializable;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
28 
33 public class GenericObject implements Serializable {
34 
35  private Serializable object;
36 
37  public Serializable getObject() {
38  return object;
39  }
40 
41  public void setObject(Serializable object) {
42  this.object = object;
43  }
44 
45  public void bytesToObject(byte[] buf) {
46  try{
47  object = (Serializable) new ObjectInputStream(new ByteArrayInputStream(buf)).readObject();
48  } catch (IOException e) {
49  Logger.getLogger(GenericObject.class.getName()).log(Level.SEVERE, null, e);
50  } catch (ClassNotFoundException ex) {
51  Logger.getLogger(GenericObject.class.getName()).log(Level.SEVERE, null, ex);
52  }
53  }
54 
55  public byte[] objectToBytes() {
56  try{
57  ByteArrayOutputStream baos = new ByteArrayOutputStream();
58  ObjectOutputStream oos = new ObjectOutputStream(baos);
59  oos.writeObject(object);
60  baos.close();
61  return baos.toByteArray();
62  } catch (IOException e) {
63  Logger.getLogger(GenericObject.class.getName()).log(Level.SEVERE, null, e);
64  }
65  return null;
66  }
67 }