BrightSide Workbench Full Report + Source Code
elephant/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 import org.turro.elephant.context.ElephantContext;
29 
34 public class GenericObject implements Serializable {
35 
36  private Serializable object;
37 
38  public Serializable getObject() {
39  return object;
40  }
41 
42  public void setObject(Serializable object) {
43  this.object = object;
44  }
45 
46  public void bytesToObject(byte[] buf) {
47  try{
48  object = (Serializable) new ObjectInputStream(new ByteArrayInputStream(buf)).readObject();
49  } catch (IOException | ClassNotFoundException e) {
50  Logger.getLogger(GenericObject.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), e);
51  }
52  }
53 
54  public byte[] objectToBytes() {
55  try{
56  ByteArrayOutputStream baos = new ByteArrayOutputStream();
57  ObjectOutputStream oos = new ObjectOutputStream(baos);
58  oos.writeObject(object);
59  baos.close();
60  return baos.toByteArray();
61  } catch (IOException e) {
62  Logger.getLogger(GenericObject.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), e);
63  }
64  return null;
65  }
66 }