001: /*
002: * Distributed as part of c3p0 v.0.9.1.2
003: *
004: * Copyright (C) 2005 Machinery For Change, Inc.
005: *
006: * Author: Steve Waldman <swaldman@mchange.com>
007: *
008: * This library is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU Lesser General Public License version 2.1, as
010: * published by the Free Software Foundation.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public License
018: * along with this software; see the file LICENSE. If not, write to the
019: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: */
022:
023: package com.mchange.v2.ser;
024:
025: import java.io.*;
026: import com.mchange.v1.io.*;
027: import com.mchange.v2.log.*;
028:
029: public final class SerializableUtils {
030: final static MLogger logger = MLog
031: .getLogger(SerializableUtils.class);
032:
033: private SerializableUtils() {
034: }
035:
036: public static byte[] toByteArray(Object obj)
037: throws NotSerializableException {
038: return serializeToByteArray(obj);
039: }
040:
041: public static byte[] toByteArray(Object obj, Indirector indirector,
042: IndirectPolicy policy) throws NotSerializableException {
043: try {
044: if (policy == IndirectPolicy.DEFINITELY_INDIRECT) {
045: if (indirector == null)
046: throw new IllegalArgumentException(
047: "null indirector is not consistent with "
048: + policy);
049:
050: IndirectlySerialized indirect = indirector
051: .indirectForm(obj);
052: return toByteArray(indirect);
053: } else if (policy == IndirectPolicy.INDIRECT_ON_EXCEPTION) {
054: if (indirector == null)
055: throw new IllegalArgumentException(
056: "null indirector is not consistent with "
057: + policy);
058:
059: try {
060: return toByteArray(obj);
061: } catch (NotSerializableException e) {
062: return toByteArray(obj, indirector,
063: IndirectPolicy.DEFINITELY_INDIRECT);
064: }
065: } else if (policy == IndirectPolicy.DEFINITELY_DIRECT)
066: return toByteArray(obj);
067: else
068: throw new InternalError("unknown indirecting policy: "
069: + policy);
070: } catch (NotSerializableException e) {
071: throw e;
072: } catch (Exception e) {
073: //e.printStackTrace();
074: if (logger.isLoggable(MLevel.WARNING))
075: logger
076: .log(
077: MLevel.WARNING,
078: "An Exception occurred while serializing an Object to a byte[] with an Indirector.",
079: e);
080: throw new NotSerializableException(e.toString());
081: }
082: }
083:
084: /**
085: * @deprecated use SerialializableUtils.toByteArray() [shorter name is better!]
086: */
087: public static byte[] serializeToByteArray(Object obj)
088: throws NotSerializableException {
089: try {
090: ByteArrayOutputStream baos = new ByteArrayOutputStream();
091: ObjectOutputStream out = new ObjectOutputStream(baos);
092: out.writeObject(obj);
093: return baos.toByteArray();
094: } catch (NotSerializableException e) {
095: //this is the only IOException that
096: //shouldn't signal a bizarre error...
097: e.fillInStackTrace();
098: throw e;
099: } catch (IOException e) {
100: //e.printStackTrace();
101: if (logger.isLoggable(MLevel.SEVERE))
102: logger
103: .log(
104: MLevel.SEVERE,
105: "An IOException occurred while writing into a ByteArrayOutputStream?!?",
106: e);
107: throw new Error("IOException writing to a byte array!");
108: }
109: }
110:
111: /**
112: * By default, unwraps IndirectlySerialized objects, returning the original
113: */
114: public static Object fromByteArray(byte[] bytes)
115: throws IOException, ClassNotFoundException {
116: Object out = deserializeFromByteArray(bytes);
117: if (out instanceof IndirectlySerialized)
118: return ((IndirectlySerialized) out).getObject();
119: else
120: return out;
121: }
122:
123: public static Object fromByteArray(byte[] bytes,
124: boolean ignore_indirects) throws IOException,
125: ClassNotFoundException {
126: if (ignore_indirects)
127: return deserializeFromByteArray(bytes);
128: else
129: return fromByteArray(bytes);
130: }
131:
132: /**
133: * @deprecated use SerialializableUtils.fromByteArray() [shorter name is better!]
134: */
135: public static Object deserializeFromByteArray(byte[] bytes)
136: throws IOException, ClassNotFoundException {
137: ObjectInputStream in = new ObjectInputStream(
138: new ByteArrayInputStream(bytes));
139: return in.readObject();
140: }
141:
142: public static Object testSerializeDeserialize(Object o)
143: throws IOException, ClassNotFoundException {
144: return deepCopy(o);
145: }
146:
147: public static Object deepCopy(Object o) throws IOException,
148: ClassNotFoundException {
149: byte[] bytes = serializeToByteArray(o);
150: return deserializeFromByteArray(bytes);
151: }
152:
153: public final static Object unmarshallObjectFromFile(File file)
154: throws IOException, ClassNotFoundException {
155: ObjectInputStream in = null;
156: try {
157: in = new ObjectInputStream(new BufferedInputStream(
158: new FileInputStream(file)));
159: return in.readObject();
160: } finally {
161: InputStreamUtils.attemptClose(in);
162: }
163: }
164:
165: public final static void marshallObjectToFile(Object o, File file)
166: throws IOException {
167: ObjectOutputStream out = null;
168: try {
169: out = new ObjectOutputStream(new BufferedOutputStream(
170: new FileOutputStream(file)));
171: out.writeObject(o);
172: } finally {
173: OutputStreamUtils.attemptClose(out);
174: }
175: }
176: }
|