This commit is contained in:
Redkale
2018-07-26 10:53:35 +08:00
parent 8718bca6e8
commit 0db1c4413c
17 changed files with 33 additions and 22 deletions

View File

@@ -89,8 +89,9 @@ 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(member, startPosition, contentLength)) { while (in.hasNext(this, member, startPosition, contentLength)) {
result.add(readMemberValue(in, member, first)); Reader itemReader = getArrayItemReader(in, member, first);
result.add(readMemberValue(itemReader, member, first));
first = false; first = false;
} }
} else { } else {
@@ -103,6 +104,10 @@ public class ArrayDecoder<T> implements Decodeable<Reader, T[]> {
return result.toArray(rs); return result.toArray(rs);
} }
protected Reader getArrayItemReader(Reader in, DeMember member, boolean first) {
return in;
}
protected T readMemberValue(Reader in, DeMember member, boolean first) { protected T readMemberValue(Reader in, DeMember member, boolean first) {
return this.decoder.convertFrom(in); return this.decoder.convertFrom(in);
} }

View File

@@ -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(member, startPosition, contentLength)) { while (in.hasNext(this, member, startPosition, contentLength)) {
result.add(readMemberValue(in, member, first)); result.add(readMemberValue(in, member, first));
first = false; first = false;
} }

View File

@@ -102,7 +102,7 @@ public class MapDecoder<K, V> implements Decodeable<Reader, Map<K, V>> {
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(member, startPosition, contentLength)) { while (in.hasNext(this, 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

@@ -22,19 +22,20 @@ public abstract class Reader {
public static final short SIGN_NOLENGTH = -2; public static final short SIGN_NOLENGTH = -2;
public static final short SIGN_NOLENBUTBYTES = -3; public static final short SIGN_NOLENBUTBYTES = -3; //目前只适合于protobuf的boolean[]...double[]类型
/** /**
* 是否还存在下个元素或字段 <br> * 是否还存在下个元素或字段 <br>
* 注意: 主要用于Array、Collection、Stream或Map等集合对象 * 注意: 主要用于Array、Collection、Stream或Map等集合对象
* *
* @param self Decodeable
* @param member DeMember * @param member DeMember
* @param startPosition 起始位置 * @param startPosition 起始位置
* @param contentLength 内容大小, 不确定的传-1 * @param contentLength 内容大小, 不确定的传-1
* *
* @return 是否还存在下个元素或字段 * @return 是否还存在下个元素或字段
*/ */
public abstract boolean hasNext(DeMember member, int startPosition, int contentLength); public abstract boolean hasNext(Decodeable self, DeMember member, int startPosition, int contentLength);
/** /**
* 是否还存在下个元素或字段 * 是否还存在下个元素或字段
@@ -43,7 +44,7 @@ public abstract class Reader {
* @return 是否还存在下个元素或字段 * @return 是否还存在下个元素或字段
*/ */
public boolean hasNext() { public boolean hasNext() {
return hasNext(null, -1, -1); return hasNext(null, null, -1, -1);
} }
/** /**

View File

@@ -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(member, startPosition, contentLength)) { while (in.hasNext(this, member, startPosition, contentLength)) {
result.add(readMemberValue(in, member, first)); result.add(readMemberValue(in, member, first));
first = false; first = false;
} }

View File

@@ -215,13 +215,15 @@ public class BsonReader extends Reader {
/** /**
* 判断对象是否存在下一个属性或者数组是否存在下一个元素 * 判断对象是否存在下一个属性或者数组是否存在下一个元素
* *
* @param self Decodeable
* @param member DeMember
* @param startPosition 起始位置 * @param startPosition 起始位置
* @param contentLength 内容大小, 不确定的传-1 * @param contentLength 内容大小, 不确定的传-1
* *
* @return 是否存在 * @return 是否存在
*/ */
@Override @Override
public boolean hasNext(DeMember member, int startPosition, int contentLength) { public boolean hasNext(Decodeable self, 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)
@@ -274,7 +276,7 @@ public class BsonReader extends Reader {
int size = 0; int size = 0;
byte[] data = new byte[8]; byte[] data = new byte[8];
int startPosition = position(); int startPosition = position();
while (hasNext(null, startPosition, contentLength)) { while (hasNext(null, 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

@@ -53,7 +53,7 @@ public final class BoolArraySimpledCoder<R extends Reader, W extends Writer> ext
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(null, startPosition, contentLength)) { while (in.hasNext(this, 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

@@ -54,7 +54,7 @@ public final class ByteBufferSimpledCoder<R extends Reader, W extends Writer> ex
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(null, startPosition, contentLength)) { while (in.hasNext(this, 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

@@ -53,7 +53,7 @@ public final class CharArraySimpledCoder<R extends Reader, W extends Writer> ext
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(null, startPosition, contentLength)) { while (in.hasNext(this, 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

@@ -54,7 +54,7 @@ public final class DoubleArraySimpledCoder<R extends Reader, W extends Writer> e
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(null, startPosition, contentLength)) { while (in.hasNext(this, 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

@@ -53,7 +53,7 @@ public final class FloatArraySimpledCoder<R extends Reader, W extends Writer> ex
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(null, startPosition, contentLength)) { while (in.hasNext(this, 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

@@ -54,7 +54,7 @@ public final class IntArraySimpledCoder<R extends Reader, W extends Writer> exte
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(null, startPosition, contentLength)) { while (in.hasNext(this, 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

@@ -54,7 +54,7 @@ public final class LongArraySimpledCoder<R extends Reader, W extends Writer> ext
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(null, startPosition, contentLength)) { while (in.hasNext(this, 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

@@ -53,7 +53,7 @@ public final class ShortArraySimpledCoder<R extends Reader, W extends Writer> ex
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(null, startPosition, contentLength)) { while (in.hasNext(this, 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

@@ -55,7 +55,7 @@ public final class StringArraySimpledCoder<R extends Reader, W extends Writer> e
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(member, startPosition, contentLength)) { while (in.hasNext(this, 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,6 +159,7 @@ public class JsonByteBufferReader extends JsonReader {
/** /**
* 判断对象是否存在下一个属性或者数组是否存在下一个元素 * 判断对象是否存在下一个属性或者数组是否存在下一个元素
* *
* @param self Decodeable
* @param member DeMember * @param member DeMember
* @param startPosition 起始位置 * @param startPosition 起始位置
* @param contentLength 内容大小, 不确定的传-1 * @param contentLength 内容大小, 不确定的传-1
@@ -166,7 +167,7 @@ public class JsonByteBufferReader extends JsonReader {
* @return 是否存在 * @return 是否存在
*/ */
@Override @Override
public boolean hasNext(DeMember member, int startPosition, int contentLength) { public boolean hasNext(Decodeable self, 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

@@ -263,13 +263,15 @@ public class JsonReader extends Reader {
/** /**
* 判断对象是否存在下一个属性或者数组是否存在下一个元素 * 判断对象是否存在下一个属性或者数组是否存在下一个元素
* *
* @param self Decodeable
* @param member DeMember
* @param startPosition 起始位置 * @param startPosition 起始位置
* @param contentLength 内容大小, 不确定的传-1 * @param contentLength 内容大小, 不确定的传-1
* *
* @return 是否存在 * @return 是否存在
*/ */
@Override @Override
public boolean hasNext(DeMember member, int startPosition, int contentLength) { public boolean hasNext(Decodeable self, 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;
@@ -485,7 +487,7 @@ public class JsonReader extends Reader {
int size = 0; int size = 0;
byte[] data = new byte[8]; byte[] data = new byte[8];
int startPosition = position(); int startPosition = position();
while (hasNext(null, startPosition, contentLength)) { while (hasNext(null, 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);