Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/time/Ser.java
38829 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;5758import java.io.Externalizable;59import java.io.IOException;60import java.io.InvalidClassException;61import java.io.ObjectInput;62import java.io.ObjectOutput;63import java.io.StreamCorruptedException;6465/**66* The shared serialization delegate for this package.67*68* @implNote69* This class wraps the object being serialized, and takes a byte representing the type of the class to70* be serialized. This byte can also be used for versioning the serialization format. In this case another71* byte flag would be used in order to specify an alternative version of the type format.72* For example {@code LOCAL_DATE_TYPE_VERSION_2 = 21}.73* <p>74* In order to serialize the object it writes its byte and then calls back to the appropriate class where75* the serialization is performed. In order to deserialize the object it read in the type byte, switching76* in order to select which class to call back into.77* <p>78* The serialization format is determined on a per class basis. In the case of field based classes each79* of the fields is written out with an appropriate size format in descending order of the field's size. For80* example in the case of {@link LocalDate} year is written before month. Composite classes, such as81* {@link LocalDateTime} are serialized as one object.82* <p>83* This class is mutable and should be created once per serialization.84*85* @serial include86* @since 1.887*/88final class Ser implements Externalizable {8990/**91* Serialization version.92*/93private static final long serialVersionUID = -7683839454370182990L;9495static final byte DURATION_TYPE = 1;96static final byte INSTANT_TYPE = 2;97static final byte LOCAL_DATE_TYPE = 3;98static final byte LOCAL_TIME_TYPE = 4;99static final byte LOCAL_DATE_TIME_TYPE = 5;100static final byte ZONE_DATE_TIME_TYPE = 6;101static final byte ZONE_REGION_TYPE = 7;102static final byte ZONE_OFFSET_TYPE = 8;103static final byte OFFSET_TIME_TYPE = 9;104static final byte OFFSET_DATE_TIME_TYPE = 10;105static final byte YEAR_TYPE = 11;106static final byte YEAR_MONTH_TYPE = 12;107static final byte MONTH_DAY_TYPE = 13;108static final byte PERIOD_TYPE = 14;109110/** The type being serialized. */111private byte type;112/** The object being serialized. */113private Object object;114115/**116* Constructor for deserialization.117*/118public Ser() {119}120121/**122* Creates an instance for serialization.123*124* @param type the type125* @param object the object126*/127Ser(byte type, Object object) {128this.type = type;129this.object = object;130}131132//-----------------------------------------------------------------------133/**134* Implements the {@code Externalizable} interface to write the object.135* @serialData136*137* Each serializable class is mapped to a type that is the first byte138* 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="../../serialized-form.html#java.time.Duration">Duration.writeReplace</a>142* <li><a href="../../serialized-form.html#java.time.Instant">Instant.writeReplace</a>143* <li><a href="../../serialized-form.html#java.time.LocalDate">LocalDate.writeReplace</a>144* <li><a href="../../serialized-form.html#java.time.LocalDateTime">LocalDateTime.writeReplace</a>145* <li><a href="../../serialized-form.html#java.time.LocalTime">LocalTime.writeReplace</a>146* <li><a href="../../serialized-form.html#java.time.MonthDay">MonthDay.writeReplace</a>147* <li><a href="../../serialized-form.html#java.time.OffsetTime">OffsetTime.writeReplace</a>148* <li><a href="../../serialized-form.html#java.time.OffsetDateTime">OffsetDateTime.writeReplace</a>149* <li><a href="../../serialized-form.html#java.time.Period">Period.writeReplace</a>150* <li><a href="../../serialized-form.html#java.time.Year">Year.writeReplace</a>151* <li><a href="../../serialized-form.html#java.time.YearMonth">YearMonth.writeReplace</a>152* <li><a href="../../serialized-form.html#java.time.ZoneId">ZoneId.writeReplace</a>153* <li><a href="../../serialized-form.html#java.time.ZoneOffset">ZoneOffset.writeReplace</a>154* <li><a href="../../serialized-form.html#java.time.ZonedDateTime">ZonedDateTime.writeReplace</a>155* </ul>156*157* @param out the data stream to write to, not null158*/159@Override160public void writeExternal(ObjectOutput out) throws IOException {161writeInternal(type, object, out);162}163164static void writeInternal(byte type, Object object, ObjectOutput out) throws IOException {165out.writeByte(type);166switch (type) {167case DURATION_TYPE:168((Duration) object).writeExternal(out);169break;170case INSTANT_TYPE:171((Instant) object).writeExternal(out);172break;173case LOCAL_DATE_TYPE:174((LocalDate) object).writeExternal(out);175break;176case LOCAL_DATE_TIME_TYPE:177((LocalDateTime) object).writeExternal(out);178break;179case LOCAL_TIME_TYPE:180((LocalTime) object).writeExternal(out);181break;182case ZONE_REGION_TYPE:183((ZoneRegion) object).writeExternal(out);184break;185case ZONE_OFFSET_TYPE:186((ZoneOffset) object).writeExternal(out);187break;188case ZONE_DATE_TIME_TYPE:189((ZonedDateTime) object).writeExternal(out);190break;191case OFFSET_TIME_TYPE:192((OffsetTime) object).writeExternal(out);193break;194case OFFSET_DATE_TIME_TYPE:195((OffsetDateTime) object).writeExternal(out);196break;197case YEAR_TYPE:198((Year) object).writeExternal(out);199break;200case YEAR_MONTH_TYPE:201((YearMonth) object).writeExternal(out);202break;203case MONTH_DAY_TYPE:204((MonthDay) object).writeExternal(out);205break;206case PERIOD_TYPE:207((Period) object).writeExternal(out);208break;209default:210throw new InvalidClassException("Unknown serialized type");211}212}213214//-----------------------------------------------------------------------215/**216* Implements the {@code Externalizable} interface to read the object.217* @serialData218*219* The streamed type and parameters defined by the type's {@code writeReplace}220* method are read and passed to the corresponding static factory for the type221* to create a new instance. That instance is returned as the de-serialized222* {@code Ser} object.223*224* <ul>225* <li><a href="../../serialized-form.html#java.time.Duration">Duration</a> - {@code Duration.ofSeconds(seconds, nanos);}226* <li><a href="../../serialized-form.html#java.time.Instant">Instant</a> - {@code Instant.ofEpochSecond(seconds, nanos);}227* <li><a href="../../serialized-form.html#java.time.LocalDate">LocalDate</a> - {@code LocalDate.of(year, month, day);}228* <li><a href="../../serialized-form.html#java.time.LocalDateTime">LocalDateTime</a> - {@code LocalDateTime.of(date, time);}229* <li><a href="../../serialized-form.html#java.time.LocalTime">LocalTime</a> - {@code LocalTime.of(hour, minute, second, nano);}230* <li><a href="../../serialized-form.html#java.time.MonthDay">MonthDay</a> - {@code MonthDay.of(month, day);}231* <li><a href="../../serialized-form.html#java.time.OffsetTime">OffsetTime</a> - {@code OffsetTime.of(time, offset);}232* <li><a href="../../serialized-form.html#java.time.OffsetDateTime">OffsetDateTime</a> - {@code OffsetDateTime.of(dateTime, offset);}233* <li><a href="../../serialized-form.html#java.time.Period">Period</a> - {@code Period.of(years, months, days);}234* <li><a href="../../serialized-form.html#java.time.Year">Year</a> - {@code Year.of(year);}235* <li><a href="../../serialized-form.html#java.time.YearMonth">YearMonth</a> - {@code YearMonth.of(year, month);}236* <li><a href="../../serialized-form.html#java.time.ZonedDateTime">ZonedDateTime</a> - {@code ZonedDateTime.ofLenient(dateTime, offset, zone);}237* <li><a href="../../serialized-form.html#java.time.ZoneId">ZoneId</a> - {@code ZoneId.of(id);}238* <li><a href="../../serialized-form.html#java.time.ZoneOffset">ZoneOffset</a> - {@code (offsetByte == 127 ? ZoneOffset.ofTotalSeconds(in.readInt()) : ZoneOffset.ofTotalSeconds(offsetByte * 900));}239* </ul>240*241* @param in the data to read, not null242*/243public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {244type = in.readByte();245object = readInternal(type, in);246}247248static Object read(ObjectInput in) throws IOException, ClassNotFoundException {249byte type = in.readByte();250return readInternal(type, in);251}252253private static Object readInternal(byte type, ObjectInput in) throws IOException, ClassNotFoundException {254switch (type) {255case DURATION_TYPE: return Duration.readExternal(in);256case INSTANT_TYPE: return Instant.readExternal(in);257case LOCAL_DATE_TYPE: return LocalDate.readExternal(in);258case LOCAL_DATE_TIME_TYPE: return LocalDateTime.readExternal(in);259case LOCAL_TIME_TYPE: return LocalTime.readExternal(in);260case ZONE_DATE_TIME_TYPE: return ZonedDateTime.readExternal(in);261case ZONE_OFFSET_TYPE: return ZoneOffset.readExternal(in);262case ZONE_REGION_TYPE: return ZoneRegion.readExternal(in);263case OFFSET_TIME_TYPE: return OffsetTime.readExternal(in);264case OFFSET_DATE_TIME_TYPE: return OffsetDateTime.readExternal(in);265case YEAR_TYPE: return Year.readExternal(in);266case YEAR_MONTH_TYPE: return YearMonth.readExternal(in);267case MONTH_DAY_TYPE: return MonthDay.readExternal(in);268case PERIOD_TYPE: return Period.readExternal(in);269default:270throw new StreamCorruptedException("Unknown serialized type");271}272}273274/**275* Returns the object that will replace this one.276*277* @return the read object, should never be null278*/279private Object readResolve() {280return object;281}282283}284285286