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

Print this page




  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;
  58 
  59 import java.io.DataInput;
  60 import java.io.DataOutput;
  61 import java.io.Externalizable;
  62 import java.io.IOException;
  63 import java.io.InvalidClassException;
  64 import java.io.ObjectInput;
  65 import java.io.ObjectOutput;
  66 import java.io.StreamCorruptedException;
  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 LOCAL_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>


  86  * This class is mutable and should be created once per serialization.
  87  *
  88  * @serial include
  89  * @since 1.8
  90  */
  91 final class Ser implements Externalizable {
  92 
  93     /**
  94      * Serialization version.
  95      */
  96     private static final long serialVersionUID = -7683839454370182990L;
  97 
  98     static final byte DURATION_TYPE = 1;
  99     static final byte INSTANT_TYPE = 2;
 100     static final byte LOCAL_DATE_TYPE = 3;
 101     static final byte LOCAL_TIME_TYPE = 4;
 102     static final byte LOCAL_DATE_TIME_TYPE = 5;
 103     static final byte ZONE_DATE_TIME_TYPE = 6;
 104     static final byte ZONE_REGION_TYPE = 7;
 105     static final byte ZONE_OFFSET_TYPE = 8;






 106 
 107     /** The type being serialized. */
 108     private byte type;
 109     /** The object being serialized. */
 110     private Object object;
 111 
 112     /**
 113      * Constructor for deserialization.
 114      */
 115     public Ser() {
 116     }
 117 
 118     /**
 119      * Creates an instance for serialization.
 120      *
 121      * @param type  the type
 122      * @param object  the object
 123      */
 124     Ser(byte type, Object object) {
 125         this.type = type;
 126         this.object = object;
 127     }
 128 
 129     //-----------------------------------------------------------------------
 130     /**
 131      * Implements the {@code Externalizable} interface to write the object.
 132      *
 133      * @param out  the data stream to write to, not null
 134      */

 135     public void writeExternal(ObjectOutput out) throws IOException {
 136         writeInternal(type, object, out);
 137     }
 138 
 139     static void writeInternal(byte type, Object object, DataOutput out) throws IOException {
 140         out.writeByte(type);
 141         switch (type) {
 142             case DURATION_TYPE:
 143                 ((Duration) object).writeExternal(out);
 144                 break;
 145             case INSTANT_TYPE:
 146                 ((Instant) object).writeExternal(out);
 147                 break;
 148             case LOCAL_DATE_TYPE:
 149                 ((LocalDate) object).writeExternal(out);
 150                 break;
 151             case LOCAL_DATE_TIME_TYPE:
 152                 ((LocalDateTime) object).writeExternal(out);
 153                 break;
 154             case LOCAL_TIME_TYPE:
 155                 ((LocalTime) object).writeExternal(out);
 156                 break;
 157             case ZONE_REGION_TYPE:
 158                 ((ZoneRegion) object).writeExternal(out);
 159                 break;
 160             case ZONE_OFFSET_TYPE:
 161                 ((ZoneOffset) object).writeExternal(out);
 162                 break;
 163             case ZONE_DATE_TIME_TYPE:
 164                 ((ZonedDateTime) object).writeExternal(out);
 165                 break;


















 166             default:
 167                 throw new InvalidClassException("Unknown serialized type");
 168         }
 169     }
 170 
 171     //-----------------------------------------------------------------------
 172     /**
 173      * Implements the {@code Externalizable} interface to read the object.
 174      *
 175      * @param in  the data to read, not null
 176      */
 177     public void readExternal(ObjectInput in) throws IOException {
 178         type = in.readByte();
 179         object = readInternal(type, in);
 180     }
 181 
 182     static Object read(DataInput in) throws IOException {
 183         byte type = in.readByte();
 184         return readInternal(type, in);
 185     }
 186 
 187     private static Object readInternal(byte type, DataInput in) throws IOException {
 188         switch (type) {
 189             case DURATION_TYPE: return Duration.readExternal(in);
 190             case INSTANT_TYPE: return Instant.readExternal(in);
 191             case LOCAL_DATE_TYPE: return LocalDate.readExternal(in);
 192             case LOCAL_DATE_TIME_TYPE: return LocalDateTime.readExternal(in);
 193             case LOCAL_TIME_TYPE: return LocalTime.readExternal(in);
 194             case ZONE_DATE_TIME_TYPE: return ZonedDateTime.readExternal(in);
 195             case ZONE_OFFSET_TYPE: return ZoneOffset.readExternal(in);
 196             case ZONE_REGION_TYPE: return ZoneRegion.readExternal(in);






 197             default:
 198                 throw new StreamCorruptedException("Unknown serialized type");
 199         }
 200     }
 201 
 202     /**
 203      * Returns the object that will replace this one.
 204      *
 205      * @return the read object, should never be null
 206      */
 207     private Object readResolve() {
 208          return object;
 209     }
 210 
 211 }


  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;
  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 
  66 /**
  67  * The shared serialization delegate for this package.
  68  *
  69  * <h3>Implementation notes</h3>
  70  * This class wraps the object being serialized, and takes a byte representing the type of the class to
  71  * be serialized.  This byte can also be used for versioning the serialization format.  In this case another
  72  * byte flag would be used in order to specify an alternative version of the type format.
  73  * For example {@code LOCAL_DATE_TYPE_VERSION_2 = 21}.
  74  * <p>
  75  * In order to serialise the object it writes its byte and then calls back to the appropriate class where
  76  * the serialisation is performed.  In order to deserialise the object it read in the type byte, switching
  77  * in order to select which class to call back into.
  78  * <p>


  84  * This class is mutable and should be created once per serialization.
  85  *
  86  * @serial include
  87  * @since 1.8
  88  */
  89 final class Ser implements Externalizable {
  90 
  91     /**
  92      * Serialization version.
  93      */
  94     private static final long serialVersionUID = -7683839454370182990L;
  95 
  96     static final byte DURATION_TYPE = 1;
  97     static final byte INSTANT_TYPE = 2;
  98     static final byte LOCAL_DATE_TYPE = 3;
  99     static final byte LOCAL_TIME_TYPE = 4;
 100     static final byte LOCAL_DATE_TIME_TYPE = 5;
 101     static final byte ZONE_DATE_TIME_TYPE = 6;
 102     static final byte ZONE_REGION_TYPE = 7;
 103     static final byte ZONE_OFFSET_TYPE = 8;
 104     static final byte OFFSET_TIME_TYPE = 9;
 105     static final byte OFFSET_DATE_TIME_TYPE = 10;
 106     static final byte YEAR_TYPE = 11;
 107     static final byte YEAR_MONTH_TYPE = 12;
 108     static final byte MONTH_DAY_TYPE = 13;
 109     static final byte PERIOD_TYPE = 14;
 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     static void writeInternal(byte type, Object object, ObjectOutput out) throws IOException {
 145         out.writeByte(type);
 146         switch (type) {
 147             case DURATION_TYPE:
 148                 ((Duration) object).writeExternal(out);
 149                 break;
 150             case INSTANT_TYPE:
 151                 ((Instant) object).writeExternal(out);
 152                 break;
 153             case LOCAL_DATE_TYPE:
 154                 ((LocalDate) object).writeExternal(out);
 155                 break;
 156             case LOCAL_DATE_TIME_TYPE:
 157                 ((LocalDateTime) object).writeExternal(out);
 158                 break;
 159             case LOCAL_TIME_TYPE:
 160                 ((LocalTime) object).writeExternal(out);
 161                 break;
 162             case ZONE_REGION_TYPE:
 163                 ((ZoneRegion) object).writeExternal(out);
 164                 break;
 165             case ZONE_OFFSET_TYPE:
 166                 ((ZoneOffset) object).writeExternal(out);
 167                 break;
 168             case ZONE_DATE_TIME_TYPE:
 169                 ((ZonedDateTime) object).writeExternal(out);
 170                 break;
 171             case OFFSET_TIME_TYPE:
 172                 ((OffsetTime) object).writeExternal(out);
 173                 break;
 174             case OFFSET_DATE_TIME_TYPE:
 175                 ((OffsetDateTime) object).writeExternal(out);
 176                 break;
 177             case YEAR_TYPE:
 178                 ((Year) object).writeExternal(out);
 179                 break;
 180             case YEAR_MONTH_TYPE:
 181                 ((YearMonth) object).writeExternal(out);
 182                 break;
 183             case MONTH_DAY_TYPE:
 184                 ((MonthDay) object).writeExternal(out);
 185                 break;
 186             case PERIOD_TYPE:
 187                 ((Period) object).writeExternal(out);
 188                 break;
 189             default:
 190                 throw new InvalidClassException("Unknown serialized type");
 191         }
 192     }
 193 
 194     //-----------------------------------------------------------------------
 195     /**
 196      * Implements the {@code Externalizable} interface to read the object.
 197      *
 198      * @param in  the data to read, not null
 199      */
 200     public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
 201         type = in.readByte();
 202         object = readInternal(type, in);
 203     }
 204 
 205     static Object read(ObjectInput in) throws IOException, ClassNotFoundException {
 206         byte type = in.readByte();
 207         return readInternal(type, in);
 208     }
 209 
 210     private static Object readInternal(byte type, ObjectInput in) throws IOException, ClassNotFoundException {
 211         switch (type) {
 212             case DURATION_TYPE: return Duration.readExternal(in);
 213             case INSTANT_TYPE: return Instant.readExternal(in);
 214             case LOCAL_DATE_TYPE: return LocalDate.readExternal(in);
 215             case LOCAL_DATE_TIME_TYPE: return LocalDateTime.readExternal(in);
 216             case LOCAL_TIME_TYPE: return LocalTime.readExternal(in);
 217             case ZONE_DATE_TIME_TYPE: return ZonedDateTime.readExternal(in);
 218             case ZONE_OFFSET_TYPE: return ZoneOffset.readExternal(in);
 219             case ZONE_REGION_TYPE: return ZoneRegion.readExternal(in);
 220             case OFFSET_TIME_TYPE: return OffsetTime.readExternal(in);
 221             case OFFSET_DATE_TIME_TYPE: return OffsetDateTime.readExternal(in);
 222             case YEAR_TYPE: return Year.readExternal(in);
 223             case YEAR_MONTH_TYPE: return YearMonth.readExternal(in);
 224             case MONTH_DAY_TYPE: return MonthDay.readExternal(in);
 225             case PERIOD_TYPE: return Period.readExternal(in);
 226             default:
 227                 throw new StreamCorruptedException("Unknown serialized type");
 228         }
 229     }
 230 
 231     /**
 232      * Returns the object that will replace this one.
 233      *
 234      * @return the read object, should never be null
 235      */
 236     private Object readResolve() {
 237          return object;
 238     }
 239 
 240 }