This commit is contained in:
Redkale
2018-07-25 16:36:09 +08:00
parent bfc2397dbf
commit 6486863d00
17 changed files with 42 additions and 39 deletions

View File

@@ -70,7 +70,7 @@ public class ArrayDecoder<T> implements Decodeable<Reader, T[]> {
int contentLength = -1; int contentLength = -1;
if (len == Reader.SIGN_NULL) return null; if (len == Reader.SIGN_NULL) return null;
if (len == Reader.SIGN_NOLENBUTBYTES) { if (len == Reader.SIGN_NOLENBUTBYTES) {
contentLength = in.readMemberContentLength(member); contentLength = in.readMemberContentLength(member, decoder);
len = Reader.SIGN_NOLENGTH; len = Reader.SIGN_NOLENGTH;
} }
if (this.decoder == null) { if (this.decoder == null) {
@@ -89,7 +89,7 @@ public class ArrayDecoder<T> implements Decodeable<Reader, T[]> {
boolean first = true; boolean first = true;
if (len == Reader.SIGN_NOLENGTH) { if (len == Reader.SIGN_NOLENGTH) {
int startPosition = in.position(); int startPosition = in.position();
while (in.hasNext(startPosition, contentLength)) { while (in.hasNext(member, startPosition, contentLength)) {
result.add(readMemberValue(in, member, first)); result.add(readMemberValue(in, member, first));
first = false; first = false;
} }

View File

@@ -70,7 +70,7 @@ public class CollectionDecoder<T> implements Decodeable<Reader, Collection<T>> {
int contentLength = -1; int contentLength = -1;
if (len == Reader.SIGN_NULL) return null; if (len == Reader.SIGN_NULL) return null;
if (len == Reader.SIGN_NOLENBUTBYTES) { if (len == Reader.SIGN_NOLENBUTBYTES) {
contentLength = in.readMemberContentLength(member); contentLength = in.readMemberContentLength(member, decoder);
len = Reader.SIGN_NOLENGTH; len = Reader.SIGN_NOLENGTH;
} }
if (this.decoder == null) { if (this.decoder == null) {
@@ -89,7 +89,7 @@ public class CollectionDecoder<T> implements Decodeable<Reader, Collection<T>> {
boolean first = true; boolean first = true;
if (len == Reader.SIGN_NOLENGTH) { if (len == Reader.SIGN_NOLENGTH) {
int startPosition = in.position(); int startPosition = in.position();
while (in.hasNext(startPosition, contentLength)) { while (in.hasNext(member, startPosition, contentLength)) {
result.add(readMemberValue(in, member, first)); result.add(readMemberValue(in, member, first));
first = false; first = false;
} }

View File

@@ -95,14 +95,14 @@ public class MapDecoder<K, V> implements Decodeable<Reader, Map<K, V>> {
int contentLength = -1; int contentLength = -1;
if (len == Reader.SIGN_NULL) return null; if (len == Reader.SIGN_NULL) return null;
if (len == Reader.SIGN_NOLENBUTBYTES) { if (len == Reader.SIGN_NOLENBUTBYTES) {
contentLength = in.readMemberContentLength(member); contentLength = in.readMemberContentLength(member, null);
len = Reader.SIGN_NOLENGTH; len = Reader.SIGN_NOLENGTH;
} }
final Map<K, V> result = this.creator.create(); final Map<K, V> result = this.creator.create();
boolean first = true; boolean first = true;
if (len == Reader.SIGN_NOLENGTH) { if (len == Reader.SIGN_NOLENGTH) {
int startPosition = in.position(); int startPosition = in.position();
while (in.hasNext(startPosition, contentLength)) { while (in.hasNext(member, startPosition, contentLength)) {
Reader entryReader = getMapEntryReader(in, member, first); Reader entryReader = getMapEntryReader(in, member, first);
K key = readKeyMember(entryReader, member, first); K key = readKeyMember(entryReader, member, first);
entryReader.readBlank(); entryReader.readBlank();

View File

@@ -28,12 +28,13 @@ public abstract class Reader {
* 是否还存在下个元素或字段 <br> * 是否还存在下个元素或字段 <br>
* 注意: 主要用于Array、Collection、Stream或Map等集合对象 * 注意: 主要用于Array、Collection、Stream或Map等集合对象
* *
* @param member DeMember
* @param startPosition 起始位置 * @param startPosition 起始位置
* @param contentLength 内容大小, 不确定的传-1 * @param contentLength 内容大小, 不确定的传-1
* *
* @return 是否还存在下个元素或字段 * @return 是否还存在下个元素或字段
*/ */
public abstract boolean hasNext(int startPosition, int contentLength); public abstract boolean hasNext(DeMember member, int startPosition, int contentLength);
/** /**
* 是否还存在下个元素或字段 * 是否还存在下个元素或字段
@@ -42,7 +43,7 @@ public abstract class Reader {
* @return 是否还存在下个元素或字段 * @return 是否还存在下个元素或字段
*/ */
public boolean hasNext() { public boolean hasNext() {
return hasNext(-1, -1); return hasNext(null, -1, -1);
} }
/** /**
@@ -56,11 +57,12 @@ public abstract class Reader {
* 读取字段值内容的字节数 <br> * 读取字段值内容的字节数 <br>
* 只有在readXXXB方法返回SIGN_NOLENBUTBYTES值才会调用此方法 * 只有在readXXXB方法返回SIGN_NOLENBUTBYTES值才会调用此方法
* *
* @param member DeMember * @param member DeMember
* @param decoder Decodeable
* *
* @return 内容大小, 不确定返回-1 * @return 内容大小, 不确定返回-1
*/ */
public abstract int readMemberContentLength(DeMember member); public abstract int readMemberContentLength(DeMember member, Decodeable decoder);
/** /**
* 跳过值(不包含值前面的字段) * 跳过值(不包含值前面的字段)

View File

@@ -66,7 +66,7 @@ public class StreamDecoder<T> implements Decodeable<Reader, Stream<T>> {
int contentLength = -1; int contentLength = -1;
if (len == Reader.SIGN_NULL) return null; if (len == Reader.SIGN_NULL) return null;
if (len == Reader.SIGN_NOLENBUTBYTES) { if (len == Reader.SIGN_NOLENBUTBYTES) {
contentLength = in.readMemberContentLength(member); contentLength = in.readMemberContentLength(member, this.decoder);
len = Reader.SIGN_NOLENGTH; len = Reader.SIGN_NOLENGTH;
} }
if (this.decoder == null) { if (this.decoder == null) {
@@ -85,7 +85,7 @@ public class StreamDecoder<T> implements Decodeable<Reader, Stream<T>> {
boolean first = true; boolean first = true;
if (len == Reader.SIGN_NOLENGTH) { if (len == Reader.SIGN_NOLENGTH) {
int startPosition = in.position(); int startPosition = in.position();
while (in.hasNext(startPosition, contentLength)) { while (in.hasNext(member, startPosition, contentLength)) {
result.add(readMemberValue(in, member, first)); result.add(readMemberValue(in, member, first));
first = false; first = false;
} }

View File

@@ -208,7 +208,7 @@ public class BsonReader extends Reader {
} }
@Override @Override
public int readMemberContentLength(DeMember member) { public int readMemberContentLength(DeMember member, Decodeable decoder) {
return -1; return -1;
} }
@@ -221,7 +221,7 @@ public class BsonReader extends Reader {
* @return 是否存在 * @return 是否存在
*/ */
@Override @Override
public boolean hasNext(int startPosition, int contentLength) { public boolean hasNext(DeMember member, int startPosition, int contentLength) {
byte b = readByte(); byte b = readByte();
if (b == SIGN_HASNEXT) return true; if (b == SIGN_HASNEXT) return true;
if (b != SIGN_NONEXT) throw new ConvertException("hasNext option must be (" + (SIGN_HASNEXT) if (b != SIGN_NONEXT) throw new ConvertException("hasNext option must be (" + (SIGN_HASNEXT)
@@ -267,14 +267,14 @@ public class BsonReader extends Reader {
int contentLength = -1; int contentLength = -1;
if (len == Reader.SIGN_NULL) return null; if (len == Reader.SIGN_NULL) return null;
if (len == Reader.SIGN_NOLENBUTBYTES) { if (len == Reader.SIGN_NOLENBUTBYTES) {
contentLength = readMemberContentLength(null); contentLength = readMemberContentLength(null, null);
len = Reader.SIGN_NOLENGTH; len = Reader.SIGN_NOLENGTH;
} }
if (len == Reader.SIGN_NOLENGTH) { if (len == Reader.SIGN_NOLENGTH) {
int size = 0; int size = 0;
byte[] data = new byte[8]; byte[] data = new byte[8];
int startPosition = position(); int startPosition = position();
while (hasNext(startPosition, contentLength)) { while (hasNext(null, startPosition, contentLength)) {
if (size >= data.length) { if (size >= data.length) {
byte[] newdata = new byte[data.length + 4]; byte[] newdata = new byte[data.length + 4];
System.arraycopy(data, 0, newdata, 0, size); System.arraycopy(data, 0, newdata, 0, size);

View File

@@ -46,14 +46,14 @@ public final class BoolArraySimpledCoder<R extends Reader, W extends Writer> ext
int contentLength = -1; int contentLength = -1;
if (len == Reader.SIGN_NULL) return null; if (len == Reader.SIGN_NULL) return null;
if (len == Reader.SIGN_NOLENBUTBYTES) { if (len == Reader.SIGN_NOLENBUTBYTES) {
contentLength = in.readMemberContentLength(null); contentLength = in.readMemberContentLength(null, BoolSimpledCoder.instance);
len = Reader.SIGN_NOLENGTH; len = Reader.SIGN_NOLENGTH;
} }
if (len == Reader.SIGN_NOLENGTH) { if (len == Reader.SIGN_NOLENGTH) {
int size = 0; int size = 0;
boolean[] data = new boolean[8]; boolean[] data = new boolean[8];
int startPosition = in.position(); int startPosition = in.position();
while (in.hasNext(startPosition, contentLength)) { while (in.hasNext(null, startPosition, contentLength)) {
if (size >= data.length) { if (size >= data.length) {
boolean[] newdata = new boolean[data.length + 4]; boolean[] newdata = new boolean[data.length + 4];
System.arraycopy(data, 0, newdata, 0, size); System.arraycopy(data, 0, newdata, 0, size);

View File

@@ -47,14 +47,14 @@ public final class ByteBufferSimpledCoder<R extends Reader, W extends Writer> ex
int contentLength = -1; int contentLength = -1;
if (len == Reader.SIGN_NULL) return null; if (len == Reader.SIGN_NULL) return null;
if (len == Reader.SIGN_NOLENBUTBYTES) { if (len == Reader.SIGN_NOLENBUTBYTES) {
contentLength = in.readMemberContentLength(null); contentLength = in.readMemberContentLength(null, ByteSimpledCoder.instance);
len = Reader.SIGN_NOLENGTH; len = Reader.SIGN_NOLENGTH;
} }
if (len == Reader.SIGN_NOLENGTH) { if (len == Reader.SIGN_NOLENGTH) {
int size = 0; int size = 0;
byte[] data = new byte[8]; byte[] data = new byte[8];
int startPosition = in.position(); int startPosition = in.position();
while (in.hasNext(startPosition, contentLength)) { while (in.hasNext(null, startPosition, contentLength)) {
if (size >= data.length) { if (size >= data.length) {
byte[] newdata = new byte[data.length + 4]; byte[] newdata = new byte[data.length + 4];
System.arraycopy(data, 0, newdata, 0, size); System.arraycopy(data, 0, newdata, 0, size);

View File

@@ -46,14 +46,14 @@ public final class CharArraySimpledCoder<R extends Reader, W extends Writer> ext
int contentLength = -1; int contentLength = -1;
if (len == Reader.SIGN_NULL) return null; if (len == Reader.SIGN_NULL) return null;
if (len == Reader.SIGN_NOLENBUTBYTES) { if (len == Reader.SIGN_NOLENBUTBYTES) {
contentLength = in.readMemberContentLength(null); contentLength = in.readMemberContentLength(null, CharSimpledCoder.instance);
len = Reader.SIGN_NOLENGTH; len = Reader.SIGN_NOLENGTH;
} }
if (len == Reader.SIGN_NOLENGTH) { if (len == Reader.SIGN_NOLENGTH) {
int size = 0; int size = 0;
char[] data = new char[8]; char[] data = new char[8];
int startPosition = in.position(); int startPosition = in.position();
while (in.hasNext(startPosition, contentLength)) { while (in.hasNext(null, startPosition, contentLength)) {
if (size >= data.length) { if (size >= data.length) {
char[] newdata = new char[data.length + 4]; char[] newdata = new char[data.length + 4];
System.arraycopy(data, 0, newdata, 0, size); System.arraycopy(data, 0, newdata, 0, size);

View File

@@ -47,14 +47,14 @@ public final class DoubleArraySimpledCoder<R extends Reader, W extends Writer> e
int contentLength = -1; int contentLength = -1;
if (len == Reader.SIGN_NULL) return null; if (len == Reader.SIGN_NULL) return null;
if (len == Reader.SIGN_NOLENBUTBYTES) { if (len == Reader.SIGN_NOLENBUTBYTES) {
contentLength = in.readMemberContentLength(null); contentLength = in.readMemberContentLength(null, DoubleSimpledCoder.instance);
len = Reader.SIGN_NOLENGTH; len = Reader.SIGN_NOLENGTH;
} }
if (len == Reader.SIGN_NOLENGTH) { if (len == Reader.SIGN_NOLENGTH) {
int size = 0; int size = 0;
double[] data = new double[8]; double[] data = new double[8];
int startPosition = in.position(); int startPosition = in.position();
while (in.hasNext(startPosition, contentLength)) { while (in.hasNext(null, startPosition, contentLength)) {
if (size >= data.length) { if (size >= data.length) {
double[] newdata = new double[data.length + 4]; double[] newdata = new double[data.length + 4];
System.arraycopy(data, 0, newdata, 0, size); System.arraycopy(data, 0, newdata, 0, size);

View File

@@ -46,14 +46,14 @@ public final class FloatArraySimpledCoder<R extends Reader, W extends Writer> ex
int contentLength = -1; int contentLength = -1;
if (len == Reader.SIGN_NULL) return null; if (len == Reader.SIGN_NULL) return null;
if (len == Reader.SIGN_NOLENBUTBYTES) { if (len == Reader.SIGN_NOLENBUTBYTES) {
contentLength = in.readMemberContentLength(null); contentLength = in.readMemberContentLength(null, FloatSimpledCoder.instance);
len = Reader.SIGN_NOLENGTH; len = Reader.SIGN_NOLENGTH;
} }
if (len == Reader.SIGN_NOLENGTH) { if (len == Reader.SIGN_NOLENGTH) {
int size = 0; int size = 0;
float[] data = new float[8]; float[] data = new float[8];
int startPosition = in.position(); int startPosition = in.position();
while (in.hasNext(startPosition, contentLength)) { while (in.hasNext(null, startPosition, contentLength)) {
if (size >= data.length) { if (size >= data.length) {
float[] newdata = new float[data.length + 4]; float[] newdata = new float[data.length + 4];
System.arraycopy(data, 0, newdata, 0, size); System.arraycopy(data, 0, newdata, 0, size);

View File

@@ -47,14 +47,14 @@ public final class IntArraySimpledCoder<R extends Reader, W extends Writer> exte
int contentLength = -1; int contentLength = -1;
if (len == Reader.SIGN_NULL) return null; if (len == Reader.SIGN_NULL) return null;
if (len == Reader.SIGN_NOLENBUTBYTES) { if (len == Reader.SIGN_NOLENBUTBYTES) {
contentLength = in.readMemberContentLength(null); contentLength = in.readMemberContentLength(null, IntSimpledCoder.instance);
len = Reader.SIGN_NOLENGTH; len = Reader.SIGN_NOLENGTH;
} }
if (len == Reader.SIGN_NOLENGTH) { if (len == Reader.SIGN_NOLENGTH) {
int size = 0; int size = 0;
int[] data = new int[8]; int[] data = new int[8];
int startPosition = in.position(); int startPosition = in.position();
while (in.hasNext(startPosition, contentLength)) { while (in.hasNext(null, startPosition, contentLength)) {
if (size >= data.length) { if (size >= data.length) {
int[] newdata = new int[data.length + 4]; int[] newdata = new int[data.length + 4];
System.arraycopy(data, 0, newdata, 0, size); System.arraycopy(data, 0, newdata, 0, size);

View File

@@ -47,14 +47,14 @@ public final class LongArraySimpledCoder<R extends Reader, W extends Writer> ext
int contentLength = -1; int contentLength = -1;
if (len == Reader.SIGN_NULL) return null; if (len == Reader.SIGN_NULL) return null;
if (len == Reader.SIGN_NOLENBUTBYTES) { if (len == Reader.SIGN_NOLENBUTBYTES) {
contentLength = in.readMemberContentLength(null); contentLength = in.readMemberContentLength(null, LongSimpledCoder.instance);
len = Reader.SIGN_NOLENGTH; len = Reader.SIGN_NOLENGTH;
} }
if (len == Reader.SIGN_NOLENGTH) { if (len == Reader.SIGN_NOLENGTH) {
int size = 0; int size = 0;
long[] data = new long[8]; long[] data = new long[8];
int startPosition = in.position(); int startPosition = in.position();
while (in.hasNext(startPosition, contentLength)) { while (in.hasNext(null, startPosition, contentLength)) {
if (size >= data.length) { if (size >= data.length) {
long[] newdata = new long[data.length + 4]; long[] newdata = new long[data.length + 4];
System.arraycopy(data, 0, newdata, 0, size); System.arraycopy(data, 0, newdata, 0, size);

View File

@@ -46,14 +46,14 @@ public final class ShortArraySimpledCoder<R extends Reader, W extends Writer> ex
int contentLength = -1; int contentLength = -1;
if (len == Reader.SIGN_NULL) return null; if (len == Reader.SIGN_NULL) return null;
if (len == Reader.SIGN_NOLENBUTBYTES) { if (len == Reader.SIGN_NOLENBUTBYTES) {
contentLength = in.readMemberContentLength(null); contentLength = in.readMemberContentLength(null, ShortSimpledCoder.instance);
len = Reader.SIGN_NOLENGTH; len = Reader.SIGN_NOLENGTH;
} }
if (len == Reader.SIGN_NOLENGTH) { if (len == Reader.SIGN_NOLENGTH) {
int size = 0; int size = 0;
short[] data = new short[8]; short[] data = new short[8];
int startPosition = in.position(); int startPosition = in.position();
while (in.hasNext(startPosition, contentLength)) { while (in.hasNext(null, startPosition, contentLength)) {
if (size >= data.length) { if (size >= data.length) {
short[] newdata = new short[data.length + 4]; short[] newdata = new short[data.length + 4];
System.arraycopy(data, 0, newdata, 0, size); System.arraycopy(data, 0, newdata, 0, size);

View File

@@ -48,14 +48,14 @@ public final class StringArraySimpledCoder<R extends Reader, W extends Writer> e
int contentLength = -1; int contentLength = -1;
if (len == Reader.SIGN_NULL) return null; if (len == Reader.SIGN_NULL) return null;
if (len == Reader.SIGN_NOLENBUTBYTES) { if (len == Reader.SIGN_NOLENBUTBYTES) {
contentLength = in.readMemberContentLength(null); contentLength = in.readMemberContentLength(null, StringSimpledCoder.instance);
len = Reader.SIGN_NOLENGTH; len = Reader.SIGN_NOLENGTH;
} }
if (len == Reader.SIGN_NOLENGTH) { if (len == Reader.SIGN_NOLENGTH) {
int size = 0; int size = 0;
String[] data = new String[8]; String[] data = new String[8];
int startPosition = in.position(); int startPosition = in.position();
while (in.hasNext(startPosition, contentLength)) { while (in.hasNext(member, startPosition, contentLength)) {
if (size >= data.length) { if (size >= data.length) {
String[] newdata = new String[data.length + 4]; String[] newdata = new String[data.length + 4];
System.arraycopy(data, 0, newdata, 0, size); System.arraycopy(data, 0, newdata, 0, size);

View File

@@ -159,13 +159,14 @@ public class JsonByteBufferReader extends JsonReader {
/** /**
* 判断对象是否存在下一个属性或者数组是否存在下一个元素 * 判断对象是否存在下一个属性或者数组是否存在下一个元素
* *
* @param member DeMember
* @param startPosition 起始位置 * @param startPosition 起始位置
* @param contentLength 内容大小, 不确定的传-1 * @param contentLength 内容大小, 不确定的传-1
* *
* @return 是否存在 * @return 是否存在
*/ */
@Override @Override
public boolean hasNext(int startPosition, int contentLength) { public boolean hasNext(DeMember member, int startPosition, int contentLength) {
char ch = nextGoodChar(); char ch = nextGoodChar();
if (ch == ',') return true; if (ch == ',') return true;
if (ch == '}' || ch == ']' || ch == 0) return false; if (ch == '}' || ch == ']' || ch == 0) return false;

View File

@@ -256,7 +256,7 @@ public class JsonReader extends Reader {
} }
@Override @Override
public int readMemberContentLength(DeMember member) { public int readMemberContentLength(DeMember member, Decodeable decoder) {
return -1; return -1;
} }
@@ -269,7 +269,7 @@ public class JsonReader extends Reader {
* @return 是否存在 * @return 是否存在
*/ */
@Override @Override
public boolean hasNext(int startPosition, int contentLength) { public boolean hasNext(DeMember member, int startPosition, int contentLength) {
char ch = this.text[++this.position]; char ch = this.text[++this.position];
if (ch == ',') return true; if (ch == ',') return true;
if (ch == '}' || ch == ']') return false; if (ch == '}' || ch == ']') return false;
@@ -478,14 +478,14 @@ public class JsonReader extends Reader {
int contentLength = -1; int contentLength = -1;
if (len == Reader.SIGN_NULL) return null; if (len == Reader.SIGN_NULL) return null;
if (len == Reader.SIGN_NOLENBUTBYTES) { if (len == Reader.SIGN_NOLENBUTBYTES) {
contentLength = readMemberContentLength(null); contentLength = readMemberContentLength(null, null);
len = Reader.SIGN_NOLENGTH; len = Reader.SIGN_NOLENGTH;
} }
if (len == Reader.SIGN_NOLENGTH) { if (len == Reader.SIGN_NOLENGTH) {
int size = 0; int size = 0;
byte[] data = new byte[8]; byte[] data = new byte[8];
int startPosition = position(); int startPosition = position();
while (hasNext(startPosition, contentLength)) { while (hasNext(null, startPosition, contentLength)) {
if (size >= data.length) { if (size >= data.length) {
byte[] newdata = new byte[data.length + 4]; byte[] newdata = new byte[data.length + 4];
System.arraycopy(data, 0, newdata, 0, size); System.arraycopy(data, 0, newdata, 0, size);