1 /* 2 * Copyright (c) 2012, 2019, 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.chrono; 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.Serializable; 65 import java.io.StreamCorruptedException; 66 import java.time.LocalDate; 67 import java.time.LocalDateTime; 68 69 /** 70 * The shared serialization delegate for this package. 71 * 72 * @implNote 73 * This class wraps the object being serialized, and takes a byte representing the type of the class to 74 * be serialized. This byte can also be used for versioning the serialization format. In this case another 75 * byte flag would be used in order to specify an alternative version of the type format. 76 * For example {@code CHRONO_TYPE_VERSION_2 = 21} 77 * <p> 78 * In order to serialize the object it writes its byte and then calls back to the appropriate class where 79 * the serialization is performed. In order to deserialize the object it read in the type byte, switching 80 * in order to select which class to call back into. 81 * <p> 82 * The serialization format is determined on a per class basis. In the case of field based classes each 83 * of the fields is written out with an appropriate size format in descending order of the field's size. For 84 * example in the case of {@link LocalDate} year is written before month. Composite classes, such as 85 * {@link LocalDateTime} are serialized as one object. Enum classes are serialized using the index of their 86 * element. 87 * <p> 88 * This class is mutable and should be created once per serialization. 89 * 90 * @serial include 91 * @since 1.8 92 */ 93 final class Ser implements Externalizable { 94 95 /** 96 * Serialization version. 97 */ 98 @java.io.Serial 99 private static final long serialVersionUID = -6103370247208168577L; 100 101 static final byte CHRONO_TYPE = 1; 102 static final byte CHRONO_LOCAL_DATE_TIME_TYPE = 2; 103 static final byte CHRONO_ZONE_DATE_TIME_TYPE = 3; 104 static final byte JAPANESE_DATE_TYPE = 4; 105 static final byte JAPANESE_ERA_TYPE = 5; 106 static final byte HIJRAH_DATE_TYPE = 6; 107 static final byte MINGUO_DATE_TYPE = 7; 108 static final byte THAIBUDDHIST_DATE_TYPE = 8; 109 static final byte CHRONO_PERIOD_TYPE = 9; 110 111 /** The type being serialized. */ 112 private byte type; 113 /** The object being serialized. */ 114 private Serializable object; 115 116 /** 117 * Constructor for deserialization. 118 */ Ser()119 public Ser() { 120 } 121 122 /** 123 * Creates an instance for serialization. 124 * 125 * @param type the type 126 * @param object the object 127 */ Ser(byte type, Serializable object)128 Ser(byte type, Serializable object) { 129 this.type = type; 130 this.object = object; 131 } 132 133 //----------------------------------------------------------------------- 134 /** 135 * Implements the {@code Externalizable} interface to write the object. 136 * @serialData 137 * Each serializable class is mapped to a type that is the first byte 138 * in the stream. Refer to each class {@code writeReplace} 139 * serialized form for the value of the type and sequence of values for the type. 140 * <ul> 141 * <li><a href="{@docRoot}/serialized-form.html#java.time.chrono.HijrahChronology">HijrahChronology.writeReplace</a> 142 * <li><a href="{@docRoot}/serialized-form.html#java.time.chrono.IsoChronology">IsoChronology.writeReplace</a> 143 * <li><a href="{@docRoot}/serialized-form.html#java.time.chrono.JapaneseChronology">JapaneseChronology.writeReplace</a> 144 * <li><a href="{@docRoot}/serialized-form.html#java.time.chrono.MinguoChronology">MinguoChronology.writeReplace</a> 145 * <li><a href="{@docRoot}/serialized-form.html#java.time.chrono.ThaiBuddhistChronology">ThaiBuddhistChronology.writeReplace</a> 146 * <li><a href="{@docRoot}/serialized-form.html#java.time.chrono.ChronoLocalDateTimeImpl">ChronoLocalDateTime.writeReplace</a> 147 * <li><a href="{@docRoot}/serialized-form.html#java.time.chrono.ChronoZonedDateTimeImpl">ChronoZonedDateTime.writeReplace</a> 148 * <li><a href="{@docRoot}/serialized-form.html#java.time.chrono.JapaneseDate">JapaneseDate.writeReplace</a> 149 * <li><a href="{@docRoot}/serialized-form.html#java.time.chrono.JapaneseEra">JapaneseEra.writeReplace</a> 150 * <li><a href="{@docRoot}/serialized-form.html#java.time.chrono.HijrahDate">HijrahDate.writeReplace</a> 151 * <li><a href="{@docRoot}/serialized-form.html#java.time.chrono.MinguoDate">MinguoDate.writeReplace</a> 152 * <li><a href="{@docRoot}/serialized-form.html#java.time.chrono.ThaiBuddhistDate">ThaiBuddhistDate.writeReplace</a> 153 * </ul> 154 * 155 * @param out the data stream to write to, not null 156 */ 157 @Override writeExternal(ObjectOutput out)158 public void writeExternal(ObjectOutput out) throws IOException { 159 writeInternal(type, object, out); 160 } 161 writeInternal(byte type, Object object, ObjectOutput out)162 private static void writeInternal(byte type, Object object, ObjectOutput out) throws IOException { 163 out.writeByte(type); 164 switch (type) { 165 case CHRONO_TYPE: 166 ((AbstractChronology) object).writeExternal(out); 167 break; 168 case CHRONO_LOCAL_DATE_TIME_TYPE: 169 ((ChronoLocalDateTimeImpl<?>) object).writeExternal(out); 170 break; 171 case CHRONO_ZONE_DATE_TIME_TYPE: 172 ((ChronoZonedDateTimeImpl<?>) object).writeExternal(out); 173 break; 174 case JAPANESE_DATE_TYPE: 175 ((JapaneseDate) object).writeExternal(out); 176 break; 177 case JAPANESE_ERA_TYPE: 178 ((JapaneseEra) object).writeExternal(out); 179 break; 180 case HIJRAH_DATE_TYPE: 181 ((HijrahDate) object).writeExternal(out); 182 break; 183 case MINGUO_DATE_TYPE: 184 ((MinguoDate) object).writeExternal(out); 185 break; 186 case THAIBUDDHIST_DATE_TYPE: 187 ((ThaiBuddhistDate) object).writeExternal(out); 188 break; 189 case CHRONO_PERIOD_TYPE: 190 ((ChronoPeriodImpl) object).writeExternal(out); 191 break; 192 default: 193 throw new InvalidClassException("Unknown serialized type"); 194 } 195 } 196 197 //----------------------------------------------------------------------- 198 /** 199 * Implements the {@code Externalizable} interface to read the object. 200 * @serialData 201 * The streamed type and parameters defined by the type's {@code writeReplace} 202 * method are read and passed to the corresponding static factory for the type 203 * to create a new instance. That instance is returned as the de-serialized 204 * {@code Ser} object. 205 * 206 * <ul> 207 * <li><a href="{@docRoot}/serialized-form.html#java.time.chrono.HijrahChronology">HijrahChronology</a> - 208 * Chronology.of(id) 209 * <li><a href="{@docRoot}/serialized-form.html#java.time.chrono.IsoChronology">IsoChronology</a> - 210 * Chronology.of(id) 211 * <li><a href="{@docRoot}/serialized-form.html#java.time.chrono.JapaneseChronology">JapaneseChronology</a> - 212 * Chronology.of(id) 213 * <li><a href="{@docRoot}/serialized-form.html#java.time.chrono.MinguoChronology">MinguoChronology</a> - 214 * Chronology.of(id) 215 * <li><a href="{@docRoot}/serialized-form.html#java.time.chrono.ThaiBuddhistChronology">ThaiBuddhistChronology</a> - 216 * Chronology.of(id) 217 * <li><a href="{@docRoot}/serialized-form.html#java.time.chrono.ChronoLocalDateTimeImpl">ChronoLocalDateTime</a> - 218 * date.atTime(time) 219 * <li><a href="{@docRoot}/serialized-form.html#java.time.chrono.ChronoZonedDateTimeImpl">ChronoZonedDateTime</a> - 220 * dateTime.atZone(offset).withZoneSameLocal(zone) 221 * <li><a href="{@docRoot}/serialized-form.html#java.time.chrono.JapaneseDate">JapaneseDate</a> - 222 * JapaneseChronology.INSTANCE.date(year, month, dayOfMonth) 223 * <li><a href="{@docRoot}/serialized-form.html#java.time.chrono.JapaneseEra">JapaneseEra</a> - 224 * JapaneseEra.of(eraValue) 225 * <li><a href="{@docRoot}/serialized-form.html#java.time.chrono.HijrahDate">HijrahDate</a> - 226 * HijrahChronology chrono.date(year, month, dayOfMonth) 227 * <li><a href="{@docRoot}/serialized-form.html#java.time.chrono.MinguoDate">MinguoDate</a> - 228 * MinguoChronology.INSTANCE.date(year, month, dayOfMonth) 229 * <li><a href="{@docRoot}/serialized-form.html#java.time.chrono.ThaiBuddhistDate">ThaiBuddhistDate</a> - 230 * ThaiBuddhistChronology.INSTANCE.date(year, month, dayOfMonth) 231 * </ul> 232 * 233 * @param in the data stream to read from, not null 234 */ 235 @Override readExternal(ObjectInput in)236 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { 237 type = in.readByte(); 238 object = readInternal(type, in); 239 } 240 read(ObjectInput in)241 static Serializable read(ObjectInput in) throws IOException, ClassNotFoundException { 242 byte type = in.readByte(); 243 return readInternal(type, in); 244 } 245 readInternal(byte type, ObjectInput in)246 private static Serializable readInternal(byte type, ObjectInput in) 247 throws IOException, ClassNotFoundException { 248 switch (type) { 249 case CHRONO_TYPE: return (Serializable)AbstractChronology.readExternal(in); 250 case CHRONO_LOCAL_DATE_TIME_TYPE: return (Serializable)ChronoLocalDateTimeImpl.readExternal(in); 251 case CHRONO_ZONE_DATE_TIME_TYPE: return (Serializable)ChronoZonedDateTimeImpl.readExternal(in); 252 case JAPANESE_DATE_TYPE: return JapaneseDate.readExternal(in); 253 case JAPANESE_ERA_TYPE: return JapaneseEra.readExternal(in); 254 case HIJRAH_DATE_TYPE: return HijrahDate.readExternal(in); 255 case MINGUO_DATE_TYPE: return MinguoDate.readExternal(in); 256 case THAIBUDDHIST_DATE_TYPE: return ThaiBuddhistDate.readExternal(in); 257 case CHRONO_PERIOD_TYPE: return ChronoPeriodImpl.readExternal(in); 258 default: throw new StreamCorruptedException("Unknown serialized type"); 259 } 260 } 261 262 /** 263 * Returns the object that will replace this one. 264 * 265 * @return the read object, should never be null 266 */ 267 @java.io.Serial readResolve()268 private Object readResolve() { 269 return object; 270 } 271 272 } 273