Skip to content

Commit 38b8c5e

Browse files
author
loushaokun
committed
code style and licence
1 parent 5527560 commit 38b8c5e

File tree

10 files changed

+134
-84
lines changed

10 files changed

+134
-84
lines changed

java/fory-core/src/main/java/org/apache/fory/annotation/NotForAndroid.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
120
package org.apache.fory.annotation;
221

322
import java.lang.annotation.ElementType;
Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
120
package org.apache.fory.annotation;
221

322
import java.lang.annotation.Documented;
@@ -6,30 +25,14 @@
625
import java.lang.annotation.RetentionPolicy;
726
import java.lang.annotation.Target;
827

9-
/**
10-
* 标注一个方法在 Android 平台上只是部分或条件性支持。
11-
*
12-
* <p>这意味着该方法可能在某些 Android API 级别、特定设备或特定执行分支下才能正常工作, 调用者需要仔细阅读说明或源码来确保其使用场景的兼容性。
13-
*/
1428
@Documented
1529
@Retention(RetentionPolicy.CLASS)
1630
@Target({ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.TYPE})
1731
public @interface PartialAndroidSupport {
1832

19-
/**
20-
* 在哪个最低 Android API 级别以上才被支持。 默认为 -1,表示未指定或与此无关。
21-
*
22-
* @return 最低支持的 API level。
23-
*/
2433
int minApiLevel() default -1;
2534

26-
/**
27-
* 描述不支持或部分支持的具体原因、条件或分支情况。 例如:"仅在硬件加速开启时有效" 或 "在软件渲染路径下会抛出异常"。
28-
*
29-
* @return 具体原因的描述文字。
30-
*/
3135
String reason() default "该方法在 Android 上的所有分支和条件下不保证完全支持,请查阅文档或源码。";
3236

33-
/** 指向更详细文档或 issue 的链接。 * @return 文档的 URL。 */
3437
String docUrl() default "";
3538
}

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

Lines changed: 62 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -367,15 +367,18 @@ public void get(int offset, ByteBuffer target, int numBytes) {
367367
final int targetPos = target.position();
368368
if (target.isDirect()) {
369369
final long sourceOffset = address + offset;
370-
if (sourceOffset > addressLimit - numBytes) throwOOBException();
370+
if (sourceOffset > addressLimit - numBytes) {
371+
throwOOBException();
372+
}
371373
final long targetAddr = ByteBufferUtil.getAddress(target) + targetPos;
372374
if (Platform.IS_ANDROID) {
373375
if (onHeap) {
374376
target.put(heapMemory, memoryOffset + offset, numBytes); // 此操作会改变target的position,无需再调整了
375377
return;
378+
} else {
379+
// Android只支持这个三个参数的copyMemory
380+
Platform.UNSAFE.copyMemory(sourceOffset, targetAddr, numBytes);
376381
}
377-
// Android只支持这个三个参数的copyMemory
378-
else Platform.UNSAFE.copyMemory(sourceOffset, targetAddr, numBytes);
379382
} else {
380383
Platform.copyMemory(heapMemory, sourceOffset, null, targetAddr, numBytes);
381384
}
@@ -395,7 +398,9 @@ public void put(int offset, ByteBuffer source, int numBytes) {
395398
if (source.isDirect()) {
396399
final long sourceAddr = ByteBufferUtil.getAddress(source) + sourcePos;
397400
final long targetAddr = address + offset;
398-
if (targetAddr > addressLimit - numBytes) throwOOBException();
401+
if (targetAddr > addressLimit - numBytes) {
402+
throwOOBException();
403+
}
399404
if (Platform.IS_ANDROID) {
400405
if (onHeap) {
401406
source.get(heapMemory, memoryOffset + offset, numBytes); // 此操作会改变source的position,无需再调整了
@@ -1287,7 +1292,9 @@ public void coverMemoryWithArray(
12871292
Types.JavaArray eleType,
12881293
boolean autoFill) {
12891294
boolean writeMode = bufOffset < 0;
1290-
if (writeMode) bufOffset = writerIndex;
1295+
if (writeMode) {
1296+
bufOffset = writerIndex;
1297+
}
12911298
int numBytes = length * eleType.bytesPerEle;
12921299
if (autoFill) {
12931300
ensure(bufOffset + numBytes);
@@ -1329,10 +1336,14 @@ public void coverMemoryWithArray(
13291336
case DOUBLE:
13301337
buffer.asDoubleBuffer().put((double[]) arr, offset, length);
13311338
break;
1339+
default:
1340+
throw new IllegalStateException("Unexpected value: " + eleType);
13321341
}
13331342
}
13341343
}
1335-
if (writeMode) writerIndex += numBytes;
1344+
if (writeMode) {
1345+
writerIndex += numBytes;
1346+
}
13361347
}
13371348

13381349
public void writeArray(
@@ -2554,7 +2565,9 @@ public void copyTo(
25542565
Types.JavaArray eleType,
25552566
boolean checkSize) {
25562567
boolean readMode = bufOffset < 0;
2557-
if (readMode) bufOffset = readerIndex;
2568+
if (readMode) {
2569+
bufOffset = readerIndex;
2570+
}
25582571
int numBytes = length * eleType.bytesPerEle;
25592572
if (checkSize) {
25602573
int remaining = size - bufOffset;
@@ -2599,10 +2612,39 @@ public void copyTo(
25992612
case DOUBLE:
26002613
buffer.asDoubleBuffer().get((double[]) target, offset, length);
26012614
break;
2615+
default:
2616+
throw new IllegalStateException("Unsupported element type: " + eleType);
26022617
}
26032618
}
26042619
}
2605-
if (readMode) readerIndex += numBytes;
2620+
if (readMode) {
2621+
readerIndex += numBytes;
2622+
}
2623+
}
2624+
2625+
public void copyTo(int offset, MemoryBuffer target, int targetOffset, int numBytes) {
2626+
final long thisPointer = this.address + offset;
2627+
final long otherPointer = target.address + targetOffset;
2628+
if ((numBytes | offset | targetOffset) >= 0
2629+
&& thisPointer <= this.addressLimit - numBytes
2630+
&& otherPointer <= target.addressLimit - numBytes) {
2631+
if (Platform.IS_ANDROID) {
2632+
int thisOldLimit = this.buffer.limit();
2633+
this.buffer.position(offset);
2634+
this.buffer.limit(offset + numBytes);
2635+
target.buffer.position(targetOffset);
2636+
target.buffer.put(this.buffer);
2637+
this.buffer.limit(thisOldLimit);
2638+
} else {
2639+
Platform.copyMemory(
2640+
this.heapMemory, thisPointer, target.heapMemory, otherPointer, numBytes);
2641+
}
2642+
} else {
2643+
throw new IndexOutOfBoundsException(
2644+
String.format(
2645+
"offset=%d, targetOffset=%d, numBytes=%d, address=%d, targetAddress=%d",
2646+
offset, targetOffset, numBytes, this.address, target.address));
2647+
}
26062648
}
26072649

26082650
public void readTo(Object target, int offset, int length, Types.JavaArray eleType) {
@@ -2685,31 +2727,6 @@ public void copyFromDirectUnsafe(long offset, long sourcePointer, long numBytes)
26852727
Platform.copyMemory(null, sourcePointer, this.heapMemory, thisPointer, numBytes);
26862728
}
26872729

2688-
public void copyTo(int offset, MemoryBuffer target, int targetOffset, int numBytes) {
2689-
final long thisPointer = this.address + offset;
2690-
final long otherPointer = target.address + targetOffset;
2691-
if ((numBytes | offset | targetOffset) >= 0
2692-
&& thisPointer <= this.addressLimit - numBytes
2693-
&& otherPointer <= target.addressLimit - numBytes) {
2694-
if (Platform.IS_ANDROID) {
2695-
int thisOldLimit = this.buffer.limit();
2696-
this.buffer.position(offset);
2697-
this.buffer.limit(offset + numBytes);
2698-
target.buffer.position(targetOffset);
2699-
target.buffer.put(this.buffer);
2700-
this.buffer.limit(thisOldLimit);
2701-
} else {
2702-
Platform.copyMemory(
2703-
this.heapMemory, thisPointer, target.heapMemory, otherPointer, numBytes);
2704-
}
2705-
} else {
2706-
throw new IndexOutOfBoundsException(
2707-
String.format(
2708-
"offset=%d, targetOffset=%d, numBytes=%d, address=%d, targetAddress=%d",
2709-
offset, targetOffset, numBytes, this.address, target.address));
2710-
}
2711-
}
2712-
27132730
public void copyFrom(int offset, MemoryBuffer source, int sourcePointer, int numBytes) {
27142731
source.copyTo(sourcePointer, this, offset, numBytes);
27152732
}
@@ -2826,9 +2843,10 @@ public void pointTo(byte[] buffer, int offset, int length) {
28262843
// TODO: support android
28272844
@NotForAndroid(reason = "Android does not support support off-heap memory only by address")
28282845
public static MemoryBuffer fromNativeAddress(long address, int size) {
2829-
if (Platform.IS_ANDROID)
2846+
if (Platform.IS_ANDROID) {
28302847
throw new UnsupportedOperationException(
28312848
"Android does not support support off-heap memory only by address");
2849+
}
28322850
return new MemoryBuffer(address, null, size, null, false);
28332851
}
28342852

@@ -2839,6 +2857,16 @@ public static MemoryBuffer wrap(byte[] buffer) {
28392857
return new MemoryBuffer(buffer, 0, buffer.length);
28402858
}
28412859

2860+
/**
2861+
* Creates a new memory segment that represents the memory backing the given byte buffer section
2862+
* of [buffer.position(), buffer,limit()].
2863+
*
2864+
* @param byteBuffer a direct buffer or heap buffer
2865+
*/
2866+
public static MemoryBuffer wrap(ByteBuffer byteBuffer) {
2867+
return fromByteBuffer(byteBuffer, byteBuffer.remaining());
2868+
}
2869+
28422870
public static MemoryBuffer buffer(int size) {
28432871
return newHeapBuffer(size);
28442872
}
@@ -2855,16 +2883,6 @@ public static MemoryBuffer bufferDirect(int size) {
28552883
return new MemoryBuffer(-1, ByteBuffer.allocateDirect(size), size, null, true);
28562884
}
28572885

2858-
/**
2859-
* Creates a new memory segment that represents the memory backing the given byte buffer section
2860-
* of [buffer.position(), buffer,limit()].
2861-
*
2862-
* @param byteBuffer a direct buffer or heap buffer
2863-
*/
2864-
public static MemoryBuffer wrap(ByteBuffer byteBuffer) {
2865-
return fromByteBuffer(byteBuffer, byteBuffer.remaining());
2866-
}
2867-
28682886
/**
28692887
* Creates a new memory buffer that represents the native memory at the absolute address given by
28702888
* the pointer.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,8 +347,8 @@ private void addDefaultSerializers() {
347347
addDefaultSerializer(Locale.class, new LocaleSerializer(fory));
348348
if (!Platform.IS_ANDROID) {
349349
addDefaultSerializer(
350-
LambdaSerializer.ReplaceStub.class,
351-
new LambdaSerializer(fory, LambdaSerializer.ReplaceStub.class));
350+
LambdaSerializer.ReplaceStub.class,
351+
new LambdaSerializer(fory, LambdaSerializer.ReplaceStub.class));
352352
}
353353
addDefaultSerializer(
354354
JdkProxySerializer.ReplaceStub.class,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ public abstract static class AbstractStringBuilderSerializer<T extends CharSeque
215215
if (RESTRICTED_STRING_BUILDER) {
216216
GET_CODER = null;
217217
GET_VALUE = null;
218-
}else {
218+
} else {
219219
GET_VALUE = (Function) makeGetterFunction(StringBuilder.class.getSuperclass(), "getValue");
220220
ToIntFunction<CharSequence> getCoder;
221221
try {

0 commit comments

Comments
 (0)