convert
This commit is contained in:
@@ -17,6 +17,7 @@ import java.util.concurrent.locks.*;
|
|||||||
* @author zhangjx
|
* @author zhangjx
|
||||||
* @param <R> Reader输入的子类型
|
* @param <R> Reader输入的子类型
|
||||||
* @param <W> Writer输出的子类型
|
* @param <W> Writer输出的子类型
|
||||||
|
* @param <T> T
|
||||||
*/
|
*/
|
||||||
public class OptionalCoder<R extends Reader, W extends Writer, T> extends SimpledCoder<R, W, Optional<T>> {
|
public class OptionalCoder<R extends Reader, W extends Writer, T> extends SimpledCoder<R, W, Optional<T>> {
|
||||||
|
|
||||||
@@ -109,4 +110,9 @@ public class OptionalCoder<R extends Reader, W extends Writer, T> extends Simple
|
|||||||
}
|
}
|
||||||
return Optional.ofNullable(this.decoder.convertFrom(in));
|
return Optional.ofNullable(this.decoder.convertFrom(in));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Class getType() {
|
||||||
|
return Optional.class;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,8 @@
|
|||||||
*/
|
*/
|
||||||
package org.redkale.convert;
|
package org.redkale.convert;
|
||||||
|
|
||||||
|
import org.redkale.annotation.Nullable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 反序列化的数据读取流
|
* 反序列化的数据读取流
|
||||||
*
|
*
|
||||||
@@ -93,7 +95,7 @@ public abstract class Reader {
|
|||||||
* @param componentDecoder Decodeable
|
* @param componentDecoder Decodeable
|
||||||
* @return 返回数组的长度
|
* @return 返回数组的长度
|
||||||
*/
|
*/
|
||||||
public abstract int readArrayB(Decodeable componentDecoder);
|
public abstract int readArrayB(@Nullable Decodeable componentDecoder);
|
||||||
|
|
||||||
/** 读取数组的尾端 */
|
/** 读取数组的尾端 */
|
||||||
public abstract void readArrayE();
|
public abstract void readArrayE();
|
||||||
|
|||||||
@@ -36,6 +36,9 @@ public abstract class SimpledCoder<R extends Reader, W extends Writer, T>
|
|||||||
if (type == null) {
|
if (type == null) {
|
||||||
Type[] ts = ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments();
|
Type[] ts = ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments();
|
||||||
type = ts[ts.length - 1];
|
type = ts[ts.length - 1];
|
||||||
|
if (!(type instanceof Class)) {
|
||||||
|
throw new ConvertException(type + " is not class");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return (Class<T>) type;
|
return (Class<T>) type;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,12 +28,11 @@ public class BsonArrayDecoder<T> extends ArrayDecoder<BsonReader, T> {
|
|||||||
@Override
|
@Override
|
||||||
public T[] convertFrom(BsonReader in) {
|
public T[] convertFrom(BsonReader in) {
|
||||||
this.checkInited();
|
this.checkInited();
|
||||||
byte[] typevals = new byte[1];
|
int len = in.readArrayB(this.componentDecoder);
|
||||||
int len = in.readArrayB(typevals, this.componentDecoder);
|
|
||||||
if (len == Reader.SIGN_NULL) {
|
if (len == Reader.SIGN_NULL) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
final Decodeable<BsonReader, T> itemDecoder = BsonFactory.typeEnum(typevals[0]);
|
final Decodeable<BsonReader, T> itemDecoder = BsonFactory.typeEnum(in.readArrayItemTypeEnum());
|
||||||
final List<T> result = new ArrayList();
|
final List<T> result = new ArrayList();
|
||||||
// 固定长度
|
// 固定长度
|
||||||
for (int i = 0; i < len; i++) {
|
for (int i = 0; i < len; i++) {
|
||||||
|
|||||||
@@ -5,12 +5,9 @@
|
|||||||
*/
|
*/
|
||||||
package org.redkale.convert.bson;
|
package org.redkale.convert.bson;
|
||||||
|
|
||||||
|
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import org.redkale.convert.*;
|
|
||||||
import static org.redkale.convert.Reader.SIGN_NULL;
|
import static org.redkale.convert.Reader.SIGN_NULL;
|
||||||
import org.redkale.convert.ext.ByteSimpledCoder;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 以ByteBuffer为数据载体的BsonReader
|
* 以ByteBuffer为数据载体的BsonReader
|
||||||
@@ -48,44 +45,6 @@ public class BsonByteBufferReader extends BsonReader {
|
|||||||
return currentBuffer.get(currentBuffer.position());
|
return currentBuffer.get(currentBuffer.position());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public int readMapB(byte[] typevals, Decodeable keyDecoder, Decodeable valueDecoder) {
|
|
||||||
short bt = readShort();
|
|
||||||
if (bt == Reader.SIGN_NULL) {
|
|
||||||
return bt;
|
|
||||||
}
|
|
||||||
short lt = readShort();
|
|
||||||
byte kt = readByte();
|
|
||||||
byte vt = readByte();
|
|
||||||
if (typevals != null) {
|
|
||||||
typevals[0] = kt;
|
|
||||||
typevals[1] = vt;
|
|
||||||
}
|
|
||||||
return (bt & 0xffff) << 16 | (lt & 0xffff);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 判断下一个非空白字节是否为[
|
|
||||||
*
|
|
||||||
* @param typevals byte[]
|
|
||||||
* @param componentDecoder Decodeable
|
|
||||||
* @return 数组长度或 SIGN_NULL
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public final int readArrayB(byte[] typevals, Decodeable componentDecoder) {
|
|
||||||
short bt = readShort();
|
|
||||||
if (bt == Reader.SIGN_NULL) {
|
|
||||||
return bt;
|
|
||||||
}
|
|
||||||
short lt = readShort();
|
|
||||||
if (componentDecoder != null && componentDecoder != ByteSimpledCoder.instance) {
|
|
||||||
byte comval = readByte();
|
|
||||||
if (typevals != null) {
|
|
||||||
typevals[0] = comval;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return (bt & 0xffff) << 16 | (lt & 0xffff);
|
|
||||||
}
|
|
||||||
// ------------------------------------------------------------
|
// ------------------------------------------------------------
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -27,12 +27,11 @@ public class BsonCollectionDecoder<T> extends CollectionDecoder<BsonReader, T> {
|
|||||||
@Override
|
@Override
|
||||||
public Collection<T> convertFrom(BsonReader in) {
|
public Collection<T> convertFrom(BsonReader in) {
|
||||||
this.checkInited();
|
this.checkInited();
|
||||||
byte[] typevals = new byte[1];
|
int len = in.readArrayB(componentDecoder);
|
||||||
int len = in.readArrayB(typevals, componentDecoder);
|
|
||||||
if (len == Reader.SIGN_NULL) {
|
if (len == Reader.SIGN_NULL) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
final Decodeable<BsonReader, T> itemDecoder = BsonFactory.typeEnum(typevals[0]);
|
final Decodeable<BsonReader, T> itemDecoder = BsonFactory.typeEnum(in.readArrayItemTypeEnum());
|
||||||
final Collection<T> result = this.creator.create();
|
final Collection<T> result = this.creator.create();
|
||||||
// 固定长度
|
// 固定长度
|
||||||
for (int i = 0; i < len; i++) {
|
for (int i = 0; i < len; i++) {
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import java.lang.reflect.Type;
|
|||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.function.*;
|
import java.util.function.*;
|
||||||
|
import org.redkale.annotation.Nullable;
|
||||||
import org.redkale.convert.*;
|
import org.redkale.convert.*;
|
||||||
import org.redkale.util.*;
|
import org.redkale.util.*;
|
||||||
|
|
||||||
@@ -48,10 +49,16 @@ public class BsonConvert extends BinaryConvert<BsonReader, BsonWriter> {
|
|||||||
|
|
||||||
private final ThreadLocal<BsonWriter> writerPool = Utility.withInitialThreadLocal(BsonWriter::new);
|
private final ThreadLocal<BsonWriter> writerPool = Utility.withInitialThreadLocal(BsonWriter::new);
|
||||||
|
|
||||||
private final Consumer<BsonWriter> writerConsumer = w -> offerWriter(w);
|
private final Consumer<BsonWriter> writerConsumer = this::offerWriter;
|
||||||
|
|
||||||
private final ThreadLocal<BsonReader> readerPool = Utility.withInitialThreadLocal(BsonReader::new);
|
private final ThreadLocal<BsonReader> readerPool = Utility.withInitialThreadLocal(BsonReader::new);
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private Encodeable lastEncodeable;
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private Decodeable lastDecodeable;
|
||||||
|
|
||||||
protected BsonConvert(ConvertFactory<BsonReader, BsonWriter> factory, int features) {
|
protected BsonConvert(ConvertFactory<BsonReader, BsonWriter> factory, int features) {
|
||||||
super(factory, features);
|
super(factory, features);
|
||||||
}
|
}
|
||||||
@@ -166,8 +173,12 @@ public class BsonConvert extends BinaryConvert<BsonReader, BsonWriter> {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
final BsonReader in = new BsonReader(bytes, offset, len);
|
final BsonReader in = new BsonReader(bytes, offset, len);
|
||||||
@SuppressWarnings("unchecked")
|
Decodeable decoder = this.lastDecodeable;
|
||||||
T rs = (T) factory.loadDecoder(type).convertFrom(in);
|
if (decoder == null || decoder.getType() != type) {
|
||||||
|
decoder = factory.loadDecoder(type);
|
||||||
|
this.lastDecodeable = decoder;
|
||||||
|
}
|
||||||
|
T rs = (T) decoder.convertFrom(in);
|
||||||
return rs;
|
return rs;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -185,7 +196,12 @@ public class BsonConvert extends BinaryConvert<BsonReader, BsonWriter> {
|
|||||||
if (type == null || Utility.isEmpty(buffers)) {
|
if (type == null || Utility.isEmpty(buffers)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return (T) factory.loadDecoder(type).convertFrom(new BsonByteBufferReader(buffers));
|
Decodeable decoder = this.lastDecodeable;
|
||||||
|
if (decoder == null || decoder.getType() != type) {
|
||||||
|
decoder = factory.loadDecoder(type);
|
||||||
|
this.lastDecodeable = decoder;
|
||||||
|
}
|
||||||
|
return (T) decoder.convertFrom(new BsonByteBufferReader(buffers));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -194,8 +210,12 @@ public class BsonConvert extends BinaryConvert<BsonReader, BsonWriter> {
|
|||||||
if (type == null) {
|
if (type == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@SuppressWarnings("unchecked")
|
Decodeable decoder = this.lastDecodeable;
|
||||||
T rs = (T) factory.loadDecoder(type).convertFrom(reader);
|
if (decoder == null || decoder.getType() != type) {
|
||||||
|
decoder = factory.loadDecoder(type);
|
||||||
|
this.lastDecodeable = decoder;
|
||||||
|
}
|
||||||
|
T rs = (T) decoder.convertFrom(reader);
|
||||||
return rs;
|
return rs;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -209,8 +229,14 @@ public class BsonConvert extends BinaryConvert<BsonReader, BsonWriter> {
|
|||||||
offerWriter(out);
|
offerWriter(out);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
final Type t = type == null ? value.getClass() : type;
|
||||||
|
Encodeable encoder = this.lastEncodeable;
|
||||||
|
if (encoder == null || encoder.getType() != t) {
|
||||||
|
encoder = factory.loadEncoder(t);
|
||||||
|
this.lastEncodeable = encoder;
|
||||||
|
}
|
||||||
final BsonWriter writer = pollWriter();
|
final BsonWriter writer = pollWriter();
|
||||||
factory.loadEncoder(type == null ? value.getClass() : type).convertTo(writer, value);
|
encoder.convertTo(writer, value);
|
||||||
byte[] result = writer.toArray();
|
byte[] result = writer.toArray();
|
||||||
offerWriter(writer);
|
offerWriter(writer);
|
||||||
return result;
|
return result;
|
||||||
@@ -227,7 +253,12 @@ public class BsonConvert extends BinaryConvert<BsonReader, BsonWriter> {
|
|||||||
if (type == null && value == null) {
|
if (type == null && value == null) {
|
||||||
writer.writeNull();
|
writer.writeNull();
|
||||||
} else {
|
} else {
|
||||||
factory.loadEncoder(type).convertTo(writer, value);
|
Encodeable encoder = this.lastEncodeable;
|
||||||
|
if (encoder == null || encoder.getType() != type) {
|
||||||
|
encoder = factory.loadEncoder(type);
|
||||||
|
this.lastEncodeable = encoder;
|
||||||
|
}
|
||||||
|
encoder.convertTo(writer, value);
|
||||||
}
|
}
|
||||||
writer.completed(handler, writerConsumer);
|
writer.completed(handler, writerConsumer);
|
||||||
}
|
}
|
||||||
@@ -239,6 +270,11 @@ public class BsonConvert extends BinaryConvert<BsonReader, BsonWriter> {
|
|||||||
if (type == null && value == null) {
|
if (type == null && value == null) {
|
||||||
writer.writeNull();
|
writer.writeNull();
|
||||||
} else {
|
} else {
|
||||||
|
Encodeable encoder = this.lastEncodeable;
|
||||||
|
if (encoder == null || encoder.getType() != type) {
|
||||||
|
encoder = factory.loadEncoder(type);
|
||||||
|
this.lastEncodeable = encoder;
|
||||||
|
}
|
||||||
factory.loadEncoder(type == null ? value.getClass() : type).convertTo(writer, value);
|
factory.loadEncoder(type == null ? value.getClass() : type).convertTo(writer, value);
|
||||||
}
|
}
|
||||||
writer.directTo(array);
|
writer.directTo(array);
|
||||||
@@ -273,7 +309,13 @@ public class BsonConvert extends BinaryConvert<BsonReader, BsonWriter> {
|
|||||||
if (type == null && value == null) { // 必须判断type==null
|
if (type == null && value == null) { // 必须判断type==null
|
||||||
writer.writeNull();
|
writer.writeNull();
|
||||||
} else {
|
} else {
|
||||||
factory.loadEncoder(type == null ? value.getClass() : type).convertTo(writer, value);
|
final Type t = type == null ? value.getClass() : type;
|
||||||
|
Encodeable encoder = this.lastEncodeable;
|
||||||
|
if (encoder == null || encoder.getType() != t) {
|
||||||
|
encoder = factory.loadEncoder(t);
|
||||||
|
this.lastEncodeable = encoder;
|
||||||
|
}
|
||||||
|
encoder.convertTo(writer, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -282,7 +324,13 @@ public class BsonConvert extends BinaryConvert<BsonReader, BsonWriter> {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
final BsonWriter writer = writerPool.get().withFeatures(features);
|
final BsonWriter writer = writerPool.get().withFeatures(features);
|
||||||
factory.loadEncoder(type == null ? value.getClass() : type).convertTo(writer, value);
|
final Type t = type == null ? value.getClass() : type;
|
||||||
|
Encodeable encoder = this.lastEncodeable;
|
||||||
|
if (encoder == null || encoder.getType() != t) {
|
||||||
|
encoder = factory.loadEncoder(t);
|
||||||
|
this.lastEncodeable = encoder;
|
||||||
|
}
|
||||||
|
encoder.convertTo(writer, value);
|
||||||
return writer;
|
return writer;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,13 +27,12 @@ public class BsonMapDecoder<K, V> extends MapDecoder<BsonReader, K, V> {
|
|||||||
@Override
|
@Override
|
||||||
public Map<K, V> convertFrom(BsonReader in) {
|
public Map<K, V> convertFrom(BsonReader in) {
|
||||||
this.checkInited();
|
this.checkInited();
|
||||||
byte[] typevals = new byte[2];
|
int len = in.readMapB(this.keyDecoder, this.valueDecoder);
|
||||||
int len = in.readMapB(typevals, this.keyDecoder, this.valueDecoder);
|
|
||||||
if (len == Reader.SIGN_NULL) {
|
if (len == Reader.SIGN_NULL) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
Decodeable<BsonReader, K> kdecoder = BsonFactory.typeEnum(typevals[0]);
|
Decodeable<BsonReader, K> kdecoder = BsonFactory.typeEnum(in.readMapKeyTypeEnum());
|
||||||
Decodeable<BsonReader, V> vdecoder = BsonFactory.typeEnum(typevals[1]);
|
Decodeable<BsonReader, V> vdecoder = BsonFactory.typeEnum(in.readmapValueTypeEnum());
|
||||||
final Map<K, V> result = this.creator.create();
|
final Map<K, V> result = this.creator.create();
|
||||||
// 固定长度
|
// 固定长度
|
||||||
for (int i = 0; i < len; i++) {
|
for (int i = 0; i < len; i++) {
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
package org.redkale.convert.bson;
|
package org.redkale.convert.bson;
|
||||||
|
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import org.redkale.annotation.Nullable;
|
||||||
import org.redkale.convert.*;
|
import org.redkale.convert.*;
|
||||||
import static org.redkale.convert.Reader.SIGN_NULL;
|
import static org.redkale.convert.Reader.SIGN_NULL;
|
||||||
import org.redkale.convert.ext.ByteSimpledCoder;
|
import org.redkale.convert.ext.ByteSimpledCoder;
|
||||||
@@ -32,7 +33,13 @@ public class BsonReader extends Reader {
|
|||||||
|
|
||||||
public static final byte VERBOSE_YES = 2;
|
public static final byte VERBOSE_YES = 2;
|
||||||
|
|
||||||
protected byte typeval; // 字段的类型值 对应 BsonWriter.writeField
|
protected byte fieldTypeEnum; // 字段的类型值 对应 BsonWriter.writeField
|
||||||
|
|
||||||
|
protected byte arrayItemTypeEnum;
|
||||||
|
|
||||||
|
protected byte mapKeyTypeEnum;
|
||||||
|
|
||||||
|
protected byte mapValueTypeEnum;
|
||||||
|
|
||||||
protected int position = -1;
|
protected int position = -1;
|
||||||
|
|
||||||
@@ -40,10 +47,6 @@ public class BsonReader extends Reader {
|
|||||||
|
|
||||||
public BsonReader() {}
|
public BsonReader() {}
|
||||||
|
|
||||||
public static ObjectPool<BsonReader> createPool(int max) {
|
|
||||||
return ObjectPool.createSafePool(max, (Object... params) -> new BsonReader(), null, (t) -> t.recycle());
|
|
||||||
}
|
|
||||||
|
|
||||||
public BsonReader(byte[] bytes) {
|
public BsonReader(byte[] bytes) {
|
||||||
setBytes(bytes, 0, bytes.length);
|
setBytes(bytes, 0, bytes.length);
|
||||||
}
|
}
|
||||||
@@ -79,7 +82,10 @@ public class BsonReader extends Reader {
|
|||||||
|
|
||||||
protected boolean recycle() {
|
protected boolean recycle() {
|
||||||
this.position = -1;
|
this.position = -1;
|
||||||
this.typeval = 0;
|
this.fieldTypeEnum = 0;
|
||||||
|
this.arrayItemTypeEnum = 0;
|
||||||
|
this.mapKeyTypeEnum = 0;
|
||||||
|
this.mapValueTypeEnum = 0;
|
||||||
// this.limit = -1;
|
// this.limit = -1;
|
||||||
this.content = null;
|
this.content = null;
|
||||||
return true;
|
return true;
|
||||||
@@ -94,11 +100,11 @@ public class BsonReader extends Reader {
|
|||||||
@Override
|
@Override
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public final void skipValue() {
|
public final void skipValue() {
|
||||||
if (typeval == 0) {
|
final byte val = this.fieldTypeEnum;
|
||||||
|
if (val == 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
final byte val = this.typeval;
|
this.fieldTypeEnum = 0;
|
||||||
this.typeval = 0;
|
|
||||||
switch (val) {
|
switch (val) {
|
||||||
case 11:
|
case 11:
|
||||||
readBoolean();
|
readBoolean();
|
||||||
@@ -165,54 +171,57 @@ public class BsonReader extends Reader {
|
|||||||
return this.content[this.position];
|
return this.content[this.position];
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public final byte readMapKeyTypeEnum() {
|
||||||
public int readMapB(Decodeable keyDecoder, Decodeable valueDecoder) {
|
return mapKeyTypeEnum;
|
||||||
return readMapB(null, keyDecoder, valueDecoder);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int readMapB(byte[] typevals, Decodeable keyDecoder, Decodeable valueDecoder) {
|
public final byte readmapValueTypeEnum() {
|
||||||
|
return mapValueTypeEnum;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public final int readMapB(Decodeable keyDecoder, Decodeable valueDecoder) {
|
||||||
short bt = readShort();
|
short bt = readShort();
|
||||||
if (bt == Reader.SIGN_NULL) {
|
if (bt == Reader.SIGN_NULL) {
|
||||||
|
this.mapKeyTypeEnum = 0;
|
||||||
|
this.mapValueTypeEnum = 0;
|
||||||
return bt;
|
return bt;
|
||||||
}
|
}
|
||||||
int rs = (bt & 0xffff) << 16 | ((content[++this.position] & 0xff) << 8) | (content[++this.position] & 0xff);
|
short lt = readShort();
|
||||||
byte kt = readByte();
|
this.mapKeyTypeEnum = readByte();
|
||||||
byte vt = readByte();
|
this.mapValueTypeEnum = readByte();
|
||||||
if (typevals != null) {
|
return (bt & 0xffff) << 16 | (lt & 0xffff);
|
||||||
typevals[0] = kt;
|
|
||||||
typevals[1] = vt;
|
|
||||||
}
|
|
||||||
return rs;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final void readMapE() {
|
public final void readMapE() {
|
||||||
// do nothing
|
this.mapKeyTypeEnum = 0;
|
||||||
|
this.mapValueTypeEnum = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public final byte readArrayItemTypeEnum() {
|
||||||
|
return arrayItemTypeEnum;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int readArrayB(Decodeable componentDecoder) {
|
public final int readArrayB(@Nullable Decodeable componentDecoder) {
|
||||||
return readArrayB(null, componentDecoder);
|
|
||||||
}
|
|
||||||
|
|
||||||
public int readArrayB(byte[] typevals, Decodeable componentDecoder) { // componentDecoder可能为null
|
|
||||||
short bt = readShort();
|
short bt = readShort();
|
||||||
if (bt == Reader.SIGN_NULL) {
|
if (bt == Reader.SIGN_NULL) {
|
||||||
|
this.arrayItemTypeEnum = 0;
|
||||||
return bt;
|
return bt;
|
||||||
}
|
}
|
||||||
int rs = (bt & 0xffff) << 16 | ((content[++this.position] & 0xff) << 8) | (content[++this.position] & 0xff);
|
short lt = readShort();
|
||||||
if (componentDecoder != null && componentDecoder != ByteSimpledCoder.instance) {
|
if (componentDecoder != null && componentDecoder != ByteSimpledCoder.instance) {
|
||||||
byte comval = readByte();
|
this.arrayItemTypeEnum = readByte();
|
||||||
if (typevals != null) {
|
} else {
|
||||||
typevals[0] = comval;
|
this.arrayItemTypeEnum = 0;
|
||||||
}
|
}
|
||||||
}
|
return (bt & 0xffff) << 16 | (lt & 0xffff);
|
||||||
return rs;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final void readArrayE() {
|
public final void readArrayE() {
|
||||||
// do nothing
|
this.arrayItemTypeEnum = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 判断下一个非空白字节是否: */
|
/** 判断下一个非空白字节是否: */
|
||||||
@@ -232,7 +241,7 @@ public class BsonReader extends Reader {
|
|||||||
* @return 是否存在
|
* @return 是否存在
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public boolean hasNext() {
|
public final boolean hasNext() {
|
||||||
byte b = readByte();
|
byte b = readByte();
|
||||||
if (b == SIGN_HASNEXT) {
|
if (b == SIGN_HASNEXT) {
|
||||||
return true;
|
return true;
|
||||||
@@ -247,7 +256,7 @@ public class BsonReader extends Reader {
|
|||||||
@Override
|
@Override
|
||||||
public final DeMember readFieldName(final DeMemberInfo memberInfo) {
|
public final DeMember readFieldName(final DeMemberInfo memberInfo) {
|
||||||
final String exceptedField = readSmallString();
|
final String exceptedField = readSmallString();
|
||||||
this.typeval = readByte();
|
this.fieldTypeEnum = readByte();
|
||||||
return memberInfo.getMemberByField(exceptedField);
|
return memberInfo.getMemberByField(exceptedField);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -264,7 +273,8 @@ public class BsonReader extends Reader {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final byte[] readByteArray() {
|
public final byte[] readByteArray() {
|
||||||
int len = readArrayB(null, null);
|
int len = readArrayB(null);
|
||||||
|
this.arrayItemTypeEnum = 0;
|
||||||
if (len == Reader.SIGN_NULL) {
|
if (len == Reader.SIGN_NULL) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,12 +28,11 @@ public class BsonStreamDecoder<T> extends StreamDecoder<BsonReader, T> {
|
|||||||
@Override
|
@Override
|
||||||
public Stream<T> convertFrom(BsonReader in) {
|
public Stream<T> convertFrom(BsonReader in) {
|
||||||
this.checkInited();
|
this.checkInited();
|
||||||
byte[] typevals = new byte[1];
|
int len = in.readArrayB(componentDecoder);
|
||||||
int len = in.readArrayB(typevals, componentDecoder);
|
|
||||||
if (len == Reader.SIGN_NULL) {
|
if (len == Reader.SIGN_NULL) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
final Decodeable<BsonReader, T> itemDecoder = BsonFactory.typeEnum(typevals[0]);
|
final Decodeable<BsonReader, T> itemDecoder = BsonFactory.typeEnum(in.readArrayItemTypeEnum());
|
||||||
final List<T> result = new ArrayList();
|
final List<T> result = new ArrayList();
|
||||||
// 固定长度
|
// 固定长度
|
||||||
for (int i = 0; i < len; i++) {
|
for (int i = 0; i < len; i++) {
|
||||||
|
|||||||
@@ -27,10 +27,6 @@ public class BsonWriter extends Writer implements ByteTuple {
|
|||||||
|
|
||||||
protected int count;
|
protected int count;
|
||||||
|
|
||||||
public static ObjectPool<BsonWriter> createPool(int max) {
|
|
||||||
return ObjectPool.createSafePool(max, (Object... params) -> new BsonWriter(), null, (t) -> t.recycle());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public byte[] content() {
|
public byte[] content() {
|
||||||
return content;
|
return content;
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import java.nio.ByteBuffer;
|
|||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.function.*;
|
import java.util.function.*;
|
||||||
|
import org.redkale.annotation.Nullable;
|
||||||
import org.redkale.convert.*;
|
import org.redkale.convert.*;
|
||||||
import org.redkale.service.RetResult;
|
import org.redkale.service.RetResult;
|
||||||
import org.redkale.util.*;
|
import org.redkale.util.*;
|
||||||
@@ -37,8 +38,10 @@ public class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
|||||||
|
|
||||||
private final ThreadLocal<JsonReader> readerPool = Utility.withInitialThreadLocal(JsonReader::new);
|
private final ThreadLocal<JsonReader> readerPool = Utility.withInitialThreadLocal(JsonReader::new);
|
||||||
|
|
||||||
|
@Nullable
|
||||||
private Encodeable lastEncodeable;
|
private Encodeable lastEncodeable;
|
||||||
|
|
||||||
|
@Nullable
|
||||||
private Decodeable lastDecodeable;
|
private Decodeable lastDecodeable;
|
||||||
|
|
||||||
protected JsonConvert(JsonFactory factory, int features) {
|
protected JsonConvert(JsonFactory factory, int features) {
|
||||||
|
|||||||
Reference in New Issue
Block a user