Path: blob/master/src/java.base/share/classes/java/time/zone/Ser.java
41159 views
/*1* Copyright (c) 2012, 2019, 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* This file is available under and governed by the GNU General Public27* License version 2 only, as published by the Free Software Foundation.28* However, the following notice accompanied the original version of this29* file:30*31* Copyright (c) 2011-2012, Stephen Colebourne & Michael Nascimento Santos32*33* All rights reserved.34*35* Redistribution and use in source and binary forms, with or without36* modification, are permitted provided that the following conditions are met:37*38* * Redistributions of source code must retain the above copyright notice,39* this list of conditions and the following disclaimer.40*41* * Redistributions in binary form must reproduce the above copyright notice,42* this list of conditions and the following disclaimer in the documentation43* and/or other materials provided with the distribution.44*45* * Neither the name of JSR-310 nor the names of its contributors46* may be used to endorse or promote products derived from this software47* without specific prior written permission.48*49* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS50* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT51* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR52* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR53* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,54* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,55* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR56* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF57* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING58* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS59* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.60*/61package java.time.zone;6263import java.io.DataInput;64import java.io.DataOutput;65import java.io.Externalizable;66import java.io.IOException;67import java.io.InvalidClassException;68import java.io.ObjectInput;69import java.io.ObjectOutput;70import java.io.Serializable;71import java.io.StreamCorruptedException;72import java.time.ZoneOffset;7374/**75* The shared serialization delegate for this package.76*77* @implNote78* This class is mutable and should be created once per serialization.79*80* @serial include81* @since 1.882*/83final class Ser implements Externalizable {8485/**86* Serialization version.87*/88private static final long serialVersionUID = -8885321777449118786L;8990/** Type for ZoneRules. */91static final byte ZRULES = 1;92/** Type for ZoneOffsetTransition. */93static final byte ZOT = 2;94/** Type for ZoneOffsetTransitionRule. */95static final byte ZOTRULE = 3;9697/** The type being serialized. */98private byte type;99/** The object being serialized. */100private Serializable object;101102/**103* Constructor for deserialization.104*/105public Ser() {106}107108/**109* Creates an instance for serialization.110*111* @param type the type112* @param object the object113*/114Ser(byte type, Serializable object) {115this.type = type;116this.object = object;117}118119//-----------------------------------------------------------------------120/**121* Implements the {@code Externalizable} interface to write the object.122* @serialData123* Each serializable class is mapped to a type that is the first byte124* in the stream. Refer to each class {@code writeReplace}125* serialized form for the value of the type and sequence of values for the type.126*127* <ul>128* <li><a href="{@docRoot}/serialized-form.html#java.time.zone.ZoneRules">ZoneRules.writeReplace</a>129* <li><a href="{@docRoot}/serialized-form.html#java.time.zone.ZoneOffsetTransition">ZoneOffsetTransition.writeReplace</a>130* <li><a href="{@docRoot}/serialized-form.html#java.time.zone.ZoneOffsetTransitionRule">ZoneOffsetTransitionRule.writeReplace</a>131* </ul>132*133* @param out the data stream to write to, not null134*/135@Override136public void writeExternal(ObjectOutput out) throws IOException {137writeInternal(type, object, out);138}139140static void write(Object object, DataOutput out) throws IOException {141writeInternal(ZRULES, object, out);142}143144private static void writeInternal(byte type, Object object, DataOutput out) throws IOException {145out.writeByte(type);146switch (type) {147case ZRULES:148((ZoneRules) object).writeExternal(out);149break;150case ZOT:151((ZoneOffsetTransition) object).writeExternal(out);152break;153case ZOTRULE:154((ZoneOffsetTransitionRule) object).writeExternal(out);155break;156default:157throw new InvalidClassException("Unknown serialized type");158}159}160161//-----------------------------------------------------------------------162/**163* Implements the {@code Externalizable} interface to read the object.164* @serialData165* The streamed type and parameters defined by the type's {@code writeReplace}166* method are read and passed to the corresponding static factory for the type167* to create a new instance. That instance is returned as the de-serialized168* {@code Ser} object.169*170* <ul>171* <li><a href="{@docRoot}/serialized-form.html#java.time.zone.ZoneRules">ZoneRules</a>172* - {@code ZoneRules.of(standardTransitions, standardOffsets, savingsInstantTransitions, wallOffsets, lastRules);}173* <li><a href="{@docRoot}/serialized-form.html#java.time.zone.ZoneOffsetTransition">ZoneOffsetTransition</a>174* - {@code ZoneOffsetTransition of(LocalDateTime.ofEpochSecond(epochSecond), offsetBefore, offsetAfter);}175* <li><a href="{@docRoot}/serialized-form.html#java.time.zone.ZoneOffsetTransitionRule">ZoneOffsetTransitionRule</a>176* - {@code ZoneOffsetTransitionRule.of(month, dom, dow, time, timeEndOfDay, timeDefinition, standardOffset, offsetBefore, offsetAfter);}177* </ul>178* @param in the data to read, not null179*/180@Override181public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {182type = in.readByte();183object = readInternal(type, in);184}185186static Serializable read(DataInput in) throws IOException, ClassNotFoundException {187byte type = in.readByte();188return readInternal(type, in);189}190191private static Serializable readInternal(byte type, DataInput in)192throws IOException, ClassNotFoundException {193switch (type) {194case ZRULES:195return ZoneRules.readExternal(in);196case ZOT:197return ZoneOffsetTransition.readExternal(in);198case ZOTRULE:199return ZoneOffsetTransitionRule.readExternal(in);200default:201throw new StreamCorruptedException("Unknown serialized type");202}203}204205/**206* Returns the object that will replace this one.207*208* @return the read object, should never be null209*/210private Object readResolve() {211return object;212}213214//-----------------------------------------------------------------------215/**216* Writes the state to the stream.217*218* @param offset the offset, not null219* @param out the output stream, not null220* @throws IOException if an error occurs221*/222static void writeOffset(ZoneOffset offset, DataOutput out) throws IOException {223final int offsetSecs = offset.getTotalSeconds();224int offsetByte = offsetSecs % 900 == 0 ? offsetSecs / 900 : 127; // compress to -72 to +72225out.writeByte(offsetByte);226if (offsetByte == 127) {227out.writeInt(offsetSecs);228}229}230231/**232* Reads the state from the stream.233*234* @param in the input stream, not null235* @return the created object, not null236* @throws IOException if an error occurs237*/238static ZoneOffset readOffset(DataInput in) throws IOException {239int offsetByte = in.readByte();240return (offsetByte == 127 ? ZoneOffset.ofTotalSeconds(in.readInt()) : ZoneOffset.ofTotalSeconds(offsetByte * 900));241}242243//-----------------------------------------------------------------------244/**245* Writes the state to the stream.246*247* @param epochSec the epoch seconds, not null248* @param out the output stream, not null249* @throws IOException if an error occurs250*/251static void writeEpochSec(long epochSec, DataOutput out) throws IOException {252if (epochSec >= -4575744000L && epochSec < 10413792000L && epochSec % 900 == 0) { // quarter hours between 1825 and 2300253int store = (int) ((epochSec + 4575744000L) / 900);254out.writeByte((store >>> 16) & 255);255out.writeByte((store >>> 8) & 255);256out.writeByte(store & 255);257} else {258out.writeByte(255);259out.writeLong(epochSec);260}261}262263/**264* Reads the state from the stream.265*266* @param in the input stream, not null267* @return the epoch seconds, not null268* @throws IOException if an error occurs269*/270static long readEpochSec(DataInput in) throws IOException {271int hiByte = in.readByte() & 255;272if (hiByte == 255) {273return in.readLong();274} else {275int midByte = in.readByte() & 255;276int loByte = in.readByte() & 255;277long tot = ((hiByte << 16) + (midByte << 8) + loByte);278return (tot * 900) - 4575744000L;279}280}281282}283284285