Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ private static byte[] getByteArray(int arrLen) {
}

private static final byte[] array1 = getByteArray(256);
private static final MemoryBuffer buffer1 = MemoryUtils.wrap(getByteArray(256));
private static final MemoryBuffer buffer1 = MemoryBuffer.wrap(getByteArray(256));

// @org.openjdk.jmh.annotations.Benchmark
public Object bufferUnsafeReadByte(MemoryState state) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,10 @@ public void setup() {
.build();
switch (bufferType) {
case array:
buffer = MemoryUtils.buffer(1024 * 512);
buffer = MemoryBuffer.buffer(1024 * 512);
break;
case directBuffer:
buffer = MemoryUtils.wrap(ByteBuffer.allocateDirect(1024 * 512));
buffer = MemoryBuffer.wrap(ByteBuffer.allocateDirect(1024 * 512));
break;
}
fory.register(ArraysData.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ public void setup() {
public void setupBuffer() {
switch (bufferType) {
case array:
buffer = MemoryUtils.buffer(1024 * 512);
buffer = MemoryBuffer.buffer(1024 * 512);
break;
case directBuffer:
buffer = MemoryUtils.wrap(ByteBuffer.allocateDirect(1024 * 512));
buffer = MemoryBuffer.wrap(ByteBuffer.allocateDirect(1024 * 512));
break;
}
}
Expand Down
3 changes: 3 additions & 0 deletions java/fory-core/src/main/java/org/apache/fory/BaseFory.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.io.OutputStream;
import java.util.function.Function;
import org.apache.fory.annotation.NotForAndroid;
import org.apache.fory.io.ForyInputStream;
import org.apache.fory.io.ForyReadableChannel;
import org.apache.fory.memory.MemoryBuffer;
Expand Down Expand Up @@ -115,6 +116,7 @@ public interface BaseFory {
* Serialize <code>obj</code> to a off-heap buffer specified by <code>address</code> and <code>
* size</code>.
*/
@NotForAndroid(reason = "Android does not support support off-heap memory only by address")
MemoryBuffer serialize(Object obj, long address, int size);

/** Serialize data into buffer. */
Expand All @@ -138,6 +140,7 @@ public interface BaseFory {
* Deserialize <code>obj</code> from a off-heap buffer specified by <code>address</code> and
* <code>size</code>.
*/
@NotForAndroid(reason = "Android does not support support off-heap memory only by address")
Object deserialize(long address, int size);

/** Deserialize <code>obj</code> from a <code>buffer</code>. */
Expand Down
17 changes: 10 additions & 7 deletions java/fory-core/src/main/java/org/apache/fory/Fory.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.function.Consumer;
import java.util.function.Function;
import javax.annotation.concurrent.NotThreadSafe;
import org.apache.fory.annotation.NotForAndroid;
import org.apache.fory.builder.JITContext;
import org.apache.fory.collection.IdentityMap;
import org.apache.fory.config.CompatibleMode;
Expand Down Expand Up @@ -265,9 +266,10 @@ public <T> Serializer<T> getSerializer(Class<T> cls) {
}
}

@NotForAndroid(reason = "Android does not support support off-heap memory only by address")
@Override
public MemoryBuffer serialize(Object obj, long address, int size) {
MemoryBuffer buffer = MemoryUtils.buffer(address, size);
MemoryBuffer buffer = MemoryBuffer.fromNativeAddress(address, size);
serialize(buffer, obj, null);
return buffer;
}
Expand Down Expand Up @@ -790,13 +792,13 @@ public long readInt64(MemoryBuffer buffer) {

@Override
public Object deserialize(byte[] bytes) {
return deserialize(MemoryUtils.wrap(bytes), null);
return deserialize(MemoryBuffer.wrap(bytes), null);
}

@SuppressWarnings("unchecked")
@Override
public <T> T deserialize(byte[] bytes, Class<T> type) {
MemoryBuffer buffer = MemoryUtils.wrap(bytes);
MemoryBuffer buffer = MemoryBuffer.wrap(bytes);
if (!crossLanguage && shareMeta) {
byte bitmap = buffer.readByte();
if ((bitmap & isNilFlag) == isNilFlag) {
Expand All @@ -816,12 +818,13 @@ public <T> T deserialize(byte[] bytes, Class<T> type) {

@Override
public Object deserialize(byte[] bytes, Iterable<MemoryBuffer> outOfBandBuffers) {
return deserialize(MemoryUtils.wrap(bytes), outOfBandBuffers);
return deserialize(MemoryBuffer.wrap(bytes), outOfBandBuffers);
}

@NotForAndroid(reason = "Android does not support support off-heap memory only by address")
@Override
public Object deserialize(long address, int size) {
return deserialize(MemoryUtils.buffer(address, size), null);
return deserialize(MemoryBuffer.fromNativeAddress(address, size), null);
}

@Override
Expand Down Expand Up @@ -1207,7 +1210,7 @@ public void serializeJavaObject(OutputStream outputStream, Object obj) {

@Override
public <T> T deserializeJavaObject(byte[] data, Class<T> cls) {
return deserializeJavaObject(MemoryBuffer.fromByteArray(data), cls);
return deserializeJavaObject(MemoryBuffer.wrap(data), cls);
}

@Override
Expand Down Expand Up @@ -1327,7 +1330,7 @@ public void serializeJavaObjectAndClass(OutputStream outputStream, Object obj) {
*/
@Override
public Object deserializeJavaObjectAndClass(byte[] data) {
return deserializeJavaObjectAndClass(MemoryBuffer.fromByteArray(data));
return deserializeJavaObjectAndClass(MemoryBuffer.wrap(data));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
import java.util.function.Function;
import javax.annotation.concurrent.ThreadSafe;
import org.apache.fory.annotation.Internal;
import org.apache.fory.annotation.NotForAndroid;
import org.apache.fory.io.ForyInputStream;
import org.apache.fory.io.ForyReadableChannel;
import org.apache.fory.memory.MemoryBuffer;
import org.apache.fory.memory.MemoryUtils;
import org.apache.fory.serializer.BufferCallback;
import org.apache.fory.util.LoaderBinding;
import org.apache.fory.util.LoaderBinding.StagingType;
Expand All @@ -44,7 +44,7 @@
@ThreadSafe
public class ThreadLocalFory extends AbstractThreadSafeFory {
private final ThreadLocal<MemoryBuffer> bufferLocal =
ThreadLocal.withInitial(() -> MemoryUtils.buffer(32));
ThreadLocal.withInitial(() -> MemoryBuffer.buffer(32));

private final ThreadLocal<LoaderBinding> bindingThreadLocal;
private Consumer<Fory> factoryCallback;
Expand Down Expand Up @@ -107,6 +107,7 @@ public byte[] serialize(Object obj, BufferCallback callback) {
return buffer.getBytes(0, buffer.writerIndex());
}

@NotForAndroid(reason = "Android does not support support off-heap memory only by address")
@Override
public MemoryBuffer serialize(Object obj, long address, int size) {
return bindingThreadLocal.get().get().serialize(obj, address, size);
Expand Down Expand Up @@ -147,6 +148,7 @@ public Object deserialize(byte[] bytes, Iterable<MemoryBuffer> outOfBandBuffers)
return bindingThreadLocal.get().get().deserialize(bytes, outOfBandBuffers);
}

@NotForAndroid(reason = "Android does not support support off-heap memory only by address")
@Override
public Object deserialize(long address, int size) {
return bindingThreadLocal.get().get().deserialize(address, size);
Expand All @@ -159,7 +161,7 @@ public Object deserialize(MemoryBuffer buffer) {

@Override
public Object deserialize(ByteBuffer byteBuffer) {
return bindingThreadLocal.get().get().deserialize(MemoryUtils.wrap(byteBuffer));
return bindingThreadLocal.get().get().deserialize(MemoryBuffer.wrap(byteBuffer));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.fory.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.CLASS)
@Target({
ElementType.METHOD,
ElementType.CONSTRUCTOR,
ElementType.TYPE,
})
public @interface NotForAndroid {
String reason() default "This API is not supported or is unsafe on the Android platform.";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.fory.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Documented
@Retention(RetentionPolicy.CLASS)
@Target({ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.TYPE})
public @interface PartialAndroidSupport {

int minApiLevel() default -1;

String reason() default "该方法在 Android 上的所有分支和条件下不保证完全支持,请查阅文档或源码。";

String docUrl() default "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ public int fillBuffer(int minFillSize) {
@Override
public void readTo(byte[] dst, int dstIndex, int length) {}

@Override
public void readToUnsafe(Object target, long targetPointer, int numBytes) {}

@Override
public void readToByteBuffer(ByteBuffer dst, int length) {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.nio.ByteBuffer;
import javax.annotation.concurrent.NotThreadSafe;
import org.apache.fory.memory.MemoryBuffer;
import org.apache.fory.memory.Platform;

/**
* A buffered stream by fory. Do not use original {@link InputStream} when this stream object
Expand All @@ -45,7 +44,7 @@ public ForyInputStream(InputStream stream, int bufferSize) {
this.stream = stream;
this.bufferSize = bufferSize;
byte[] bytes = new byte[bufferSize];
this.buffer = MemoryBuffer.fromByteArray(bytes, 0, 0, this);
this.buffer = new MemoryBuffer(bytes, 0, 0, this);
}

@Override
Expand Down Expand Up @@ -115,18 +114,50 @@ public void readTo(byte[] dst, int dstIndex, int len) {
}
}

@Override
public void readToUnsafe(Object target, long targetPointer, int numBytes) {
MemoryBuffer buf = buffer;
int remaining = buf.remaining();
if (remaining < numBytes) {
fillBuffer(numBytes - remaining);
}
byte[] heapMemory = buf.getHeapMemory();
long address = buf.getUnsafeReaderAddress();
Platform.copyMemory(heapMemory, address, target, targetPointer, numBytes);
buf.increaseReaderIndex(numBytes);
}
// @Override
// public void readCharsTo(char[] target, int offset, int length) {
// int numBytes = length * 2;
// MemoryBuffer buf = buffer;
// int remaining = buf.remaining();
// if (remaining < numBytes) {
// fillBuffer(numBytes - remaining);
// }
// buf.readCharsDirect(target, offset, length);
// }
//
// @Override
// public void readLongsTo(long[] target, int offset, int length) {
// int numBytes = length * 8;
// MemoryBuffer buf = buffer;
// int remaining = buf.remaining();
// if (remaining < numBytes) {
// fillBuffer(numBytes - remaining);
// }
// buf.readLongsDirect(target, offset, length);
// }
//
// @Override
// public void readBoolsTo(boolean[] target, int offset, int length) {
// MemoryBuffer buf = buffer;
// int remaining = buf.remaining();
// if (remaining < length) {
// fillBuffer(length - remaining);
// }
// buf.readBoolsDirect(target, offset, length);
// }

// @Override
// public void readToUnsafe(Object target, long targetPointer, int numBytes) {
// MemoryBuffer buf = buffer;
// int remaining = buf.remaining();
// if (remaining < numBytes) {
// fillBuffer(numBytes - remaining);
// }
// byte[] heapMemory = buf.getHeapMemory();
// long address = buf.getUnsafeReaderAddress();
// Platform.copyMemory(heapMemory, address, target, targetPointer, numBytes);
// buf.increaseReaderIndex(numBytes);
// }

@Override
public void readToByteBuffer(ByteBuffer dst, int length) {
Expand Down
Loading
Loading