Skip to content

Commit 6a98d8c

Browse files
author
loushaokun
committed
modify StringSerializer & AbstractStringBuilderSerializer
1 parent 49fabf0 commit 6a98d8c

File tree

9 files changed

+204
-68
lines changed

9 files changed

+204
-68
lines changed

java/fory-core/src/main/java/org/apache/fory/memory/ByteBufferUtil.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,6 @@ public static ByteBuffer createDirectByteBufferFromNativeAddress(long address, i
7676
}
7777
}
7878

79-
// /** Wrap a buffer [address, address + size) into provided <code>buffer</code>. */
80-
// @NotForAndroid(reason = "Android cannot directly operate the capacity field")
81-
// public static void wrapDirectByteBufferFromNativeAddress(
82-
// ByteBuffer buffer, long address, int size) {
83-
// Preconditions.checkArgument(
84-
// buffer.isDirect(), "Can't wrap native memory into a non-direct ByteBuffer.");
85-
// Platform.putLong(buffer, BUFFER_ADDRESS_FIELD_OFFSET, address);
86-
// Platform.putInt(buffer, BUFFER_CAPACITY_FIELD_OFFSET, size);
87-
// buffer.clear();
88-
// }
89-
9079
@NotForAndroid
9180
public static ByteBuffer wrapDirectBuffer(long address, int size) {
9281
return createDirectByteBufferFromNativeAddress(address, size);

java/fory-core/src/main/java/org/apache/fory/memory/MemoryBuffer.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1307,20 +1307,28 @@ public void coverMemoryWithArray(
13071307
switch (eleType) {
13081308
case BOOL:
13091309
coverMemoryWithBoolArrayInner((boolean[]) arr, offset, length);
1310+
break;
13101311
case BYTE:
13111312
buffer.put((byte[]) arr, offset, length);
1313+
break;
13121314
case CHAR:
13131315
buffer.asCharBuffer().put((char[]) arr, offset, length);
1316+
break;
13141317
case SHORT:
13151318
buffer.asShortBuffer().put((short[]) arr, offset, length);
1319+
break;
13161320
case INT:
13171321
buffer.asIntBuffer().put((int[]) arr, offset, length);
1322+
break;
13181323
case LONG:
13191324
buffer.asLongBuffer().put((long[]) arr, offset, length);
1325+
break;
13201326
case FLOAT:
13211327
buffer.asFloatBuffer().put((float[]) arr, offset, length);
1328+
break;
13221329
case DOUBLE:
13231330
buffer.asDoubleBuffer().put((double[]) arr, offset, length);
1331+
break;
13241332
}
13251333
}
13261334
}
@@ -2569,21 +2577,28 @@ public void copyTo(
25692577
switch (eleType) {
25702578
case BOOL:
25712579
copyToBoolsInner((boolean[]) target, offset, length);
2572-
return;
2580+
break;
25732581
case BYTE:
25742582
buffer.get((byte[]) target, offset, length);
2583+
break;
25752584
case CHAR:
25762585
buffer.asCharBuffer().get((char[]) target, offset, length);
2586+
break;
25772587
case SHORT:
25782588
buffer.asShortBuffer().get((short[]) target, offset, length);
2589+
break;
25792590
case INT:
25802591
buffer.asIntBuffer().get((int[]) target, offset, length);
2592+
break;
25812593
case LONG:
25822594
buffer.asLongBuffer().get((long[]) target, offset, length);
2595+
break;
25832596
case FLOAT:
25842597
buffer.asFloatBuffer().get((float[]) target, offset, length);
2598+
break;
25852599
case DOUBLE:
25862600
buffer.asDoubleBuffer().get((double[]) target, offset, length);
2601+
break;
25872602
}
25882603
}
25892604
}

java/fory-core/src/main/java/org/apache/fory/resolver/ClassResolver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ private void addDefaultSerializers() {
358358
UnmodifiableSerializers.registerSerializers(fory);
359359
ImmutableCollectionSerializers.registerSerializers(fory);
360360
SubListSerializers.registerSerializers(fory, true);
361-
if (fory.getConfig().registerGuavaTypes()) {
361+
if (fory.getConfig().registerGuavaTypes() && !Platform.IS_ANDROID) {
362362
GuavaCollectionSerializers.registerDefaultSerializers(fory);
363363
}
364364
if (fory.getConfig().deserializeNonexistentClass()) {

java/fory-core/src/main/java/org/apache/fory/serializer/Serializers.java

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -202,25 +202,32 @@ public T xread(MemoryBuffer buffer) {
202202
}
203203
}
204204

205-
private static final ToIntFunction GET_CODER;
206-
private static final Function GET_VALUE;
207-
208-
static {
209-
GET_VALUE = (Function) makeGetterFunction(StringBuilder.class.getSuperclass(), "getValue");
210-
ToIntFunction<CharSequence> getCoder;
211-
try {
212-
Method getCoderMethod = StringBuilder.class.getSuperclass().getDeclaredMethod("getCoder");
213-
getCoder = (ToIntFunction<CharSequence>) makeGetterFunction(getCoderMethod, int.class);
214-
} catch (NoSuchMethodException e) {
215-
getCoder = null;
216-
}
217-
GET_CODER = getCoder;
218-
}
219-
220205
public abstract static class AbstractStringBuilderSerializer<T extends CharSequence>
221206
extends Serializer<T> {
222207
protected final StringSerializer stringSerializer;
223208

209+
private static final ToIntFunction GET_CODER;
210+
private static final Function GET_VALUE;
211+
212+
private static final boolean RESTRICTED_STRING_BUILDER = Platform.IS_ANDROID;
213+
214+
static {
215+
if (RESTRICTED_STRING_BUILDER) {
216+
GET_CODER = null;
217+
GET_VALUE = null;
218+
}else {
219+
GET_VALUE = (Function) makeGetterFunction(StringBuilder.class.getSuperclass(), "getValue");
220+
ToIntFunction<CharSequence> getCoder;
221+
try {
222+
Method getCoderMethod = StringBuilder.class.getSuperclass().getDeclaredMethod("getCoder");
223+
getCoder = (ToIntFunction<CharSequence>) makeGetterFunction(getCoderMethod, int.class);
224+
} catch (NoSuchMethodException e) {
225+
getCoder = null;
226+
}
227+
GET_CODER = getCoder;
228+
}
229+
}
230+
224231
public AbstractStringBuilderSerializer(Fory fory, Class<T> type) {
225232
super(fory, type);
226233
stringSerializer = new StringSerializer(fory);
@@ -233,6 +240,12 @@ public void xwrite(MemoryBuffer buffer, T value) {
233240

234241
@Override
235242
public void write(MemoryBuffer buffer, T value) {
243+
if (RESTRICTED_STRING_BUILDER) {
244+
// Restricted StringBuilder doesn't have getCoder method, so we use
245+
// the StringSerializer to write the string.
246+
stringSerializer.writeString(buffer, value.toString());
247+
return;
248+
}
236249
if (GET_CODER != null) {
237250
int coder = GET_CODER.applyAsInt(value);
238251
byte[] v = (byte[]) GET_VALUE.apply(value);

0 commit comments

Comments
 (0)