This commit is contained in:
redkale
2024-09-28 17:23:33 +08:00
parent 7229fb90de
commit fd5b63f58c
9 changed files with 326 additions and 255 deletions

View File

@@ -21,7 +21,7 @@ class BsonStreamReader extends BsonByteBufferReader {
protected BsonStreamReader(InputStream in) { protected BsonStreamReader(InputStream in) {
super(); super();
this.in = in; this.in = in instanceof BufferedInputStream ? in : new BufferedInputStream(in);
} }
@Override @Override

View File

@@ -110,6 +110,7 @@ public class JsonByteBufferReader extends JsonReader {
* @param allowComment 是否容许含注释 * @param allowComment 是否容许含注释
* @return 有效字符 * @return 有效字符
*/ */
@Override
protected char nextGoodChar(boolean allowComment) { protected char nextGoodChar(boolean allowComment) {
char c; char c;
for (; ; ) { for (; ; ) {

View File

@@ -19,7 +19,7 @@ class JsonStreamReader extends JsonByteBufferReader {
protected JsonStreamReader(InputStream in) { protected JsonStreamReader(InputStream in) {
super(); super();
this.in = in; this.in = in instanceof BufferedInputStream ? in : new BufferedInputStream(in);
} }
@Override @Override

View File

@@ -6,33 +6,39 @@
package org.redkale.convert.pb; package org.redkale.convert.pb;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import org.redkale.convert.ConvertException;
import org.redkale.util.ByteArray;
/** @author zhangjx */ /** @author zhangjx */
public class ProtobufByteBufferReader extends ProtobufReader { public class ProtobufByteBufferReader extends ProtobufReader {
private ByteBuffer[] buffers; private ByteBuffer[] buffers;
private int currentIndex = 0;
private ByteBuffer currentBuffer; private ByteBuffer currentBuffer;
private int currentIndex = 0;
protected ProtobufByteBufferReader(ByteBuffer... buffers) { protected ProtobufByteBufferReader(ByteBuffer... buffers) {
this.buffers = buffers; this.buffers = buffers;
if (buffers != null && buffers.length > 0) this.currentBuffer = buffers[currentIndex]; this.currentBuffer = buffers[currentIndex];
} }
@Override @Override
protected boolean recycle() { protected boolean recycle() {
super.recycle(); // this.position 初始化值为-1 super.recycle();
this.currentIndex = 0; this.currentIndex = 0;
this.currentBuffer = null; this.currentBuffer = null;
this.buffers = null; this.buffers = null;
return false; return false;
} }
@Override protected int remaining() {
protected byte currentByte() { int count = 0;
return currentBuffer.get(currentBuffer.position()); for (int i = currentIndex; i < buffers.length; i++) {
count += buffers[i].remaining();
}
return count;
} }
protected byte nextByte() { protected byte nextByte() {
@@ -48,151 +54,172 @@ public class ProtobufByteBufferReader extends ProtobufReader {
} }
} }
} }
//
// //------------------------------------------------------------ protected byte[] nextBytes(int size) {
// /** byte[] bs = new byte[size];
// * 判断对象是否存在下一个属性或者数组是否存在下一个元素 if (this.currentBuffer.remaining() >= size) {
// * this.position += size;
// * @param startPosition 起始位置 this.currentBuffer.get(bs);
// * @param contentLength 内容大小, 不确定的传-1 } else {
// * for (int i = 0; i < bs.length; i++) {
// * @return 是否存在 bs[i] = nextByte();
// */ }
// @Override }
// public boolean hasNext(int startPosition, int contentLength) { return bs;
// //("-------------: " + startPosition + ", " + contentLength + ", " + this.position); }
// if (startPosition >= 0 && contentLength >= 0) {
// return (this.position) < (startPosition + contentLength); @Override
// } public byte[] remainBytes() {
// return (this.position + 1) < this.content.length; ByteArray array = new ByteArray();
// } if (currentBuffer.hasRemaining()) {
// array.put(currentBuffer);
// @Override }
// public byte[] readByteArray() { int end = buffers.length - 1;
// final int size = readRawVarint32(); while (this.currentIndex < end) {
// byte[] bs = new byte[size]; this.currentBuffer = this.buffers[++this.currentIndex];
// System.arraycopy(content, position + 1, bs, 0, size); array.put(currentBuffer);
// position += size; }
// return bs; return array.getBytes();
// } }
//
// protected int readRawVarint32() { //readUInt32 @Override
// fastpath: public boolean hasNext() {
// { if (currentBuffer.hasRemaining()) {
// int tempPos = this.position; return true;
// if ((tempPos + 1) == content.length) break fastpath; }
// int end = buffers.length - 1;
// int x; while (this.currentIndex < end) {
// if ((x = content[++tempPos]) >= 0) { this.currentBuffer = this.buffers[++this.currentIndex];
// this.position = tempPos; if (this.currentBuffer.hasRemaining()) {
// return x; return true;
// } else if (content.length - (tempPos + 1) < 9) { }
// break fastpath; }
// } else if ((x ^= (content[++tempPos] << 7)) < 0) { return false;
// x ^= (~0 << 7); }
// } else if ((x ^= (content[++tempPos] << 14)) >= 0) {
// x ^= (~0 << 7) ^ (~0 << 14); @Override
// } else if ((x ^= (content[++tempPos] << 21)) < 0) { public final boolean readBoolean() {
// x ^= (~0 << 7) ^ (~0 << 14) ^ (~0 << 21); return nextByte() != 0;
// } else { }
// int y = content[++tempPos];
// x ^= y << 28; @Override
// x ^= (~0 << 7) ^ (~0 << 14) ^ (~0 << 21) ^ (~0 << 28); public final String readString() {
// if (y < 0 final int size = readRawVarint32();
// && content[++tempPos] < 0 return new String(nextBytes(size), StandardCharsets.UTF_8);
// && content[++tempPos] < 0 }
// && content[++tempPos] < 0
// && content[++tempPos] < 0 @Override
// && content[++tempPos] < 0) { public final byte[] readByteArray() {
// break fastpath; // Will throw malformedVarint() final int size = readRawVarint32();
// } return nextBytes(size);
// } }
// this.position = tempPos;
// return x; protected final int readRawVarint32() { // readUInt32
// } fastpath:
// return (int) readRawVarint64SlowPath(); {
// } if (!hasNext()) {
// break fastpath;
// protected long readRawVarint64() { }
// fastpath: int x;
// { if ((x = nextByte()) >= 0) {
// int tempPos = this.position; return x;
// if ((tempPos + 1) == content.length) break fastpath; } else if (remaining() < 9) {
// break fastpath;
// long x; } else if ((x ^= (nextByte() << 7)) < 0) {
// int y; x ^= (~0 << 7);
// if ((y = content[++tempPos]) >= 0) { } else if ((x ^= (nextByte() << 14)) >= 0) {
// this.position = tempPos; x ^= (~0 << 7) ^ (~0 << 14);
// return y; } else if ((x ^= (nextByte() << 21)) < 0) {
// } else if (content.length - (tempPos + 1) < 9) { x ^= (~0 << 7) ^ (~0 << 14) ^ (~0 << 21);
// break fastpath; } else {
// } else if ((y ^= (content[++tempPos] << 7)) < 0) { int y = nextByte();
// x = y ^ (~0 << 7); x ^= y << 28;
// } else if ((y ^= (content[++tempPos] << 14)) >= 0) { x ^= (~0 << 7) ^ (~0 << 14) ^ (~0 << 21) ^ (~0 << 28);
// x = y ^ ((~0 << 7) ^ (~0 << 14)); if (y < 0 && nextByte() < 0 && nextByte() < 0 && nextByte() < 0 && nextByte() < 0 && nextByte() < 0) {
// } else if ((y ^= (content[++tempPos] << 21)) < 0) { break fastpath; // Will throw malformedVarint()
// x = y ^ ((~0 << 7) ^ (~0 << 14) ^ (~0 << 21)); }
// } else if ((x = y ^ ((long) content[++tempPos] << 28)) >= 0L) { }
// x ^= (~0L << 7) ^ (~0L << 14) ^ (~0L << 21) ^ (~0L << 28); return x;
// } else if ((x ^= ((long) content[++tempPos] << 35)) < 0L) { }
// x ^= (~0L << 7) ^ (~0L << 14) ^ (~0L << 21) ^ (~0L << 28) ^ (~0L << 35); return (int) readRawVarint64SlowPath();
// } else if ((x ^= ((long) content[++tempPos] << 42)) >= 0L) { }
// x ^= (~0L << 7) ^ (~0L << 14) ^ (~0L << 21) ^ (~0L << 28) ^ (~0L << 35) ^ (~0L << 42);
// } else if ((x ^= ((long) content[++tempPos] << 49)) < 0L) { protected final long readRawVarint64() {
// x ^= (~0L << 7) fastpath:
// ^ (~0L << 14) {
// ^ (~0L << 21) if (!hasNext()) {
// ^ (~0L << 28) break fastpath;
// ^ (~0L << 35) }
// ^ (~0L << 42) long x;
// ^ (~0L << 49); int y;
// } else { if ((y = nextByte()) >= 0) {
// x ^= ((long) content[++tempPos] << 56); return y;
// x ^= (~0L << 7) } else if (remaining() < 9) {
// ^ (~0L << 14) break fastpath;
// ^ (~0L << 21) } else if ((y ^= (nextByte() << 7)) < 0) {
// ^ (~0L << 28) x = y ^ (~0 << 7);
// ^ (~0L << 35) } else if ((y ^= (nextByte() << 14)) >= 0) {
// ^ (~0L << 42) x = y ^ ((~0 << 7) ^ (~0 << 14));
// ^ (~0L << 49) } else if ((y ^= (nextByte() << 21)) < 0) {
// ^ (~0L << 56); x = y ^ ((~0 << 7) ^ (~0 << 14) ^ (~0 << 21));
// if (x < 0L) { } else if ((x = y ^ ((long) nextByte() << 28)) >= 0L) {
// if (content[++tempPos] < 0L) { x ^= (~0L << 7) ^ (~0L << 14) ^ (~0L << 21) ^ (~0L << 28);
// break fastpath; // Will throw malformedVarint() } else if ((x ^= ((long) nextByte() << 35)) < 0L) {
// } x ^= (~0L << 7) ^ (~0L << 14) ^ (~0L << 21) ^ (~0L << 28) ^ (~0L << 35);
// } } else if ((x ^= ((long) nextByte() << 42)) >= 0L) {
// } x ^= (~0L << 7) ^ (~0L << 14) ^ (~0L << 21) ^ (~0L << 28) ^ (~0L << 35) ^ (~0L << 42);
// this.position = tempPos; } else if ((x ^= ((long) nextByte() << 49)) < 0L) {
// return x; x ^= (~0L << 7) ^ (~0L << 14) ^ (~0L << 21) ^ (~0L << 28) ^ (~0L << 35) ^ (~0L << 42) ^ (~0L << 49);
// } } else {
// return readRawVarint64SlowPath(); x ^= ((long) nextByte() << 56);
// } x ^= (~0L << 7)
// ^ (~0L << 14)
// protected long readRawVarint64SlowPath() { ^ (~0L << 21)
// long result = 0; ^ (~0L << 28)
// for (int shift = 0; shift < 64; shift += 7) { ^ (~0L << 35)
// final byte b = content[++this.position]; ^ (~0L << 42)
// result |= (long) (b & 0x7F) << shift; ^ (~0L << 49)
// if ((b & 0x80) == 0) return result; ^ (~0L << 56);
// } if (x < 0L) {
// throw new ConvertException("readRawVarint64SlowPath error"); if (nextByte() < 0L) {
// } break fastpath; // Will throw malformedVarint()
// }
// protected int readRawLittleEndian32() { }
// return ((content[++this.position] & 0xff) }
// | ((content[++this.position] & 0xff) << 8) return x;
// | ((content[++this.position] & 0xff) << 16) }
// | ((content[++this.position] & 0xff) << 24)); return readRawVarint64SlowPath();
// } }
//
// protected long readRawLittleEndian64() { protected final long readRawVarint64SlowPath() {
// return ((content[++this.position] & 0xffL) long result = 0;
// | ((content[++this.position] & 0xffL) << 8) for (int shift = 0; shift < 64; shift += 7) {
// | ((content[++this.position] & 0xffL) << 16) final byte b = nextByte();
// | ((content[++this.position] & 0xffL) << 24) result |= (long) (b & 0x7F) << shift;
// | ((content[++this.position] & 0xffL) << 32) if ((b & 0x80) == 0) {
// | ((content[++this.position] & 0xffL) << 40) return result;
// | ((content[++this.position] & 0xffL) << 48) }
// | ((content[++this.position] & 0xffL) << 56)); }
// } throw new ConvertException("readRawVarint64SlowPath error");
}
@Override
protected final int readRawLittleEndian32() {
return ((nextByte() & 0xff)
| ((nextByte() & 0xff) << 8)
| ((nextByte() & 0xff) << 16)
| ((nextByte() & 0xff) << 24));
}
@Override
protected final long readRawLittleEndian64() {
return ((nextByte() & 0xffL)
| ((nextByte() & 0xffL) << 8)
| ((nextByte() & 0xffL) << 16)
| ((nextByte() & 0xffL) << 24)
| ((nextByte() & 0xffL) << 32)
| ((nextByte() & 0xffL) << 40)
| ((nextByte() & 0xffL) << 48)
| ((nextByte() & 0xffL) << 56));
}
} }

View File

@@ -614,9 +614,6 @@ public class ProtobufConvert extends BinaryConvert<ProtobufReader, ProtobufWrite
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> T convertFrom(final Type type, final InputStream in) { public <T> T convertFrom(final Type type, final InputStream in) {
if (true) {
throw new ConvertException(this.getClass().getSimpleName() + " not supported convertFrom InputStream");
}
if (type == null || in == null) { if (type == null || in == null) {
return null; return null;
} }
@@ -634,9 +631,6 @@ public class ProtobufConvert extends BinaryConvert<ProtobufReader, ProtobufWrite
@Override @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> T convertFrom(final Type type, final ByteBuffer... buffers) { public <T> T convertFrom(final Type type, final ByteBuffer... buffers) {
if (true) {
throw new ConvertException(this.getClass().getSimpleName() + " not supported convertFrom ByteBuffer");
}
if (type == null || Utility.isEmpty(buffers)) { if (type == null || Utility.isEmpty(buffers)) {
return null; return null;
} }
@@ -782,9 +776,6 @@ public class ProtobufConvert extends BinaryConvert<ProtobufReader, ProtobufWrite
} }
public void convertTo(final OutputStream out, final Type type, final Object value) { public void convertTo(final OutputStream out, final Type type, final Object value) {
if (true) {
throw new ConvertException(this.getClass().getSimpleName() + " not supported convertTo OutputStream");
}
ProtobufWriter writer = pollProtobufWriter(out); ProtobufWriter writer = pollProtobufWriter(out);
if (value == null) { if (value == null) {
writer.writeNull(); writer.writeNull();
@@ -807,8 +798,6 @@ public class ProtobufConvert extends BinaryConvert<ProtobufReader, ProtobufWrite
@Override @Override
public ByteBuffer[] convertTo(final Supplier<ByteBuffer> supplier, final Type type, final Object value) { public ByteBuffer[] convertTo(final Supplier<ByteBuffer> supplier, final Type type, final Object value) {
// if (true) throw new ConvertException(this.getClass().getSimpleName() + " not supported convertTo
// ByteBuffer");
Objects.requireNonNull(supplier); Objects.requireNonNull(supplier);
ProtobufByteBufferWriter writer = pollProtobufWriter(supplier); ProtobufByteBufferWriter writer = pollProtobufWriter(supplier);
if (value == null) { if (value == null) {

View File

@@ -88,14 +88,6 @@ public class ProtobufReader extends Reader {
return this; return this;
} }
// 通常用于尾部解析
public byte[] remainBytes() {
if (this.position >= this.limit) {
return new byte[0];
}
return Arrays.copyOfRange(this.content, this.position + 1, this.limit);
}
/** 跳过属性的值 */ /** 跳过属性的值 */
@Override @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@@ -124,7 +116,7 @@ public class ProtobufReader extends Reader {
@Override @Override
public final String readObjectB(final Class clazz) { public final String readObjectB(final Class clazz) {
return (this.position + 1) < this.limit ? "" : null; return hasNext() ? "" : null;
} }
@Override @Override
@@ -183,11 +175,11 @@ public class ProtobufReader extends Reader {
// ------------------------------------------------------------ // ------------------------------------------------------------
@Override @Override
public final boolean readBoolean() { public boolean readBoolean() {
return content[++this.position] != 0; return content[++this.position] != 0;
} }
public boolean[] readBools() { public final boolean[] readBools() {
int size = readRawVarint32(); int size = readRawVarint32();
boolean[] data = new boolean[size]; boolean[] data = new boolean[size];
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
@@ -196,7 +188,7 @@ public class ProtobufReader extends Reader {
return data; return data;
} }
public Collection<Boolean> readBools(Creator<? extends Collection> creator) { public final Collection<Boolean> readBools(Creator<? extends Collection> creator) {
int size = readRawVarint32(); int size = readRawVarint32();
Collection<Boolean> data = creator.create(); Collection<Boolean> data = creator.create();
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
@@ -210,11 +202,11 @@ public class ProtobufReader extends Reader {
return (byte) readInt(); return (byte) readInt();
} }
public byte[] readBytes() { public final byte[] readBytes() {
return readByteArray(); return readByteArray();
} }
public Collection<Byte> readBytes(Creator<? extends Collection> creator) { public final Collection<Byte> readBytes(Creator<? extends Collection> creator) {
Collection<Byte> data = creator.create(); Collection<Byte> data = creator.create();
for (byte b : readByteArray()) { for (byte b : readByteArray()) {
data.add(b); data.add(b);
@@ -227,7 +219,7 @@ public class ProtobufReader extends Reader {
return (char) readInt(); return (char) readInt();
} }
public char[] readChars() { public final char[] readChars() {
int len = readRawVarint32(); int len = readRawVarint32();
List<Integer> list = new ArrayList<>(len); List<Integer> list = new ArrayList<>(len);
while (len > 0) { while (len > 0) {
@@ -242,7 +234,7 @@ public class ProtobufReader extends Reader {
return rs; return rs;
} }
public Collection<Character> readChars(Creator<? extends Collection> creator) { public final Collection<Character> readChars(Creator<? extends Collection> creator) {
Collection<Character> data = creator.create(); Collection<Character> data = creator.create();
int len = readRawVarint32(); int len = readRawVarint32();
while (len > 0) { while (len > 0) {
@@ -258,7 +250,7 @@ public class ProtobufReader extends Reader {
return (short) readInt(); return (short) readInt();
} }
public short[] readShorts() { public final short[] readShorts() {
int len = readRawVarint32(); int len = readRawVarint32();
List<Short> list = new ArrayList<>(len); List<Short> list = new ArrayList<>(len);
while (len > 0) { while (len > 0) {
@@ -273,7 +265,7 @@ public class ProtobufReader extends Reader {
return rs; return rs;
} }
public Collection<Short> readShorts(Creator<? extends Collection> creator) { public final Collection<Short> readShorts(Creator<? extends Collection> creator) {
Collection<Short> data = creator.create(); Collection<Short> data = creator.create();
int len = readRawVarint32(); int len = readRawVarint32();
while (len > 0) { while (len > 0) {
@@ -290,7 +282,7 @@ public class ProtobufReader extends Reader {
return (n >>> 1) ^ -(n & 1); return (n >>> 1) ^ -(n & 1);
} }
public int[] readInts() { public final int[] readInts() {
int len = readRawVarint32(); int len = readRawVarint32();
List<Integer> list = new ArrayList<>(len); List<Integer> list = new ArrayList<>(len);
while (len > 0) { while (len > 0) {
@@ -305,7 +297,7 @@ public class ProtobufReader extends Reader {
return rs; return rs;
} }
public Collection<Integer> readInts(Creator<? extends Collection> creator) { public final Collection<Integer> readInts(Creator<? extends Collection> creator) {
Collection<Integer> data = creator.create(); Collection<Integer> data = creator.create();
int len = readRawVarint32(); int len = readRawVarint32();
while (len > 0) { while (len > 0) {
@@ -316,12 +308,12 @@ public class ProtobufReader extends Reader {
return data; return data;
} }
public AtomicInteger[] readAtomicIntegers() { public final AtomicInteger[] readAtomicIntegers() {
Collection<AtomicInteger> data = readAtomicIntegers(LIS_CREATOR); Collection<AtomicInteger> data = readAtomicIntegers(LIS_CREATOR);
return data.toArray(new AtomicInteger[data.size()]); return data.toArray(new AtomicInteger[data.size()]);
} }
public Collection<AtomicInteger> readAtomicIntegers(Creator<? extends Collection> creator) { public final Collection<AtomicInteger> readAtomicIntegers(Creator<? extends Collection> creator) {
Collection<AtomicInteger> data = creator.create(); Collection<AtomicInteger> data = creator.create();
int len = readRawVarint32(); int len = readRawVarint32();
while (len > 0) { while (len > 0) {
@@ -337,7 +329,7 @@ public class ProtobufReader extends Reader {
return Float.intBitsToFloat(readRawLittleEndian32()); return Float.intBitsToFloat(readRawLittleEndian32());
} }
public float[] readFloats() { public final float[] readFloats() {
int len = readRawVarint32(); int len = readRawVarint32();
float[] rs = new float[len / 4]; float[] rs = new float[len / 4];
for (int i = 0; i < rs.length; i++) { for (int i = 0; i < rs.length; i++) {
@@ -346,7 +338,7 @@ public class ProtobufReader extends Reader {
return rs; return rs;
} }
public Collection<Float> readFloats(Creator<? extends Collection> creator) { public final Collection<Float> readFloats(Creator<? extends Collection> creator) {
Collection<Float> data = creator.create(); Collection<Float> data = creator.create();
int len = readRawVarint32() / 4; int len = readRawVarint32() / 4;
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
@@ -361,7 +353,7 @@ public class ProtobufReader extends Reader {
return (n >>> 1) ^ -(n & 1); return (n >>> 1) ^ -(n & 1);
} }
public long[] readLongs() { public final long[] readLongs() {
int len = readRawVarint32(); int len = readRawVarint32();
List<Long> list = new ArrayList<>(len); List<Long> list = new ArrayList<>(len);
while (len > 0) { while (len > 0) {
@@ -376,7 +368,7 @@ public class ProtobufReader extends Reader {
return rs; return rs;
} }
public Collection<Long> readLongs(Creator<? extends Collection> creator) { public final Collection<Long> readLongs(Creator<? extends Collection> creator) {
Collection<Long> data = creator.create(); Collection<Long> data = creator.create();
int len = readRawVarint32(); int len = readRawVarint32();
while (len > 0) { while (len > 0) {
@@ -387,12 +379,12 @@ public class ProtobufReader extends Reader {
return data; return data;
} }
public AtomicLong[] readAtomicLongs() { public final AtomicLong[] readAtomicLongs() {
Collection<AtomicLong> data = readAtomicLongs(LIS_CREATOR); Collection<AtomicLong> data = readAtomicLongs(LIS_CREATOR);
return data.toArray(new AtomicLong[data.size()]); return data.toArray(new AtomicLong[data.size()]);
} }
public Collection<AtomicLong> readAtomicLongs(Creator<? extends Collection> creator) { public final Collection<AtomicLong> readAtomicLongs(Creator<? extends Collection> creator) {
Collection<AtomicLong> data = creator.create(); Collection<AtomicLong> data = creator.create();
int len = readRawVarint32(); int len = readRawVarint32();
while (len > 0) { while (len > 0) {
@@ -403,12 +395,12 @@ public class ProtobufReader extends Reader {
return data; return data;
} }
public String[] readStrings(int tag) { public final String[] readStrings(int tag) {
Collection<String> data = readStrings(tag, LIS_CREATOR); Collection<String> data = readStrings(tag, LIS_CREATOR);
return data.toArray(new String[data.size()]); return data.toArray(new String[data.size()]);
} }
public Collection<String> readStrings(int tag, Creator<? extends Collection> creator) { public final Collection<String> readStrings(int tag, Creator<? extends Collection> creator) {
Collection<String> data = creator.create(); Collection<String> data = creator.create();
while (true) { while (true) {
data.add(readString()); data.add(readString());
@@ -424,7 +416,7 @@ public class ProtobufReader extends Reader {
return Double.longBitsToDouble(readRawLittleEndian64()); return Double.longBitsToDouble(readRawLittleEndian64());
} }
public double[] readDoubles() { public final double[] readDoubles() {
int len = readRawVarint32(); int len = readRawVarint32();
double[] rs = new double[len / 8]; double[] rs = new double[len / 8];
for (int i = 0; i < rs.length; i++) { for (int i = 0; i < rs.length; i++) {
@@ -433,7 +425,7 @@ public class ProtobufReader extends Reader {
return rs; return rs;
} }
public Collection<Double> readDoubles(Creator<? extends Collection> creator) { public final Collection<Double> readDoubles(Creator<? extends Collection> creator) {
Collection<Double> data = creator.create(); Collection<Double> data = creator.create();
int len = readRawVarint32() / 8; int len = readRawVarint32() / 8;
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
@@ -453,18 +445,18 @@ public class ProtobufReader extends Reader {
} }
@Override @Override
public final String readString() { public String readString() {
final int size = readRawVarint32(); final int size = readRawVarint32();
String val = new String(content, position + 1, size, StandardCharsets.UTF_8); String val = new String(content, position + 1, size, StandardCharsets.UTF_8);
position += size; position += size;
return val; return val;
} }
public boolean readNextTag(DeMember member) { public final boolean readNextTag(DeMember member) {
return readNextTag(member.getTag()); return readNextTag(member.getTag());
} }
public boolean readNextTag(int memberTag) { public final boolean readNextTag(int memberTag) {
if (!hasNext()) { if (!hasNext()) {
return false; return false;
} }
@@ -489,8 +481,9 @@ public class ProtobufReader extends Reader {
this.cacheTag = tag; this.cacheTag = tag;
} }
protected byte currentByte() { @Override
return this.content[this.position]; public final ValueType readType() {
throw new UnsupportedOperationException("Not supported yet.");
} }
@Override @Override
@@ -507,6 +500,14 @@ public class ProtobufReader extends Reader {
return bs; return bs;
} }
// 通常用于尾部解析
public byte[] remainBytes() {
if (this.position >= this.limit) {
return new byte[0];
}
return Arrays.copyOfRange(this.content, this.position + 1, this.limit);
}
protected int readRawVarint32() { // readUInt32 protected int readRawVarint32() { // readUInt32
byte[] data = content; byte[] data = content;
fastpath: fastpath:
@@ -631,9 +632,4 @@ public class ProtobufReader extends Reader {
| ((content[++this.position] & 0xffL) << 48) | ((content[++this.position] & 0xffL) << 48)
| ((content[++this.position] & 0xffL) << 56)); | ((content[++this.position] & 0xffL) << 56));
} }
@Override
public ValueType readType() {
throw new UnsupportedOperationException("Not supported yet.");
}
} }

View File

@@ -7,6 +7,7 @@ package org.redkale.convert.pb;
import java.io.*; import java.io.*;
import org.redkale.convert.*; import org.redkale.convert.*;
import org.redkale.util.ByteArray;
/** /**
* 详情见: https://redkale.org * 详情见: https://redkale.org
@@ -17,34 +18,80 @@ class ProtobufStreamReader extends ProtobufByteBufferReader {
private InputStream in; private InputStream in;
private byte currByte; private Byte backByte;
protected ProtobufStreamReader(InputStream in) { protected ProtobufStreamReader(InputStream in) {
super(); super();
this.in = in; this.in = in instanceof BufferedInputStream ? in : new BufferedInputStream(in);
} }
@Override @Override
protected boolean recycle() { protected boolean recycle() {
super.recycle(); // this.position 初始化值为-1 super.recycle(); // this.position 初始化值为-1
this.in = null; this.in = null;
this.currByte = 0; this.backByte = null;
return false; return false;
} }
@Override @Override
public byte nextByte() { protected int remaining() {
try { int count = 0;
byte b = (currByte = (byte) in.read()); return count;
this.position++; }
@Override
protected byte nextByte() {
if (backByte != null) {
byte b = backByte;
backByte = null;
return b; return b;
}
try {
int v = in.read();
if (v == -1) {
throw new ConvertException("eof");
}
this.position++;
return (byte) v;
} catch (IOException e) { } catch (IOException e) {
throw new ConvertException(e); throw new ConvertException(e);
} }
} }
@Override @Override
protected byte currentByte() { protected byte[] nextBytes(int size) {
return currByte; byte[] bs = new byte[size];
for (int i = 0; i < bs.length; i++) {
bs[i] = nextByte();
}
return bs;
}
@Override
public byte[] remainBytes() {
ByteArray array = new ByteArray();
try {
int v;
while ((v = in.read()) != -1) {
array.putByte(v);
}
} catch (IOException e) {
throw new ConvertException(e);
}
return array.getBytes();
}
@Override
public boolean hasNext() {
try {
int v = in.read();
if (v == -1) {
return false;
}
backByte = (byte) v;
return true;
} catch (IOException e) {
return false;
}
} }
} }

View File

@@ -1354,12 +1354,16 @@ public class ProtobufWriter extends Writer implements ByteTuple {
writeTo(TENTHOUSAND_UINT_BYTES2[-value]); writeTo(TENTHOUSAND_UINT_BYTES2[-value]);
return; return;
} }
expand(6);
int curr = this.count;
byte[] data = this.content;
while (true) { while (true) {
if ((value & ~0x7F) == 0) { if ((value & ~0x7F) == 0) {
writeTo((byte) value); data[curr++] = (byte) value;
this.count = curr;
return; return;
} else { } else {
writeTo((byte) ((value & 0x7F) | 0x80)); data[curr++] = (byte) ((value & 0x7F) | 0x80);
value >>>= 7; value >>>= 7;
} }
} }
@@ -1373,12 +1377,16 @@ public class ProtobufWriter extends Writer implements ByteTuple {
writeTo(TENTHOUSAND_UINT_BYTES2[(int) -value]); writeTo(TENTHOUSAND_UINT_BYTES2[(int) -value]);
return; return;
} }
expand(12);
int curr = this.count;
byte[] data = this.content;
while (true) { while (true) {
if ((value & ~0x7FL) == 0) { if ((value & ~0x7FL) == 0) {
writeTo((byte) value); data[curr++] = (byte) value;
this.count = curr;
return; return;
} else { } else {
writeTo((byte) (((int) value & 0x7F) | 0x80)); data[curr++] = (byte) (((int) value & 0x7F) | 0x80);
value >>>= 7; value >>>= 7;
} }
} }
@@ -1392,8 +1400,11 @@ public class ProtobufWriter extends Writer implements ByteTuple {
writeTo(TENTHOUSAND_FIXED32_BYTES2[-value]); writeTo(TENTHOUSAND_FIXED32_BYTES2[-value]);
return; return;
} }
writeTo((byte) (value & 0xFF), (byte) ((value >> 8) & 0xFF), (byte) ((value >> 16) & 0xFF), (byte) writeTo(
((value >> 24) & 0xFF)); (byte) (value & 0xFF), // 0
(byte) ((value >> 8) & 0xFF), // 1
(byte) ((value >> 16) & 0xFF), // 2
(byte) ((value >> 24) & 0xFF)); // 3
} }
protected void writeFixed64(long value) { protected void writeFixed64(long value) {

View File

@@ -54,17 +54,6 @@ public class GenericEntityTest {
@Test @Test
public void runJson2() throws Exception { public void runJson2() throws Exception {
JsonConvert convert = JsonConvert.root();
InputStream in = ConvertHelper.createInputStream(createBytes());
GenericEntity<Long, String, SimpleEntity> bean = convert.convertFrom(ENTITY_TYPE, in);
Assertions.assertEquals(JSON, bean.toString());
ByteArrayOutputStream out = new ByteArrayOutputStream();
convert.convertTo(out, ENTITY_TYPE, bean);
Assertions.assertArrayEquals(createBytes(), out.toByteArray());
}
@Test
public void runJson3() throws Exception {
JsonConvert convert = JsonConvert.root(); JsonConvert convert = JsonConvert.root();
ByteBuffer in = ConvertHelper.createByteBuffer(createBytes()); ByteBuffer in = ConvertHelper.createByteBuffer(createBytes());
GenericEntity<Long, String, SimpleEntity> bean = convert.convertFrom(ENTITY_TYPE, in); GenericEntity<Long, String, SimpleEntity> bean = convert.convertFrom(ENTITY_TYPE, in);
@@ -74,6 +63,17 @@ public class GenericEntityTest {
Assertions.assertArrayEquals(createBytes(), ConvertHelper.toBytes(buffers)); Assertions.assertArrayEquals(createBytes(), ConvertHelper.toBytes(buffers));
} }
@Test
public void runJson3() throws Exception {
JsonConvert convert = JsonConvert.root();
InputStream in = ConvertHelper.createInputStream(createBytes());
GenericEntity<Long, String, SimpleEntity> bean = convert.convertFrom(ENTITY_TYPE, in);
Assertions.assertEquals(JSON, bean.toString());
ByteArrayOutputStream out = new ByteArrayOutputStream();
convert.convertTo(out, ENTITY_TYPE, bean);
Assertions.assertArrayEquals(createBytes(), out.toByteArray());
}
@Test @Test
public void runPb1() throws Exception { public void runPb1() throws Exception {
ProtobufConvert convert = ProtobufConvert.root(); ProtobufConvert convert = ProtobufConvert.root();
@@ -90,12 +90,12 @@ public class GenericEntityTest {
ProtobufConvert convert = ProtobufConvert.root(); ProtobufConvert convert = ProtobufConvert.root();
GenericEntity<Long, String, SimpleEntity> bean = createBean(); GenericEntity<Long, String, SimpleEntity> bean = createBean();
byte[] bs = convert.convertTo(bean); byte[] bs = convert.convertTo(bean);
// InputStream in = ConvertHelper.createInputStream(bs); ByteBuffer in = ConvertHelper.createByteBuffer(bs);
// GenericEntity<Long, String, SimpleEntity> rs = convert.convertFrom(ENTITY_TYPE, in); // GenericEntity<Long, String, SimpleEntity> rs = convert.convertFrom(ENTITY_TYPE, in);
// Assertions.assertEquals(JSON, rs.toString()); // Assertions.assertEquals(JSON, rs.toString());
// ByteArrayOutputStream out = new ByteArrayOutputStream(); // Supplier<ByteBuffer> out = ConvertHelper.createSupplier();
// convert.convertTo(out, ENTITY_TYPE, rs); // ByteBuffer[] buffers = convert.convertTo(out, ENTITY_TYPE, rs);
// Assertions.assertArrayEquals(bs, out.toByteArray()); // Assertions.assertArrayEquals(bs, ConvertHelper.toBytes(buffers));
} }
@Test @Test
@@ -103,12 +103,12 @@ public class GenericEntityTest {
ProtobufConvert convert = ProtobufConvert.root(); ProtobufConvert convert = ProtobufConvert.root();
GenericEntity<Long, String, SimpleEntity> bean = createBean(); GenericEntity<Long, String, SimpleEntity> bean = createBean();
byte[] bs = convert.convertTo(bean); byte[] bs = convert.convertTo(bean);
// ByteBuffer in = ConvertHelper.createByteBuffer(bs); InputStream in = ConvertHelper.createInputStream(bs);
// GenericEntity<Long, String, SimpleEntity> rs = convert.convertFrom(ENTITY_TYPE, in); // GenericEntity<Long, String, SimpleEntity> rs = convert.convertFrom(ENTITY_TYPE, in);
// Assertions.assertEquals(JSON, rs.toString()); // Assertions.assertEquals(JSON, rs.toString());
// Supplier<ByteBuffer> out = ConvertHelper.createSupplier(); // ByteArrayOutputStream out = new ByteArrayOutputStream();
// ByteBuffer[] buffers = convert.convertTo(out, ENTITY_TYPE, rs); // convert.convertTo(out, ENTITY_TYPE, rs);
// Assertions.assertArrayEquals(bs, ConvertHelper.toBytes(buffers)); // Assertions.assertArrayEquals(bs, out.toByteArray());
} }
@Test @Test
@@ -127,12 +127,12 @@ public class GenericEntityTest {
BsonConvert convert = BsonConvert.root(); BsonConvert convert = BsonConvert.root();
GenericEntity<Long, String, SimpleEntity> bean = createBean(); GenericEntity<Long, String, SimpleEntity> bean = createBean();
byte[] bs = convert.convertTo(bean); byte[] bs = convert.convertTo(bean);
// InputStream in = ConvertHelper.createInputStream(bs); // ByteBuffer in = ConvertHelper.createByteBuffer(bs);
// GenericEntity<Long, String, SimpleEntity> rs = convert.convertFrom(ENTITY_TYPE, in); // GenericEntity<Long, String, SimpleEntity> rs = convert.convertFrom(ENTITY_TYPE, in);
// Assertions.assertEquals(JSON, rs.toString()); // Assertions.assertEquals(JSON, rs.toString());
// ByteArrayOutputStream out = new ByteArrayOutputStream(); // Supplier<ByteBuffer> out = ConvertHelper.createSupplier();
// convert.convertTo(out, ENTITY_TYPE, rs); // ByteBuffer[] buffers = convert.convertTo(out, ENTITY_TYPE, rs);
// Assertions.assertArrayEquals(bs, out.toByteArray()); // Assertions.assertArrayEquals(bs, ConvertHelper.toBytes(buffers));
} }
@Test @Test
@@ -140,12 +140,12 @@ public class GenericEntityTest {
BsonConvert convert = BsonConvert.root(); BsonConvert convert = BsonConvert.root();
GenericEntity<Long, String, SimpleEntity> bean = createBean(); GenericEntity<Long, String, SimpleEntity> bean = createBean();
byte[] bs = convert.convertTo(bean); byte[] bs = convert.convertTo(bean);
// ByteBuffer in = ConvertHelper.createByteBuffer(bs); // InputStream in = ConvertHelper.createInputStream(bs);
// GenericEntity<Long, String, SimpleEntity> rs = convert.convertFrom(ENTITY_TYPE, in); // GenericEntity<Long, String, SimpleEntity> rs = convert.convertFrom(ENTITY_TYPE, in);
// Assertions.assertEquals(JSON, rs.toString()); // Assertions.assertEquals(JSON, rs.toString());
// Supplier<ByteBuffer> out = ConvertHelper.createSupplier(); // ByteArrayOutputStream out = new ByteArrayOutputStream();
// ByteBuffer[] buffers = convert.convertTo(out, ENTITY_TYPE, rs); // convert.convertTo(out, ENTITY_TYPE, rs);
// Assertions.assertArrayEquals(bs, ConvertHelper.toBytes(buffers)); // Assertions.assertArrayEquals(bs, out.toByteArray());
} }
private byte[] createBytes() { private byte[] createBytes() {