Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/java/time/Ser.java
41152 views
1
/*
2
* Copyright (c) 2012, 2019, 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
* Copyright (c) 2011-2012, Stephen Colebourne & Michael Nascimento Santos
28
*
29
* All rights reserved.
30
*
31
* Redistribution and use in source and binary forms, with or without
32
* modification, are permitted provided that the following conditions are met:
33
*
34
* * Redistributions of source code must retain the above copyright notice,
35
* this list of conditions and the following disclaimer.
36
*
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;
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.Serializable;
65
import java.io.StreamCorruptedException;
66
67
/**
68
* The shared serialization delegate for this package.
69
*
70
* @implNote
71
* This class wraps the object being serialized, and takes a byte representing the type of the class to
72
* be serialized. This byte can also be used for versioning the serialization format. In this case another
73
* byte flag would be used in order to specify an alternative version of the type format.
74
* For example {@code LOCAL_DATE_TYPE_VERSION_2 = 21}.
75
* <p>
76
* In order to serialize the object it writes its byte and then calls back to the appropriate class where
77
* the serialization is performed. In order to deserialize the object it read in the type byte, switching
78
* in order to select which class to call back into.
79
* <p>
80
* The serialization format is determined on a per class basis. In the case of field based classes each
81
* of the fields is written out with an appropriate size format in descending order of the field's size. For
82
* example in the case of {@link LocalDate} year is written before month. Composite classes, such as
83
* {@link LocalDateTime} are serialized as one object.
84
* <p>
85
* This class is mutable and should be created once per serialization.
86
*
87
* @serial include
88
* @since 1.8
89
*/
90
final class Ser implements Externalizable {
91
92
/**
93
* Serialization version.
94
*/
95
@java.io.Serial
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
static final byte OFFSET_TIME_TYPE = 9;
107
static final byte OFFSET_DATE_TIME_TYPE = 10;
108
static final byte YEAR_TYPE = 11;
109
static final byte YEAR_MONTH_TYPE = 12;
110
static final byte MONTH_DAY_TYPE = 13;
111
static final byte PERIOD_TYPE = 14;
112
113
/** The type being serialized. */
114
private byte type;
115
/** The object being serialized. */
116
private Serializable object;
117
118
/**
119
* Constructor for deserialization.
120
*/
121
public Ser() {
122
}
123
124
/**
125
* Creates an instance for serialization.
126
*
127
* @param type the type
128
* @param object the object
129
*/
130
Ser(byte type, Serializable object) {
131
this.type = type;
132
this.object = object;
133
}
134
135
//-----------------------------------------------------------------------
136
/**
137
* Implements the {@code Externalizable} interface to write the object.
138
* @serialData
139
*
140
* Each serializable class is mapped to a type that is the first byte
141
* in the stream. Refer to each class {@code writeReplace}
142
* serialized form for the value of the type and sequence of values for the type.
143
* <ul>
144
* <li><a href="{@docRoot}/serialized-form.html#java.time.Duration">Duration.writeReplace</a>
145
* <li><a href="{@docRoot}/serialized-form.html#java.time.Instant">Instant.writeReplace</a>
146
* <li><a href="{@docRoot}/serialized-form.html#java.time.LocalDate">LocalDate.writeReplace</a>
147
* <li><a href="{@docRoot}/serialized-form.html#java.time.LocalDateTime">LocalDateTime.writeReplace</a>
148
* <li><a href="{@docRoot}/serialized-form.html#java.time.LocalTime">LocalTime.writeReplace</a>
149
* <li><a href="{@docRoot}/serialized-form.html#java.time.MonthDay">MonthDay.writeReplace</a>
150
* <li><a href="{@docRoot}/serialized-form.html#java.time.OffsetTime">OffsetTime.writeReplace</a>
151
* <li><a href="{@docRoot}/serialized-form.html#java.time.OffsetDateTime">OffsetDateTime.writeReplace</a>
152
* <li><a href="{@docRoot}/serialized-form.html#java.time.Period">Period.writeReplace</a>
153
* <li><a href="{@docRoot}/serialized-form.html#java.time.Year">Year.writeReplace</a>
154
* <li><a href="{@docRoot}/serialized-form.html#java.time.YearMonth">YearMonth.writeReplace</a>
155
* <li><a href="{@docRoot}/serialized-form.html#java.time.ZoneId">ZoneId.writeReplace</a>
156
* <li><a href="{@docRoot}/serialized-form.html#java.time.ZoneOffset">ZoneOffset.writeReplace</a>
157
* <li><a href="{@docRoot}/serialized-form.html#java.time.ZonedDateTime">ZonedDateTime.writeReplace</a>
158
* </ul>
159
*
160
* @param out the data stream to write to, not null
161
*/
162
@Override
163
public void writeExternal(ObjectOutput out) throws IOException {
164
writeInternal(type, object, out);
165
}
166
167
static void writeInternal(byte type, Object object, ObjectOutput out) throws IOException {
168
out.writeByte(type);
169
switch (type) {
170
case DURATION_TYPE:
171
((Duration) object).writeExternal(out);
172
break;
173
case INSTANT_TYPE:
174
((Instant) object).writeExternal(out);
175
break;
176
case LOCAL_DATE_TYPE:
177
((LocalDate) object).writeExternal(out);
178
break;
179
case LOCAL_DATE_TIME_TYPE:
180
((LocalDateTime) object).writeExternal(out);
181
break;
182
case LOCAL_TIME_TYPE:
183
((LocalTime) object).writeExternal(out);
184
break;
185
case ZONE_REGION_TYPE:
186
((ZoneRegion) object).writeExternal(out);
187
break;
188
case ZONE_OFFSET_TYPE:
189
((ZoneOffset) object).writeExternal(out);
190
break;
191
case ZONE_DATE_TIME_TYPE:
192
((ZonedDateTime) object).writeExternal(out);
193
break;
194
case OFFSET_TIME_TYPE:
195
((OffsetTime) object).writeExternal(out);
196
break;
197
case OFFSET_DATE_TIME_TYPE:
198
((OffsetDateTime) object).writeExternal(out);
199
break;
200
case YEAR_TYPE:
201
((Year) object).writeExternal(out);
202
break;
203
case YEAR_MONTH_TYPE:
204
((YearMonth) object).writeExternal(out);
205
break;
206
case MONTH_DAY_TYPE:
207
((MonthDay) object).writeExternal(out);
208
break;
209
case PERIOD_TYPE:
210
((Period) object).writeExternal(out);
211
break;
212
default:
213
throw new InvalidClassException("Unknown serialized type");
214
}
215
}
216
217
//-----------------------------------------------------------------------
218
/**
219
* Implements the {@code Externalizable} interface to read the object.
220
* @serialData
221
*
222
* The streamed type and parameters defined by the type's {@code writeReplace}
223
* method are read and passed to the corresponding static factory for the type
224
* to create a new instance. That instance is returned as the de-serialized
225
* {@code Ser} object.
226
*
227
* <ul>
228
* <li><a href="{@docRoot}/serialized-form.html#java.time.Duration">Duration</a> -
229
* {@code Duration.ofSeconds(seconds, nanos);}
230
* <li><a href="{@docRoot}/serialized-form.html#java.time.Instant">Instant</a> -
231
* {@code Instant.ofEpochSecond(seconds, nanos);}
232
* <li><a href="{@docRoot}/serialized-form.html#java.time.LocalDate">LocalDate</a> -
233
* {@code LocalDate.of(year, month, day);}
234
* <li><a href="{@docRoot}/serialized-form.html#java.time.LocalDateTime">LocalDateTime</a> -
235
* {@code LocalDateTime.of(date, time);}
236
* <li><a href="{@docRoot}/serialized-form.html#java.time.LocalTime">LocalTime</a> -
237
* {@code LocalTime.of(hour, minute, second, nano);}
238
* <li><a href="{@docRoot}/serialized-form.html#java.time.MonthDay">MonthDay</a> -
239
* {@code MonthDay.of(month, day);}
240
* <li><a href="{@docRoot}/serialized-form.html#java.time.OffsetTime">OffsetTime</a> -
241
* {@code OffsetTime.of(time, offset);}
242
* <li><a href="{@docRoot}/serialized-form.html#java.time.OffsetDateTime">OffsetDateTime</a> -
243
* {@code OffsetDateTime.of(dateTime, offset);}
244
* <li><a href="{@docRoot}/serialized-form.html#java.time.Period">Period</a> -
245
* {@code Period.of(years, months, days);}
246
* <li><a href="{@docRoot}/serialized-form.html#java.time.Year">Year</a> -
247
* {@code Year.of(year);}
248
* <li><a href="{@docRoot}/serialized-form.html#java.time.YearMonth">YearMonth</a> -
249
* {@code YearMonth.of(year, month);}
250
* <li><a href="{@docRoot}/serialized-form.html#java.time.ZonedDateTime">ZonedDateTime</a> -
251
* {@code ZonedDateTime.ofLenient(dateTime, offset, zone);}
252
* <li><a href="{@docRoot}/serialized-form.html#java.time.ZoneId">ZoneId</a> -
253
* {@code ZoneId.of(id);}
254
* <li><a href="{@docRoot}/serialized-form.html#java.time.ZoneOffset">ZoneOffset</a> -
255
* {@code (offsetByte == 127 ? ZoneOffset.ofTotalSeconds(in.readInt()) :
256
* ZoneOffset.ofTotalSeconds(offsetByte * 900));}
257
* </ul>
258
*
259
* @param in the data to read, not null
260
*/
261
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
262
type = in.readByte();
263
object = readInternal(type, in);
264
}
265
266
static Serializable read(ObjectInput in) throws IOException, ClassNotFoundException {
267
byte type = in.readByte();
268
return readInternal(type, in);
269
}
270
271
private static Serializable readInternal(byte type, ObjectInput in)
272
throws IOException, ClassNotFoundException {
273
switch (type) {
274
case DURATION_TYPE: return Duration.readExternal(in);
275
case INSTANT_TYPE: return Instant.readExternal(in);
276
case LOCAL_DATE_TYPE: return LocalDate.readExternal(in);
277
case LOCAL_DATE_TIME_TYPE: return LocalDateTime.readExternal(in);
278
case LOCAL_TIME_TYPE: return LocalTime.readExternal(in);
279
case ZONE_DATE_TIME_TYPE: return ZonedDateTime.readExternal(in);
280
case ZONE_OFFSET_TYPE: return ZoneOffset.readExternal(in);
281
case ZONE_REGION_TYPE: return ZoneRegion.readExternal(in);
282
case OFFSET_TIME_TYPE: return OffsetTime.readExternal(in);
283
case OFFSET_DATE_TIME_TYPE: return OffsetDateTime.readExternal(in);
284
case YEAR_TYPE: return Year.readExternal(in);
285
case YEAR_MONTH_TYPE: return YearMonth.readExternal(in);
286
case MONTH_DAY_TYPE: return MonthDay.readExternal(in);
287
case PERIOD_TYPE: return Period.readExternal(in);
288
default:
289
throw new StreamCorruptedException("Unknown serialized type");
290
}
291
}
292
293
/**
294
* Returns the object that will replace this one.
295
*
296
* @return the read object, should never be null
297
*/
298
@java.io.Serial
299
private Object readResolve() {
300
return object;
301
}
302
303
}
304
305