-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use externalizable replacement for serializable entities
- Loading branch information
Showing
7 changed files
with
125 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright 2019-2024 JetBrains s.r.o. and contributors. | ||
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file. | ||
*/ | ||
|
||
package kotlinx.datetime.internal | ||
|
||
import kotlinx.datetime.* | ||
import java.io.* | ||
|
||
internal class SerializedValue(var typeTag: Int, var value: Any?) : Externalizable { | ||
constructor() : this(0, null) | ||
|
||
override fun writeExternal(out: ObjectOutput) { | ||
out.writeByte(typeTag) | ||
val value = this.value | ||
when (typeTag) { | ||
INSTANT_TAG -> { | ||
value as Instant | ||
out.writeLong(value.epochSeconds) | ||
out.writeInt(value.nanosecondsOfSecond) | ||
} | ||
DATE_TAG -> { | ||
value as LocalDate | ||
out.writeLong(value.value.toEpochDay()) | ||
} | ||
TIME_TAG -> { | ||
value as LocalTime | ||
out.writeLong(value.toNanosecondOfDay()) | ||
} | ||
DATE_TIME_TAG -> { | ||
value as LocalDateTime | ||
out.writeLong(value.date.value.toEpochDay()) | ||
out.writeLong(value.time.toNanosecondOfDay()) | ||
} | ||
UTC_OFFSET_TAG -> { | ||
value as UtcOffset | ||
out.writeInt(value.totalSeconds) | ||
} | ||
else -> throw IllegalStateException("Unknown type tag: $typeTag for value: $value") | ||
} | ||
} | ||
|
||
override fun readExternal(`in`: ObjectInput) { | ||
typeTag = `in`.readByte().toInt() | ||
value = when (typeTag) { | ||
INSTANT_TAG -> | ||
Instant.fromEpochSeconds(`in`.readLong(), `in`.readInt()) | ||
DATE_TAG -> | ||
LocalDate(java.time.LocalDate.ofEpochDay(`in`.readLong())) | ||
TIME_TAG -> | ||
LocalTime.fromNanosecondOfDay(`in`.readLong()) | ||
DATE_TIME_TAG -> | ||
LocalDateTime( | ||
LocalDate(java.time.LocalDate.ofEpochDay(`in`.readLong())), | ||
LocalTime.fromNanosecondOfDay(`in`.readLong()) | ||
) | ||
UTC_OFFSET_TAG -> | ||
UtcOffset(seconds = `in`.readInt()) | ||
else -> throw IOException("Unknown type tag: $typeTag") | ||
} | ||
} | ||
|
||
private fun readResolve(): Any = value!! | ||
|
||
companion object { | ||
private const val serialVersionUID: Long = 0L | ||
const val INSTANT_TAG = 1 | ||
const val DATE_TAG = 2 | ||
const val TIME_TAG = 3 | ||
const val DATE_TIME_TAG = 4 | ||
const val UTC_OFFSET_TAG = 10 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters