View Javadoc
1   /*
2    * Copyright 2013 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.orangesignal.csv;
18  
19  import java.io.ByteArrayInputStream;
20  import java.io.ByteArrayOutputStream;
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.io.ObjectInputStream;
24  import java.io.ObjectOutputStream;
25  import java.io.OutputStream;
26  import java.io.Serializable;
27  
28  /**
29   * 直列化操作補助ユーティリティを提供します。
30   * 
31   * @author Koji Sugisawa
32   * @since 1.1
33   */
34  abstract class SerializationUtils {
35  
36  	/**
37  	 * デフォルトコンストラクタです。
38  	 */
39  	protected SerializationUtils() {
40  	}
41  
42  	/**
43  	 * <p>直列化を使用したディープクローンを行います。</p>
44  	 * 
45  	 * <p>
46  	 * このメソッドは各オブジェクトが実装する clone メソッドよりも処理に時間を要します。
47  	 * しかしディープクローンを実装していない構造の複雑なオブジェクトに対するシンプルな代用として使用することができます。
48  	 * もちろん全てのオブジェクトは  {@code Serializable} である必要があります。
49  	 * </p>
50  	 * 
51  	 * @param object クローンする <code>Serializable</code> オブジェクト
52  	 * @return クローンされたオブジェクト
53  	 * @throws RuntimeException 直列化に失敗した場合
54  	 */
55  	public static Object clone(final Serializable object) {
56  		return deserialize(serialize(object));
57  	}
58  
59  	/**
60  	 * オブジェクトを指定されたストリームに対して直列化します。
61  	 * このストリームはオブジェクトが書き出された後一旦クローズされます。
62  	 * これによってアプリケーション内での finally 句、例外のハンドリングの実装を行う必要がなくなります。
63  	 * 
64  	 * @param obj バイトに直列化するオブジェクト
65  	 * @param outputStream 書込む null ではないストリーム
66  	 * @throws IllegalArgumentException {@code outputStream} が {@code null} の場合
67  	 * @throws IllegalStateException 直列化に失敗した場合
68  	 */
69  	public static void serialize(final Serializable obj, final OutputStream outputStream) {
70  		if (outputStream == null) {
71  			throw new IllegalArgumentException("The OutputStream must not be null");
72  		}
73  		ObjectOutputStream out = null;
74  		try {
75  			out = new ObjectOutputStream(outputStream);
76  			out.writeObject(obj);
77  		} catch (final IOException e) {
78  			throw new IllegalStateException(e.getMessage(), e);
79  		} finally {
80  			Csv.closeQuietly(out);
81  		}
82  	}
83  
84  	/**
85  	 * オブジェクトを直列化しバイト配列に格納します。
86  	 * 
87  	 * @param obj バイトに直列化するオブジェクト
88  	 * @return 直列化したバイト配列
89  	 * @throws RuntimeException 直列化に失敗した場合
90  	 */
91  	public static byte[] serialize(final Serializable obj) {
92  		final ByteArrayOutputStream baos = new ByteArrayOutputStream(512);
93  		serialize(obj, baos);
94  		return baos.toByteArray();
95  	}
96  
97  	/**
98  	 * 指定されたストリームからオブジェクトを直列化復元します。
99  	 * このストリームはオブジェクトが復元された後一旦クローズされます。
100 	 * これによってアプリケーション内での finally 句、例外のハンドリングの実装を行う必要がなくなります。
101 	 * 
102 	 * @param inputStream 直列化されたオブジェクトの入力ストリーム
103 	 * @return 直列化復元されたオブジェクト
104 	 * @throws IllegalArgumentException {@code inputStream} が {@code null} の場合
105 	 * @throws IllegalStateException 直列化復元に失敗した場合
106 	 */
107 	public static Object deserialize(final InputStream inputStream) {
108 		if (inputStream == null) {
109 			throw new IllegalArgumentException("The InputStream must not be null");
110 		}
111 		ObjectInputStream in = null;
112 		try {
113 			in = new ObjectInputStream(inputStream);
114 			return in.readObject();
115 		} catch (final ClassNotFoundException e) {
116 			throw new IllegalStateException(e.getMessage(), e);
117 		} catch (final IOException e) {
118 			throw new IllegalStateException(e.getMessage(), e);
119 		} finally {
120 			Csv.closeQuietly(in);
121 		}
122 	}
123 
124 	/**
125 	 * <p>Deserializes a single {@code Object} from an array of bytes.</p>
126 	 * 
127 	 * @param objectData 直列化されたオブジェクト
128 	 * @return 直列化復元されたオブジェクト
129 	 * @throws IllegalArgumentException {@code objectData} が {@code null} の場合
130 	 * @throws RuntimeException 直列化復元に失敗した場合
131 	 */
132 	public static Object deserialize(final byte[] objectData) {
133 		if (objectData == null) {
134 			throw new IllegalArgumentException("The byte[] must not be null");
135 		}
136 		return deserialize(new ByteArrayInputStream(objectData));
137 	}
138 
139 }