|
2 | 2 |
|
3 | 3 | import it.telami.commons.util.OperatingSystem; |
4 | 4 |
|
5 | | -import java.io.BufferedOutputStream; |
6 | | -import java.io.File; |
7 | | -import java.io.FileOutputStream; |
8 | | -import java.io.InputStream; |
| 5 | +import java.io.*; |
9 | 6 | import java.lang.foreign.*; |
10 | 7 | import java.lang.invoke.MethodHandle; |
| 8 | +import java.lang.invoke.MethodHandles; |
11 | 9 | import java.lang.invoke.VarHandle; |
12 | 10 | import java.nio.ByteOrder; |
13 | 11 |
|
14 | | -import static java.lang.foreign.ValueLayout.JAVA_BOOLEAN; |
15 | | -import static java.lang.foreign.ValueLayout.ADDRESS; |
16 | | -import static java.lang.foreign.ValueLayout.JAVA_LONG; |
| 12 | +import static java.lang.foreign.ValueLayout.*; |
17 | 13 |
|
18 | 14 | /** |
19 | 15 | * Class representing an integer <b>128-bit</b> long supporting many atomic operations. <br> |
@@ -99,10 +95,10 @@ public final class i128 extends Number implements Comparable<i128> { |
99 | 95 | .groupElement("low")); |
100 | 96 |
|
101 | 97 | //Used for avoiding branches based on the byte order |
102 | | - private final VarHandle mostSignificantBits; |
103 | | - private final VarHandle leastSignificantBits; |
| 98 | + private transient VarHandle mostSignificantBits; |
| 99 | + private transient VarHandle leastSignificantBits; |
104 | 100 | //Native memory segment |
105 | | - private final MemorySegment i128; |
| 101 | + private transient MemorySegment i128; |
106 | 102 |
|
107 | 103 |
|
108 | 104 | /** |
@@ -729,4 +725,45 @@ public double doubleValue () { |
729 | 725 | public int compareTo (final i128 o) { |
730 | 726 | return i128.asByteBuffer().compareTo(o.i128.asByteBuffer()); |
731 | 727 | } |
| 728 | + |
| 729 | + @Serial |
| 730 | + private void writeObject (final ObjectOutputStream out) throws IOException { |
| 731 | + final byte[] state; |
| 732 | + final ByteOrder order; |
| 733 | + final VarHandle view; |
| 734 | + (view = MethodHandles.byteArrayViewVarHandle(long[].class, order |
| 735 | + = mostSignificantBits != i128_low |
| 736 | + ? ByteOrder.BIG_ENDIAN |
| 737 | + : ByteOrder.LITTLE_ENDIAN)) |
| 738 | + .set(state = new byte[17], 0, getHigh()); |
| 739 | + view.set(state, 8, getLow()); |
| 740 | + state[16] = order != ByteOrder.BIG_ENDIAN |
| 741 | + ? (byte) 0 |
| 742 | + : (byte) 1; |
| 743 | + out.write(state); |
| 744 | + } |
| 745 | + @Serial |
| 746 | + private void readObject (final ObjectInputStream in) throws IOException { |
| 747 | + if (in.available() != 0) { |
| 748 | + final byte[] state = new byte[17]; |
| 749 | + if (in.read(state) != 17) |
| 750 | + return; |
| 751 | + i128 = defaultArena.allocate(I128); |
| 752 | + final ByteOrder order; |
| 753 | + final VarHandle view; |
| 754 | + set((long) (view = MethodHandles.byteArrayViewVarHandle(long[].class, order |
| 755 | + = state[16] != 0 |
| 756 | + ? ByteOrder.BIG_ENDIAN |
| 757 | + : ByteOrder.LITTLE_ENDIAN)) |
| 758 | + .get(state, 0), |
| 759 | + (long) view.get(state, 8)); |
| 760 | + if (order != ByteOrder.LITTLE_ENDIAN) { |
| 761 | + mostSignificantBits = i128_high; |
| 762 | + leastSignificantBits = i128_low; |
| 763 | + } else { |
| 764 | + mostSignificantBits = i128_low; |
| 765 | + leastSignificantBits = i128_high; |
| 766 | + } |
| 767 | + } |
| 768 | + } |
732 | 769 | } |
0 commit comments