src/share/classes/java/time/chrono/Ser.java

Print this page




  37  *  * Redistributions in binary form must reproduce the above copyright notice,
  38  *    this list of conditions and the following disclaimer in the documentation
  39  *    and/or other materials provided with the distribution.
  40  *
  41  *  * Neither the name of JSR-310 nor the names of its contributors
  42  *    may be used to endorse or promote products derived from this software
  43  *    without specific prior written permission.
  44  *
  45  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  46  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  47  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  48  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  49  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  50  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  51  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  52  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  53  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  54  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  55  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  56  */
  57 package java.time.calendar;
  58 
  59 import java.io.Externalizable;
  60 import java.io.IOException;
  61 import java.io.InvalidClassException;
  62 import java.io.ObjectInput;
  63 import java.io.ObjectOutput;
  64 import java.io.StreamCorruptedException;
  65 import java.time.LocalDate;
  66 import java.time.LocalDateTime;
  67 
  68 /**
  69  * The shared serialization delegate for this package.
  70  *
  71  * <h3>Implementation notes</h3>
  72  * This class wraps the object being serialized, and takes a byte representing the type of the class to
  73  * be serialized.  This byte can also be used for versioning the serialization format.  In this case another
  74  * byte flag would be used in order to specify an alternative version of the type format.
  75  * For example {@code JAPANESE_DATE_TYPE_VERSION_2 = 21}.
  76  * <p>
  77  * In order to serialise the object it writes its byte and then calls back to the appropriate class where
  78  * the serialisation is performed.  In order to deserialise the object it read in the type byte, switching
  79  * in order to select which class to call back into.
  80  * <p>
  81  * The serialisation format is determined on a per class basis.  In the case of field based classes each
  82  * of the fields is written out with an appropriate size format in descending order of the field's size.  For
  83  * example in the case of {@link LocalDate} year is written before month.  Composite classes, such as
  84  * {@link LocalDateTime} are serialised as one object.  Enum classes are serialised using the index of their
  85  * element.
  86  * <p>
  87  * This class is mutable and should be created once per serialization.
  88  *
  89  * @serial include
  90  * @since 1.8
  91  */
  92 final class Ser implements Externalizable {
  93 
  94     /**
  95      * Serialization version.
  96      */
  97     private static final long serialVersionUID = 7857518227608961174L;
  98 
  99     static final byte JAPANESE_DATE_TYPE = 1;
 100     static final byte JAPANESE_ERA_TYPE = 2;
 101     static final byte HIJRAH_DATE_TYPE = 3;
 102     static final byte HIJRAH_ERA_TYPE = 4;
 103     static final byte MINGUO_DATE_TYPE = 5;
 104     static final byte MINGUO_ERA_TYPE = 6;
 105     static final byte THAIBUDDHIST_DATE_TYPE = 7;
 106     static final byte THAIBUDDHIST_ERA_TYPE = 8;



 107 
 108     /** The type being serialized. */
 109     private byte type;
 110     /** The object being serialized. */
 111     private Object object;
 112 
 113     /**
 114      * Constructor for deserialization.
 115      */
 116     public Ser() {
 117     }
 118 
 119     /**
 120      * Creates an instance for serialization.
 121      *
 122      * @param type  the type
 123      * @param object  the object
 124      */
 125     Ser(byte type, Object object) {
 126         this.type = type;
 127         this.object = object;
 128     }
 129 
 130     //-----------------------------------------------------------------------
 131     /**
 132      * Implements the {@code Externalizable} interface to write the object.
 133      *
 134      * @param out  the data stream to write to, not null
 135      */
 136     @Override
 137     public void writeExternal(ObjectOutput out) throws IOException {
 138         writeInternal(type, object, out);
 139     }
 140 
 141     private static void writeInternal(byte type, Object object, ObjectOutput out) throws IOException {
 142         out.writeByte(type);
 143         switch (type) {









 144             case JAPANESE_DATE_TYPE:
 145                 ((JapaneseDate) object).writeExternal(out);
 146                 break;
 147             case JAPANESE_ERA_TYPE:
 148                 ((JapaneseEra) object).writeExternal(out);
 149                 break;
 150             case HIJRAH_DATE_TYPE:
 151                 ((HijrahDate) object).writeExternal(out);
 152                 break;
 153             case HIJRAH_ERA_TYPE:
 154                 ((HijrahEra) object).writeExternal(out);
 155                 break;
 156             case MINGUO_DATE_TYPE:
 157                 ((MinguoDate) object).writeExternal(out);
 158                 break;
 159             case MINGUO_ERA_TYPE:
 160                 ((MinguoEra) object).writeExternal(out);
 161                 break;
 162             case THAIBUDDHIST_DATE_TYPE:
 163                 ((ThaiBuddhistDate) object).writeExternal(out);


 172 
 173     //-----------------------------------------------------------------------
 174     /**
 175      * Implements the {@code Externalizable} interface to read the object.
 176      *
 177      * @param in  the data to read, not null
 178      */
 179     @Override
 180     public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
 181         type = in.readByte();
 182         object = readInternal(type, in);
 183     }
 184 
 185     static Object read(ObjectInput in) throws IOException, ClassNotFoundException {
 186         byte type = in.readByte();
 187         return readInternal(type, in);
 188     }
 189 
 190     private static Object readInternal(byte type, ObjectInput in) throws IOException, ClassNotFoundException {
 191         switch (type) {



 192             case JAPANESE_DATE_TYPE:  return JapaneseDate.readExternal(in);
 193             case JAPANESE_ERA_TYPE: return JapaneseEra.readExternal(in);
 194             case HIJRAH_DATE_TYPE: return HijrahDate.readExternal(in);
 195             case HIJRAH_ERA_TYPE: return HijrahEra.readExternal(in);
 196             case MINGUO_DATE_TYPE: return MinguoDate.readExternal(in);
 197             case MINGUO_ERA_TYPE: return MinguoEra.readExternal(in);
 198             case THAIBUDDHIST_DATE_TYPE: return ThaiBuddhistDate.readExternal(in);
 199             case THAIBUDDHIST_ERA_TYPE: return ThaiBuddhistEra.readExternal(in);
 200             default:
 201                 throw new StreamCorruptedException("Unknown serialized type");
 202         }
 203     }
 204 
 205     /**
 206      * Returns the object that will replace this one.
 207      *
 208      * @return the read object, should never be null
 209      */
 210     private Object readResolve() {
 211          return object;
 212     }
 213 
 214 }


  37  *  * Redistributions in binary form must reproduce the above copyright notice,
  38  *    this list of conditions and the following disclaimer in the documentation
  39  *    and/or other materials provided with the distribution.
  40  *
  41  *  * Neither the name of JSR-310 nor the names of its contributors
  42  *    may be used to endorse or promote products derived from this software
  43  *    without specific prior written permission.
  44  *
  45  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  46  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  47  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  48  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  49  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  50  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  51  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  52  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  53  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  54  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  55  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  56  */
  57 package java.time.chrono;
  58 
  59 import java.io.Externalizable;
  60 import java.io.IOException;
  61 import java.io.InvalidClassException;
  62 import java.io.ObjectInput;
  63 import java.io.ObjectOutput;
  64 import java.io.StreamCorruptedException;
  65 import java.time.LocalDate;
  66 import java.time.LocalDateTime;
  67 
  68 /**
  69  * The shared serialization delegate for this package.
  70  *
  71  * <h3>Implementation notes</h3>
  72  * This class wraps the object being serialized, and takes a byte representing the type of the class to
  73  * be serialized.  This byte can also be used for versioning the serialization format.  In this case another
  74  * byte flag would be used in order to specify an alternative version of the type format.
  75  * For example {@code CHRONO_TYPE_VERSION_2 = 21}
  76  * <p>
  77  * In order to serialise the object it writes its byte and then calls back to the appropriate class where
  78  * the serialisation is performed.  In order to deserialise the object it read in the type byte, switching
  79  * in order to select which class to call back into.
  80  * <p>
  81  * The serialisation format is determined on a per class basis.  In the case of field based classes each
  82  * of the fields is written out with an appropriate size format in descending order of the field's size.  For
  83  * example in the case of {@link LocalDate} year is written before month.  Composite classes, such as
  84  * {@link LocalDateTime} are serialised as one object.  Enum classes are serialised using the index of their
  85  * element.
  86  * <p>
  87  * This class is mutable and should be created once per serialization.
  88  *
  89  * @serial include
  90  * @since 1.8
  91  */
  92 final class Ser implements Externalizable {
  93 
  94     /**
  95      * Serialization version.
  96      */
  97     private static final long serialVersionUID = -6103370247208168577L;
  98 
  99     static final byte CHRONO_TYPE = 1;
 100     static final byte CHRONO_LOCAL_DATE_TIME_TYPE = 2;
 101     static final byte CHRONO_ZONE_DATE_TIME_TYPE = 3;
 102     static final byte JAPANESE_DATE_TYPE = 4;
 103     static final byte JAPANESE_ERA_TYPE = 5;
 104     static final byte HIJRAH_DATE_TYPE = 6;
 105     static final byte HIJRAH_ERA_TYPE = 7;
 106     static final byte MINGUO_DATE_TYPE = 8;
 107     static final byte MINGUO_ERA_TYPE = 9;
 108     static final byte THAIBUDDHIST_DATE_TYPE = 10;
 109     static final byte THAIBUDDHIST_ERA_TYPE = 11;
 110 
 111     /** The type being serialized. */
 112     private byte type;
 113     /** The object being serialized. */
 114     private Object object;
 115 
 116     /**
 117      * Constructor for deserialization.
 118      */
 119     public Ser() {
 120     }
 121 
 122     /**
 123      * Creates an instance for serialization.
 124      *
 125      * @param type  the type
 126      * @param object  the object
 127      */
 128     Ser(byte type, Object object) {
 129         this.type = type;
 130         this.object = object;
 131     }
 132 
 133     //-----------------------------------------------------------------------
 134     /**
 135      * Implements the {@code Externalizable} interface to write the object.
 136      *
 137      * @param out  the data stream to write to, not null
 138      */
 139     @Override
 140     public void writeExternal(ObjectOutput out) throws IOException {
 141         writeInternal(type, object, out);
 142     }
 143 
 144     private static void writeInternal(byte type, Object object, ObjectOutput out) throws IOException {
 145         out.writeByte(type);
 146         switch (type) {
 147             case CHRONO_TYPE:
 148                 ((Chronology) object).writeExternal(out);
 149                 break;
 150             case CHRONO_LOCAL_DATE_TIME_TYPE:
 151                 ((ChronoLocalDateTimeImpl<?>) object).writeExternal(out);
 152                 break;
 153             case CHRONO_ZONE_DATE_TIME_TYPE:
 154                 ((ChronoZonedDateTimeImpl<?>) object).writeExternal(out);
 155                 break;
 156             case JAPANESE_DATE_TYPE:
 157                 ((JapaneseDate) object).writeExternal(out);
 158                 break;
 159             case JAPANESE_ERA_TYPE:
 160                 ((JapaneseEra) object).writeExternal(out);
 161                 break;
 162             case HIJRAH_DATE_TYPE:
 163                 ((HijrahDate) object).writeExternal(out);
 164                 break;
 165             case HIJRAH_ERA_TYPE:
 166                 ((HijrahEra) object).writeExternal(out);
 167                 break;
 168             case MINGUO_DATE_TYPE:
 169                 ((MinguoDate) object).writeExternal(out);
 170                 break;
 171             case MINGUO_ERA_TYPE:
 172                 ((MinguoEra) object).writeExternal(out);
 173                 break;
 174             case THAIBUDDHIST_DATE_TYPE:
 175                 ((ThaiBuddhistDate) object).writeExternal(out);


 184 
 185     //-----------------------------------------------------------------------
 186     /**
 187      * Implements the {@code Externalizable} interface to read the object.
 188      *
 189      * @param in  the data to read, not null
 190      */
 191     @Override
 192     public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
 193         type = in.readByte();
 194         object = readInternal(type, in);
 195     }
 196 
 197     static Object read(ObjectInput in) throws IOException, ClassNotFoundException {
 198         byte type = in.readByte();
 199         return readInternal(type, in);
 200     }
 201 
 202     private static Object readInternal(byte type, ObjectInput in) throws IOException, ClassNotFoundException {
 203         switch (type) {
 204             case CHRONO_TYPE: return Chronology.readExternal(in);
 205             case CHRONO_LOCAL_DATE_TIME_TYPE: return ChronoLocalDateTimeImpl.readExternal(in);
 206             case CHRONO_ZONE_DATE_TIME_TYPE: return ChronoZonedDateTimeImpl.readExternal(in);
 207             case JAPANESE_DATE_TYPE:  return JapaneseDate.readExternal(in);
 208             case JAPANESE_ERA_TYPE: return JapaneseEra.readExternal(in);
 209             case HIJRAH_DATE_TYPE: return HijrahDate.readExternal(in);
 210             case HIJRAH_ERA_TYPE: return HijrahEra.readExternal(in);
 211             case MINGUO_DATE_TYPE: return MinguoDate.readExternal(in);
 212             case MINGUO_ERA_TYPE: return MinguoEra.readExternal(in);
 213             case THAIBUDDHIST_DATE_TYPE: return ThaiBuddhistDate.readExternal(in);
 214             case THAIBUDDHIST_ERA_TYPE: return ThaiBuddhistEra.readExternal(in);
 215             default: throw new StreamCorruptedException("Unknown serialized type");

 216         }
 217     }
 218 
 219     /**
 220      * Returns the object that will replace this one.
 221      *
 222      * @return the read object, should never be null
 223      */
 224     private Object readResolve() {
 225          return object;
 226     }
 227 
 228 }