01: /*
02: * Copyright (c) 1998 - 2005 Versant Corporation
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * Versant Corporation - initial API and implementation
10: */
11: package com.versant.core.jdo.externalizer;
12:
13: import com.versant.core.common.BindingSupportImpl;
14:
15: import javax.jdo.PersistenceManager;
16: import java.io.*;
17:
18: /**
19: * Externalizer that converts to/from byte[] using Java serialization.
20: * This class is Serializable. This is only a requirement if you are using
21: * remote PMs.
22: */
23: public class SerializedExternalizer implements Externalizer,
24: Serializable {
25:
26: public static final String SHORT_NAME = "SERIALIZED";
27:
28: public Object toExternalForm(PersistenceManager pm, Object o) {
29: if (o == null)
30: return null;
31: try {
32: ByteArrayOutputStream bo = new ByteArrayOutputStream(256);
33: ObjectOutputStream os = new ObjectOutputStream(bo);
34: os.writeObject(o);
35: os.close();
36: return bo.toByteArray();
37: } catch (IOException e) {
38: throw BindingSupportImpl.getInstance().fatal(e.toString(),
39: e);
40: }
41: }
42:
43: public Object fromExternalForm(PersistenceManager pm, Object o) {
44: if (o == null)
45: return o;
46: try {
47: ByteArrayInputStream bi = new ByteArrayInputStream(
48: (byte[]) o);
49: ObjectInputStream oi = new ObjectInputStream(bi);
50: Object ans = oi.readObject();
51: oi.close();
52: return ans;
53: } catch (Exception e) {
54: throw BindingSupportImpl.getInstance().fatal(e.toString(),
55: e);
56: }
57: }
58:
59: public Class getExternalType() {
60: return byte[].class;
61: }
62: }
|