1 /*
   2  * Copyright (c) 2012, 2013, 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  * This file is available under and governed by the GNU General Public
  28  * License version 2 only, as published by the Free Software Foundation.
  29  * However, the following notice accompanied the original version of this
  30  * file:
  31  *
  32  * Copyright (c) 2011-2012, Stephen Colebourne & Michael Nascimento Santos
  33  *
  34  * All rights reserved.
  35  *
  36  * Redistribution and use in source and binary forms, with or without
  37  * modification, are permitted provided that the following conditions are met:
  38  *
  39  *  * Redistributions of source code must retain the above copyright notice,
  40  *    this list of conditions and the following disclaimer.
  41  *
  42  *  * Redistributions in binary form must reproduce the above copyright notice,
  43  *    this list of conditions and the following disclaimer in the documentation
  44  *    and/or other materials provided with the distribution.
  45  *
  46  *  * Neither the name of JSR-310 nor the names of its contributors
  47  *    may be used to endorse or promote products derived from this software
  48  *    without specific prior written permission.
  49  *
  50  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  51  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  52  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  53  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  54  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  55  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  56  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  57  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  58  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  59  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  60  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  61  */
  62 package java.time.zone;
  63 
  64 import java.io.DataInput;
  65 import java.io.DataOutput;
  66 import java.io.Externalizable;
  67 import java.io.IOException;
  68 import java.io.InvalidClassException;
  69 import java.io.ObjectInput;
  70 import java.io.ObjectOutput;
  71 import java.io.StreamCorruptedException;
  72 import java.time.ZoneOffset;
  73 
  74 /**
  75  * The shared serialization delegate for this package.
  76  *
  77  * <h3>Implementation notes</h3>
  78  * This class is mutable and should be created once per serialization.
  79  *
  80  * @serial include
  81  * @since 1.8
  82  */
  83 final class Ser implements Externalizable {
  84 
  85     /**
  86      * Serialization version.
  87      */
  88     private static final long serialVersionUID = -8885321777449118786L;
  89 
  90     /** Type for ZoneRules. */
  91     static final byte ZRULES = 1;
  92     /** Type for ZoneOffsetTransition. */
  93     static final byte ZOT = 2;
  94     /** Type for ZoneOffsetTransition. */
  95     static final byte ZOTRULE = 3;
  96 
  97     /** The type being serialized. */
  98     private byte type;
  99     /** The object being serialized. */
 100     private Object object;
 101 
 102     /**
 103      * Constructor for deserialization.
 104      */
 105     public Ser() {
 106     }
 107 
 108     /**
 109      * Creates an instance for serialization.
 110      *
 111      * @param type  the type
 112      * @param object  the object
 113      */
 114     Ser(byte type, Object object) {
 115         this.type = type;
 116         this.object = object;
 117     }
 118 
 119     //-----------------------------------------------------------------------
 120     /**
 121      * Implements the {@code Externalizable} interface to write the object.
 122      *
 123      * @param out  the data stream to write to, not null
 124      */
 125     public void writeExternal(ObjectOutput out) throws IOException {
 126         writeInternal(type, object, out);
 127     }
 128 
 129     static void write(Object object, DataOutput out) throws IOException {
 130         writeInternal(ZRULES, object, out);
 131     }
 132 
 133     private static void writeInternal(byte type, Object object, DataOutput out) throws IOException {
 134         out.writeByte(type);
 135         switch (type) {
 136             case ZRULES:
 137                 ((ZoneRules) object).writeExternal(out);
 138                 break;
 139             case ZOT:
 140                 ((ZoneOffsetTransition) object).writeExternal(out);
 141                 break;
 142             case ZOTRULE:
 143                 ((ZoneOffsetTransitionRule) object).writeExternal(out);
 144                 break;
 145             default:
 146                 throw new InvalidClassException("Unknown serialized type");
 147         }
 148     }
 149 
 150     //-----------------------------------------------------------------------
 151     /**
 152      * Implements the {@code Externalizable} interface to read the object.
 153      *
 154      * @param in  the data to read, not null
 155      */
 156     public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
 157         type = in.readByte();
 158         object = readInternal(type, in);
 159     }
 160 
 161     static Object read(DataInput in) throws IOException, ClassNotFoundException {
 162         byte type = in.readByte();
 163         return readInternal(type, in);
 164     }
 165 
 166     private static Object readInternal(byte type, DataInput in) throws IOException, ClassNotFoundException {
 167         switch (type) {
 168             case ZRULES:
 169                 return ZoneRules.readExternal(in);
 170             case ZOT:
 171                 return ZoneOffsetTransition.readExternal(in);
 172             case ZOTRULE:
 173                 return ZoneOffsetTransitionRule.readExternal(in);
 174             default:
 175                 throw new StreamCorruptedException("Unknown serialized type");
 176         }
 177     }
 178 
 179     /**
 180      * Returns the object that will replace this one.
 181      *
 182      * @return the read object, should never be null
 183      */
 184     private Object readResolve() {
 185          return object;
 186     }
 187 
 188     //-----------------------------------------------------------------------
 189     /**
 190      * Writes the state to the stream.
 191      *
 192      * @param offset  the offset, not null
 193      * @param out  the output stream, not null
 194      * @throws IOException if an error occurs
 195      */
 196     static void writeOffset(ZoneOffset offset, DataOutput out) throws IOException {
 197         final int offsetSecs = offset.getTotalSeconds();
 198         int offsetByte = offsetSecs % 900 == 0 ? offsetSecs / 900 : 127;  // compress to -72 to +72
 199         out.writeByte(offsetByte);
 200         if (offsetByte == 127) {
 201             out.writeInt(offsetSecs);
 202         }
 203     }
 204 
 205     /**
 206      * Reads the state from the stream.
 207      *
 208      * @param in  the input stream, not null
 209      * @return the created object, not null
 210      * @throws IOException if an error occurs
 211      */
 212     static ZoneOffset readOffset(DataInput in) throws IOException {
 213         int offsetByte = in.readByte();
 214         return (offsetByte == 127 ? ZoneOffset.ofTotalSeconds(in.readInt()) : ZoneOffset.ofTotalSeconds(offsetByte * 900));
 215     }
 216 
 217     //-----------------------------------------------------------------------
 218     /**
 219      * Writes the state to the stream.
 220      *
 221      * @param epochSec  the epoch seconds, not null
 222      * @param out  the output stream, not null
 223      * @throws IOException if an error occurs
 224      */
 225     static void writeEpochSec(long epochSec, DataOutput out) throws IOException {
 226         if (epochSec >= -4575744000L && epochSec < 10413792000L && epochSec % 900 == 0) {  // quarter hours between 1825 and 2300
 227             int store = (int) ((epochSec + 4575744000L) / 900);
 228             out.writeByte((store >>> 16) & 255);
 229             out.writeByte((store >>> 8) & 255);
 230             out.writeByte(store & 255);
 231         } else {
 232             out.writeByte(255);
 233             out.writeLong(epochSec);
 234         }
 235     }
 236 
 237     /**
 238      * Reads the state from the stream.
 239      *
 240      * @param in  the input stream, not null
 241      * @return the epoch seconds, not null
 242      * @throws IOException if an error occurs
 243      */
 244     static long readEpochSec(DataInput in) throws IOException {
 245         int hiByte = in.readByte() & 255;
 246         if (hiByte == 255) {
 247             return in.readLong();
 248         } else {
 249             int midByte = in.readByte() & 255;
 250             int loByte = in.readByte() & 255;
 251             long tot = ((hiByte << 16) + (midByte << 8) + loByte);
 252             return (tot * 900) - 4575744000L;
 253         }
 254     }
 255 
 256 }