1 /* |
|
2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 /* |
|
27 * Copyright (c) 2011-2012, Stephen Colebourne & Michael Nascimento Santos |
|
28 * |
|
29 * All rights reserved. |
|
30 * |
|
31 * Redistribution and use in source and binary forms, with or without |
|
32 * modification, are permitted provided that the following conditions are met: |
|
33 * |
|
34 * * Redistributions of source code must retain the above copyright notice, |
|
35 * this list of conditions and the following disclaimer. |
|
36 * |
|
37 * * Redistributions in binary form must reproduce the above copyright notice, |
|
38 * this list of conditions and the following disclaimer in the documentation |
|
39 * and/or other materials provided with the distribution. |
|
40 * |
|
41 * * Neither the name of JSR-310 nor the names of its contributors |
|
42 * may be used to endorse or promote products derived from this software |
|
43 * without specific prior written permission. |
|
44 * |
|
45 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
46 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
47 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
48 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
|
49 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
50 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
51 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
52 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
|
53 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
54 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
55 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
56 */ |
|
57 package java.time.temporal; |
|
58 |
|
59 import java.io.Externalizable; |
|
60 import java.io.IOException; |
|
61 import java.io.InvalidClassException; |
|
62 import java.io.ObjectInput; |
|
63 import java.io.ObjectOutput; |
|
64 import java.io.StreamCorruptedException; |
|
65 import java.time.LocalDate; |
|
66 import java.time.LocalDateTime; |
|
67 |
|
68 /** |
|
69 * The shared serialization delegate for this package. |
|
70 * |
|
71 * <h3>Implementation notes</h3> |
|
72 * This class wraps the object being serialized, and takes a byte representing the type of the class to |
|
73 * be serialized. This byte can also be used for versioning the serialization format. In this case another |
|
74 * byte flag would be used in order to specify an alternative version of the type format. |
|
75 * For example {@code CHRONO_TYPE_VERSION_2 = 21} |
|
76 * <p> |
|
77 * In order to serialise the object it writes its byte and then calls back to the appropriate class where |
|
78 * the serialisation is performed. In order to deserialise the object it read in the type byte, switching |
|
79 * in order to select which class to call back into. |
|
80 * <p> |
|
81 * The serialisation format is determined on a per class basis. In the case of field based classes each |
|
82 * of the fields is written out with an appropriate size format in descending order of the field's size. For |
|
83 * example in the case of {@link LocalDate} year is written before month. Composite classes, such as |
|
84 * {@link LocalDateTime} are serialised as one object. Enum classes are serialised using the index of their |
|
85 * element. |
|
86 * <p> |
|
87 * This class is mutable and should be created once per serialization. |
|
88 * |
|
89 * @serial include |
|
90 * @since 1.8 |
|
91 */ |
|
92 final class Ser implements Externalizable { |
|
93 |
|
94 /** |
|
95 * Serialization version. |
|
96 */ |
|
97 private static final long serialVersionUID = -6103370247208168577L; |
|
98 |
|
99 static final byte OFFSET_DATE_TYPE = 1; |
|
100 static final byte OFFSET_TIME_TYPE = 2; |
|
101 static final byte OFFSET_DATE_TIME_TYPE = 3; |
|
102 static final byte YEAR_TYPE = 4; |
|
103 static final byte YEAR_MONTH_TYPE = 5; |
|
104 static final byte MONTH_DAY_TYPE = 6; |
|
105 static final byte CHRONO_TYPE = 7; |
|
106 static final byte CHRONO_LOCAL_DATE_TIME_TYPE = 8; |
|
107 static final byte CHRONO_ZONE_DATE_TIME_TYPE = 9; |
|
108 static final byte SIMPLE_PERIOD_TYPE = 10; |
|
109 |
|
110 /** The type being serialized. */ |
|
111 private byte type; |
|
112 /** The object being serialized. */ |
|
113 private Object object; |
|
114 |
|
115 /** |
|
116 * Constructor for deserialization. |
|
117 */ |
|
118 public Ser() { |
|
119 } |
|
120 |
|
121 /** |
|
122 * Creates an instance for serialization. |
|
123 * |
|
124 * @param type the type |
|
125 * @param object the object |
|
126 */ |
|
127 Ser(byte type, Object object) { |
|
128 this.type = type; |
|
129 this.object = object; |
|
130 } |
|
131 |
|
132 //----------------------------------------------------------------------- |
|
133 /** |
|
134 * Implements the {@code Externalizable} interface to write the object. |
|
135 * |
|
136 * @param out the data stream to write to, not null |
|
137 */ |
|
138 @Override |
|
139 public void writeExternal(ObjectOutput out) throws IOException { |
|
140 writeInternal(type, object, out); |
|
141 } |
|
142 |
|
143 private static void writeInternal(byte type, Object object, ObjectOutput out) throws IOException { |
|
144 out.writeByte(type); |
|
145 switch (type) { |
|
146 case OFFSET_DATE_TYPE: |
|
147 ((OffsetDate) object).writeExternal(out); |
|
148 break; |
|
149 case OFFSET_TIME_TYPE: |
|
150 ((OffsetTime) object).writeExternal(out); |
|
151 break; |
|
152 case OFFSET_DATE_TIME_TYPE: |
|
153 ((OffsetDateTime) object).writeExternal(out); |
|
154 break; |
|
155 case YEAR_TYPE: |
|
156 ((Year) object).writeExternal(out); |
|
157 break; |
|
158 case YEAR_MONTH_TYPE: |
|
159 ((YearMonth) object).writeExternal(out); |
|
160 break; |
|
161 case MONTH_DAY_TYPE: |
|
162 ((MonthDay) object).writeExternal(out); |
|
163 break; |
|
164 case CHRONO_TYPE: |
|
165 ((Chrono<?>) object).writeExternal(out); |
|
166 break; |
|
167 case CHRONO_LOCAL_DATE_TIME_TYPE: |
|
168 ((ChronoLocalDateTimeImpl<?>) object).writeExternal(out); |
|
169 break; |
|
170 case CHRONO_ZONE_DATE_TIME_TYPE: |
|
171 ((ChronoZonedDateTimeImpl<?>) object).writeExternal(out); |
|
172 break; |
|
173 case SIMPLE_PERIOD_TYPE: |
|
174 ((SimplePeriod) object).writeExternal(out); |
|
175 break; |
|
176 default: |
|
177 throw new InvalidClassException("Unknown serialized type"); |
|
178 } |
|
179 } |
|
180 |
|
181 //----------------------------------------------------------------------- |
|
182 /** |
|
183 * Implements the {@code Externalizable} interface to read the object. |
|
184 * |
|
185 * @param in the data to read, not null |
|
186 */ |
|
187 @Override |
|
188 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { |
|
189 type = in.readByte(); |
|
190 object = readInternal(type, in); |
|
191 } |
|
192 |
|
193 static Object read(ObjectInput in) throws IOException, ClassNotFoundException { |
|
194 byte type = in.readByte(); |
|
195 return readInternal(type, in); |
|
196 } |
|
197 |
|
198 private static Object readInternal(byte type, ObjectInput in) throws IOException, ClassNotFoundException { |
|
199 switch (type) { |
|
200 case OFFSET_DATE_TYPE: return OffsetDate.readExternal(in); |
|
201 case OFFSET_TIME_TYPE: return OffsetTime.readExternal(in); |
|
202 case OFFSET_DATE_TIME_TYPE: return OffsetDateTime.readExternal(in); |
|
203 case YEAR_TYPE: return Year.readExternal(in); |
|
204 case YEAR_MONTH_TYPE: return YearMonth.readExternal(in); |
|
205 case MONTH_DAY_TYPE: return MonthDay.readExternal(in); |
|
206 case CHRONO_TYPE: return Chrono.readExternal(in); |
|
207 case CHRONO_LOCAL_DATE_TIME_TYPE: return ChronoLocalDateTimeImpl.readExternal(in); |
|
208 case CHRONO_ZONE_DATE_TIME_TYPE: return ChronoZonedDateTimeImpl.readExternal(in); |
|
209 case SIMPLE_PERIOD_TYPE: return SimplePeriod.readExternal(in); |
|
210 default: throw new StreamCorruptedException("Unknown serialized type"); |
|
211 } |
|
212 } |
|
213 |
|
214 /** |
|
215 * Returns the object that will replace this one. |
|
216 * |
|
217 * @return the read object, should never be null |
|
218 */ |
|
219 private Object readResolve() { |
|
220 return object; |
|
221 } |
|
222 |
|
223 } |
|