Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/time/zone/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* 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.StreamCorruptedException;71import java.time.ZoneOffset;7273/**74* The shared serialization delegate for this package.75*76* @implNote77* This class is mutable and should be created once per serialization.78*79* @serial include80* @since 1.881*/82final class Ser implements Externalizable {8384/**85* Serialization version.86*/87private static final long serialVersionUID = -8885321777449118786L;8889/** Type for ZoneRules. */90static final byte ZRULES = 1;91/** Type for ZoneOffsetTransition. */92static final byte ZOT = 2;93/** Type for ZoneOffsetTransition. */94static final byte ZOTRULE = 3;9596/** The type being serialized. */97private byte type;98/** The object being serialized. */99private Object object;100101/**102* Constructor for deserialization.103*/104public Ser() {105}106107/**108* Creates an instance for serialization.109*110* @param type the type111* @param object the object112*/113Ser(byte type, Object object) {114this.type = type;115this.object = object;116}117118//-----------------------------------------------------------------------119/**120* Implements the {@code Externalizable} interface to write the object.121* @serialData122* Each serializable class is mapped to a type that is the first byte123* in the stream. Refer to each class {@code writeReplace}124* serialized form for the value of the type and sequence of values for the type.125*126* <ul>127* <li><a href="../../../serialized-form.html#java.time.zone.ZoneRules">ZoneRules.writeReplace</a>128* <li><a href="../../../serialized-form.html#java.time.zone.ZoneOffsetTransition">ZoneOffsetTransition.writeReplace</a>129* <li><a href="../../../serialized-form.html#java.time.zone.ZoneOffsetTransitionRule">ZoneOffsetTransitionRule.writeReplace</a>130* </ul>131*132* @param out the data stream to write to, not null133*/134@Override135public void writeExternal(ObjectOutput out) throws IOException {136writeInternal(type, object, out);137}138139static void write(Object object, DataOutput out) throws IOException {140writeInternal(ZRULES, object, out);141}142143private static void writeInternal(byte type, Object object, DataOutput out) throws IOException {144out.writeByte(type);145switch (type) {146case ZRULES:147((ZoneRules) object).writeExternal(out);148break;149case ZOT:150((ZoneOffsetTransition) object).writeExternal(out);151break;152case ZOTRULE:153((ZoneOffsetTransitionRule) object).writeExternal(out);154break;155default:156throw new InvalidClassException("Unknown serialized type");157}158}159160//-----------------------------------------------------------------------161/**162* Implements the {@code Externalizable} interface to read the object.163* @serialData164* The streamed type and parameters defined by the type's {@code writeReplace}165* method are read and passed to the corresponding static factory for the type166* to create a new instance. That instance is returned as the de-serialized167* {@code Ser} object.168*169* <ul>170* <li><a href="../../../serialized-form.html#java.time.zone.ZoneRules">ZoneRules</a>171* - {@code ZoneRules.of(standardTransitions, standardOffsets, savingsInstantTransitions, wallOffsets, lastRules);}172* <li><a href="../../../serialized-form.html#java.time.zone.ZoneOffsetTransition">ZoneOffsetTransition</a>173* - {@code ZoneOffsetTransition of(LocalDateTime.ofEpochSecond(epochSecond), offsetBefore, offsetAfter);}174* <li><a href="../../../serialized-form.html#java.time.zone.ZoneOffsetTransitionRule">ZoneOffsetTransitionRule</a>175* - {@code ZoneOffsetTransitionRule.of(month, dom, dow, time, timeEndOfDay, timeDefinition, standardOffset, offsetBefore, offsetAfter);}176* </ul>177* @param in the data to read, not null178*/179@Override180public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {181type = in.readByte();182object = readInternal(type, in);183}184185static Object read(DataInput in) throws IOException, ClassNotFoundException {186byte type = in.readByte();187return readInternal(type, in);188}189190private static Object readInternal(byte type, DataInput in) throws IOException, ClassNotFoundException {191switch (type) {192case ZRULES:193return ZoneRules.readExternal(in);194case ZOT:195return ZoneOffsetTransition.readExternal(in);196case ZOTRULE:197return ZoneOffsetTransitionRule.readExternal(in);198default:199throw new StreamCorruptedException("Unknown serialized type");200}201}202203/**204* Returns the object that will replace this one.205*206* @return the read object, should never be null207*/208private Object readResolve() {209return object;210}211212//-----------------------------------------------------------------------213/**214* Writes the state to the stream.215*216* @param offset the offset, not null217* @param out the output stream, not null218* @throws IOException if an error occurs219*/220static void writeOffset(ZoneOffset offset, DataOutput out) throws IOException {221final int offsetSecs = offset.getTotalSeconds();222int offsetByte = offsetSecs % 900 == 0 ? offsetSecs / 900 : 127; // compress to -72 to +72223out.writeByte(offsetByte);224if (offsetByte == 127) {225out.writeInt(offsetSecs);226}227}228229/**230* Reads the state from the stream.231*232* @param in the input stream, not null233* @return the created object, not null234* @throws IOException if an error occurs235*/236static ZoneOffset readOffset(DataInput in) throws IOException {237int offsetByte = in.readByte();238return (offsetByte == 127 ? ZoneOffset.ofTotalSeconds(in.readInt()) : ZoneOffset.ofTotalSeconds(offsetByte * 900));239}240241//-----------------------------------------------------------------------242/**243* Writes the state to the stream.244*245* @param epochSec the epoch seconds, not null246* @param out the output stream, not null247* @throws IOException if an error occurs248*/249static void writeEpochSec(long epochSec, DataOutput out) throws IOException {250if (epochSec >= -4575744000L && epochSec < 10413792000L && epochSec % 900 == 0) { // quarter hours between 1825 and 2300251int store = (int) ((epochSec + 4575744000L) / 900);252out.writeByte((store >>> 16) & 255);253out.writeByte((store >>> 8) & 255);254out.writeByte(store & 255);255} else {256out.writeByte(255);257out.writeLong(epochSec);258}259}260261/**262* Reads the state from the stream.263*264* @param in the input stream, not null265* @return the epoch seconds, not null266* @throws IOException if an error occurs267*/268static long readEpochSec(DataInput in) throws IOException {269int hiByte = in.readByte() & 255;270if (hiByte == 255) {271return in.readLong();272} else {273int midByte = in.readByte() & 255;274int loByte = in.readByte() & 255;275long tot = ((hiByte << 16) + (midByte << 8) + loByte);276return (tot * 900) - 4575744000L;277}278}279280}281282283