Skip to content

Commit fd422dc

Browse files
committed
Added Serialization
1 parent 2fd04ad commit fd422dc

1 file changed

Lines changed: 47 additions & 10 deletions

File tree

  • src/it/telami/commons/concurrency/atomic

src/it/telami/commons/concurrency/atomic/i128.java

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,14 @@
22

33
import it.telami.commons.util.OperatingSystem;
44

5-
import java.io.BufferedOutputStream;
6-
import java.io.File;
7-
import java.io.FileOutputStream;
8-
import java.io.InputStream;
5+
import java.io.*;
96
import java.lang.foreign.*;
107
import java.lang.invoke.MethodHandle;
8+
import java.lang.invoke.MethodHandles;
119
import java.lang.invoke.VarHandle;
1210
import java.nio.ByteOrder;
1311

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.*;
1713

1814
/**
1915
* 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> {
9995
.groupElement("low"));
10096

10197
//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;
104100
//Native memory segment
105-
private final MemorySegment i128;
101+
private transient MemorySegment i128;
106102

107103

108104
/**
@@ -729,4 +725,45 @@ public double doubleValue () {
729725
public int compareTo (final i128 o) {
730726
return i128.asByteBuffer().compareTo(o.i128.asByteBuffer());
731727
}
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+
}
732769
}

0 commit comments

Comments
 (0)