KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdo > externalizer > SerializedExternalizer


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.jdo.externalizer;
13
14 import com.versant.core.common.BindingSupportImpl;
15
16 import javax.jdo.PersistenceManager;
17 import java.io.*;
18
19 /**
20  * Externalizer that converts to/from byte[] using Java serialization.
21  * This class is Serializable. This is only a requirement if you are using
22  * remote PMs.
23  */

24 public class SerializedExternalizer implements Externalizer, Serializable {
25
26     public static final String JavaDoc SHORT_NAME = "SERIALIZED";
27
28     public Object JavaDoc toExternalForm(PersistenceManager pm, Object JavaDoc o) {
29         if (o == null) return null;
30         try {
31             ByteArrayOutputStream bo = new ByteArrayOutputStream(256);
32             ObjectOutputStream os = new ObjectOutputStream(bo);
33             os.writeObject(o);
34             os.close();
35             return bo.toByteArray();
36         } catch (IOException e) {
37             throw BindingSupportImpl.getInstance().fatal(e.toString(), e);
38         }
39     }
40
41     public Object JavaDoc fromExternalForm(PersistenceManager pm, Object JavaDoc o) {
42         if (o == null) return o;
43         try {
44             ByteArrayInputStream bi = new ByteArrayInputStream((byte[])o);
45             ObjectInputStream oi = new ObjectInputStream(bi);
46             Object JavaDoc ans = oi.readObject();
47             oi.close();
48             return ans;
49         } catch (Exception JavaDoc e) {
50             throw BindingSupportImpl.getInstance().fatal(e.toString(), e);
51         }
52     }
53
54     public Class JavaDoc getExternalType() {
55         return byte[].class;
56     }
57 }
58
Popular Tags