Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/time/chrono/Ser.java
38918 views
/*1* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* Copyright (c) 2011-2012, Stephen Colebourne & Michael Nascimento Santos27*28* All rights reserved.29*30* Redistribution and use in source and binary forms, with or without31* modification, are permitted provided that the following conditions are met:32*33* * Redistributions of source code must retain the above copyright notice,34* this list of conditions and the following disclaimer.35*36* * Redistributions in binary form must reproduce the above copyright notice,37* this list of conditions and the following disclaimer in the documentation38* and/or other materials provided with the distribution.39*40* * Neither the name of JSR-310 nor the names of its contributors41* may be used to endorse or promote products derived from this software42* without specific prior written permission.43*44* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS45* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT46* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR47* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR48* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,49* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,50* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR51* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF52* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING53* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS54* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.55*/56package java.time.chrono;5758import java.io.Externalizable;59import java.io.IOException;60import java.io.InvalidClassException;61import java.io.ObjectInput;62import java.io.ObjectOutput;63import java.io.StreamCorruptedException;64import java.time.LocalDate;65import java.time.LocalDateTime;6667/**68* The shared serialization delegate for this package.69*70* @implNote71* This class wraps the object being serialized, and takes a byte representing the type of the class to72* be serialized. This byte can also be used for versioning the serialization format. In this case another73* byte flag would be used in order to specify an alternative version of the type format.74* For example {@code CHRONO_TYPE_VERSION_2 = 21}75* <p>76* In order to serialize the object it writes its byte and then calls back to the appropriate class where77* the serialization is performed. In order to deserialize the object it read in the type byte, switching78* in order to select which class to call back into.79* <p>80* The serialization format is determined on a per class basis. In the case of field based classes each81* of the fields is written out with an appropriate size format in descending order of the field's size. For82* example in the case of {@link LocalDate} year is written before month. Composite classes, such as83* {@link LocalDateTime} are serialized as one object. Enum classes are serialized using the index of their84* element.85* <p>86* This class is mutable and should be created once per serialization.87*88* @serial include89* @since 1.890*/91final class Ser implements Externalizable {9293/**94* Serialization version.95*/96private static final long serialVersionUID = -6103370247208168577L;9798static final byte CHRONO_TYPE = 1;99static final byte CHRONO_LOCAL_DATE_TIME_TYPE = 2;100static final byte CHRONO_ZONE_DATE_TIME_TYPE = 3;101static final byte JAPANESE_DATE_TYPE = 4;102static final byte JAPANESE_ERA_TYPE = 5;103static final byte HIJRAH_DATE_TYPE = 6;104static final byte MINGUO_DATE_TYPE = 7;105static final byte THAIBUDDHIST_DATE_TYPE = 8;106static final byte CHRONO_PERIOD_TYPE = 9;107108/** The type being serialized. */109private byte type;110/** The object being serialized. */111private Object object;112113/**114* Constructor for deserialization.115*/116public Ser() {117}118119/**120* Creates an instance for serialization.121*122* @param type the type123* @param object the object124*/125Ser(byte type, Object object) {126this.type = type;127this.object = object;128}129130//-----------------------------------------------------------------------131/**132* Implements the {@code Externalizable} interface to write the object.133* @serialData134* Each serializable class is mapped to a type that is the first byte135* in the stream. Refer to each class {@code writeReplace}136* serialized form for the value of the type and sequence of values for the type.137* <ul>138* <li><a href="../../../serialized-form.html#java.time.chrono.HijrahChronology">HijrahChronology.writeReplace</a>139* <li><a href="../../../serialized-form.html#java.time.chrono.IsoChronology">IsoChronology.writeReplace</a>140* <li><a href="../../../serialized-form.html#java.time.chrono.JapaneseChronology">JapaneseChronology.writeReplace</a>141* <li><a href="../../../serialized-form.html#java.time.chrono.MinguoChronology">MinguoChronology.writeReplace</a>142* <li><a href="../../../serialized-form.html#java.time.chrono.ThaiBuddhistChronology">ThaiBuddhistChronology.writeReplace</a>143* <li><a href="../../../serialized-form.html#java.time.chrono.ChronoLocalDateTimeImpl">ChronoLocalDateTime.writeReplace</a>144* <li><a href="../../../serialized-form.html#java.time.chrono.ChronoZonedDateTimeImpl">ChronoZonedDateTime.writeReplace</a>145* <li><a href="../../../serialized-form.html#java.time.chrono.JapaneseDate">JapaneseDate.writeReplace</a>146* <li><a href="../../../serialized-form.html#java.time.chrono.JapaneseEra">JapaneseEra.writeReplace</a>147* <li><a href="../../../serialized-form.html#java.time.chrono.HijrahDate">HijrahDate.writeReplace</a>148* <li><a href="../../../serialized-form.html#java.time.chrono.MinguoDate">MinguoDate.writeReplace</a>149* <li><a href="../../../serialized-form.html#java.time.chrono.ThaiBuddhistDate">ThaiBuddhistDate.writeReplace</a>150* </ul>151*152* @param out the data stream to write to, not null153*/154@Override155public void writeExternal(ObjectOutput out) throws IOException {156writeInternal(type, object, out);157}158159private static void writeInternal(byte type, Object object, ObjectOutput out) throws IOException {160out.writeByte(type);161switch (type) {162case CHRONO_TYPE:163((AbstractChronology) object).writeExternal(out);164break;165case CHRONO_LOCAL_DATE_TIME_TYPE:166((ChronoLocalDateTimeImpl<?>) object).writeExternal(out);167break;168case CHRONO_ZONE_DATE_TIME_TYPE:169((ChronoZonedDateTimeImpl<?>) object).writeExternal(out);170break;171case JAPANESE_DATE_TYPE:172((JapaneseDate) object).writeExternal(out);173break;174case JAPANESE_ERA_TYPE:175((JapaneseEra) object).writeExternal(out);176break;177case HIJRAH_DATE_TYPE:178((HijrahDate) object).writeExternal(out);179break;180case MINGUO_DATE_TYPE:181((MinguoDate) object).writeExternal(out);182break;183case THAIBUDDHIST_DATE_TYPE:184((ThaiBuddhistDate) object).writeExternal(out);185break;186case CHRONO_PERIOD_TYPE:187((ChronoPeriodImpl) object).writeExternal(out);188break;189default:190throw new InvalidClassException("Unknown serialized type");191}192}193194//-----------------------------------------------------------------------195/**196* Implements the {@code Externalizable} interface to read the object.197* @serialData198* The streamed type and parameters defined by the type's {@code writeReplace}199* method are read and passed to the corresponding static factory for the type200* to create a new instance. That instance is returned as the de-serialized201* {@code Ser} object.202*203* <ul>204* <li><a href="../../../serialized-form.html#java.time.chrono.HijrahChronology">HijrahChronology</a> - Chronology.of(id)205* <li><a href="../../../serialized-form.html#java.time.chrono.IsoChronology">IsoChronology</a> - Chronology.of(id)206* <li><a href="../../../serialized-form.html#java.time.chrono.JapaneseChronology">JapaneseChronology</a> - Chronology.of(id)207* <li><a href="../../../serialized-form.html#java.time.chrono.MinguoChronology">MinguoChronology</a> - Chronology.of(id)208* <li><a href="../../../serialized-form.html#java.time.chrono.ThaiBuddhistChronology">ThaiBuddhistChronology</a> - Chronology.of(id)209* <li><a href="../../../serialized-form.html#java.time.chrono.ChronoLocalDateTimeImpl">ChronoLocalDateTime</a> - date.atTime(time)210* <li><a href="../../../serialized-form.html#java.time.chrono.ChronoZonedDateTimeImpl">ChronoZonedDateTime</a> - dateTime.atZone(offset).withZoneSameLocal(zone)211* <li><a href="../../../serialized-form.html#java.time.chrono.JapaneseDate">JapaneseDate</a> - JapaneseChronology.INSTANCE.date(year, month, dayOfMonth)212* <li><a href="../../../serialized-form.html#java.time.chrono.JapaneseEra">JapaneseEra</a> - JapaneseEra.of(eraValue)213* <li><a href="../../../serialized-form.html#java.time.chrono.HijrahDate">HijrahDate</a> - HijrahChronology chrono.date(year, month, dayOfMonth)214* <li><a href="../../../serialized-form.html#java.time.chrono.MinguoDate">MinguoDate</a> - MinguoChronology.INSTANCE.date(year, month, dayOfMonth)215* <li><a href="../../../serialized-form.html#java.time.chrono.ThaiBuddhistDate">ThaiBuddhistDate</a> - ThaiBuddhistChronology.INSTANCE.date(year, month, dayOfMonth)216* </ul>217*218* @param in the data stream to read from, not null219*/220@Override221public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {222type = in.readByte();223object = readInternal(type, in);224}225226static Object read(ObjectInput in) throws IOException, ClassNotFoundException {227byte type = in.readByte();228return readInternal(type, in);229}230231private static Object readInternal(byte type, ObjectInput in) throws IOException, ClassNotFoundException {232switch (type) {233case CHRONO_TYPE: return AbstractChronology.readExternal(in);234case CHRONO_LOCAL_DATE_TIME_TYPE: return ChronoLocalDateTimeImpl.readExternal(in);235case CHRONO_ZONE_DATE_TIME_TYPE: return ChronoZonedDateTimeImpl.readExternal(in);236case JAPANESE_DATE_TYPE: return JapaneseDate.readExternal(in);237case JAPANESE_ERA_TYPE: return JapaneseEra.readExternal(in);238case HIJRAH_DATE_TYPE: return HijrahDate.readExternal(in);239case MINGUO_DATE_TYPE: return MinguoDate.readExternal(in);240case THAIBUDDHIST_DATE_TYPE: return ThaiBuddhistDate.readExternal(in);241case CHRONO_PERIOD_TYPE: return ChronoPeriodImpl.readExternal(in);242default: throw new StreamCorruptedException("Unknown serialized type");243}244}245246/**247* Returns the object that will replace this one.248*249* @return the read object, should never be null250*/251private Object readResolve() {252return object;253}254255}256257258