diff --git a/src/main/java/org/redkale/convert/bson/BsonByteBufferWriter.java b/src/main/java/org/redkale/convert/bson/BsonByteBufferWriter.java index 8895655a2..df966a65e 100644 --- a/src/main/java/org/redkale/convert/bson/BsonByteBufferWriter.java +++ b/src/main/java/org/redkale/convert/bson/BsonByteBufferWriter.java @@ -6,7 +6,9 @@ package org.redkale.convert.bson; import java.nio.ByteBuffer; +import java.util.Objects; import java.util.function.Supplier; +import org.redkale.util.ByteArray; import org.redkale.util.Utility; /** @@ -49,24 +51,20 @@ public class BsonByteBufferWriter extends BsonWriter { } @Override - public byte[] toArray() { - if (buffers == null) { - return new byte[0]; + public ByteArray toByteArray() { + ByteArray array = new ByteArray(); + if (buffers != null) { + for (ByteBuffer buf : toBuffers()) { + array.put(buf); + buf.flip(); + } } - int pos = 0; - byte[] bytes = new byte[this.count]; - for (ByteBuffer buf : toBuffers()) { - int r = buf.remaining(); - buf.get(bytes, pos, r); - buf.flip(); - pos += r; - } - return bytes; + return array; } @Override public String toString() { - return this.getClass().getSimpleName() + "[count=" + this.count + "]"; + return Objects.toString(this); } @Override @@ -138,17 +136,22 @@ public class BsonByteBufferWriter extends BsonWriter { } @Override - public byte[] content() { + public final byte[] toArray() { throw new UnsupportedOperationException("Not supported yet."); // 无需实现 } @Override - public int offset() { + public final byte[] content() { throw new UnsupportedOperationException("Not supported yet."); // 无需实现 } @Override - public int length() { + public final int offset() { + throw new UnsupportedOperationException("Not supported yet."); // 无需实现 + } + + @Override + public final int length() { throw new UnsupportedOperationException("Not supported yet."); // 无需实现 } } diff --git a/src/main/java/org/redkale/convert/bson/BsonConvert.java b/src/main/java/org/redkale/convert/bson/BsonConvert.java index ef8a68322..895ffb573 100644 --- a/src/main/java/org/redkale/convert/bson/BsonConvert.java +++ b/src/main/java/org/redkale/convert/bson/BsonConvert.java @@ -224,7 +224,7 @@ public class BsonConvert extends BinaryConvert { @Override public void convertToBytes(final Type type, final Object value, final ConvertBytesHandler handler) { final BsonWriter writer = pollWriter(); - if (type == null && type == null) { + if (type == null && value == null) { writer.writeNull(); } else { factory.loadEncoder(type).convertTo(writer, value); diff --git a/src/main/java/org/redkale/convert/bson/BsonWriter.java b/src/main/java/org/redkale/convert/bson/BsonWriter.java index 121d777b3..11b1e740e 100644 --- a/src/main/java/org/redkale/convert/bson/BsonWriter.java +++ b/src/main/java/org/redkale/convert/bson/BsonWriter.java @@ -94,8 +94,10 @@ public class BsonWriter extends Writer implements ByteTuple { this.count = array.length(); } - public BsonWriter withFeatures(int features) { - return (BsonWriter) super.withFeatures(features); + @Override + public final BsonWriter withFeatures(int features) { + super.withFeatures(features); + return this; } // ----------------------------------------------------------------------- diff --git a/src/main/java/org/redkale/convert/json/JsonByteBufferReader.java b/src/main/java/org/redkale/convert/json/JsonByteBufferReader.java index 0ba1f4027..10be8f0e7 100644 --- a/src/main/java/org/redkale/convert/json/JsonByteBufferReader.java +++ b/src/main/java/org/redkale/convert/json/JsonByteBufferReader.java @@ -24,14 +24,14 @@ public class JsonByteBufferReader extends JsonReader { private ByteBuffer[] buffers; - private int currentIndex = 0; - private ByteBuffer currentBuffer; + + private int currBufIndex = 0; protected JsonByteBufferReader(ByteBuffer... buffers) { this.buffers = buffers; if (buffers != null && buffers.length > 0) { - this.currentBuffer = buffers[currentIndex]; + this.currentBuffer = buffers[currBufIndex]; } } @@ -40,7 +40,7 @@ public class JsonByteBufferReader extends JsonReader { super.recycle(); // this.position 初始化值为-1 this.currentChar = 0; this.buffers = null; - this.currentIndex = 0; + this.currBufIndex = 0; this.currentBuffer = null; return false; } @@ -51,7 +51,7 @@ public class JsonByteBufferReader extends JsonReader { return this.currentBuffer.get(); } for (; ; ) { - this.currentBuffer = this.buffers[++this.currentIndex]; + this.currentBuffer = this.buffers[++this.currBufIndex]; if (this.currentBuffer.hasRemaining()) { this.position++; return this.currentBuffer.get(); @@ -77,7 +77,7 @@ public class JsonByteBufferReader extends JsonReader { } if (this.currentBuffer != null) { int remain = this.currentBuffer.remaining(); - if (remain == 0 && this.currentIndex + 1 >= this.buffers.length) { + if (remain == 0 && this.currBufIndex + 1 >= this.buffers.length) { return 0; } } diff --git a/src/main/java/org/redkale/convert/json/JsonByteBufferWriter.java b/src/main/java/org/redkale/convert/json/JsonByteBufferWriter.java index 2a772cb87..55d9c8405 100644 --- a/src/main/java/org/redkale/convert/json/JsonByteBufferWriter.java +++ b/src/main/java/org/redkale/convert/json/JsonByteBufferWriter.java @@ -31,7 +31,7 @@ public class JsonByteBufferWriter extends JsonWriter { private ByteBuffer[] buffers; - private int index; + private int currBufIndex; public JsonByteBufferWriter(int features, Supplier supplier) { this(features, null, supplier); @@ -48,7 +48,7 @@ public class JsonByteBufferWriter extends JsonWriter { super.recycle(); this.charset = null; this.buffers = null; - this.index = 0; + this.currBufIndex = 0; return false; } @@ -56,7 +56,7 @@ public class JsonByteBufferWriter extends JsonWriter { if (buffers == null) { return new ByteBuffer[0]; } - for (int i = index; i < this.buffers.length; i++) { + for (int i = currBufIndex; i < this.buffers.length; i++) { ByteBuffer buf = this.buffers[i]; if (buf.position() != 0) { buf.flip(); @@ -78,15 +78,15 @@ public class JsonByteBufferWriter extends JsonWriter { private int expand(final int byteLength) { if (this.buffers == null) { - this.index = 0; + this.currBufIndex = 0; this.buffers = new ByteBuffer[] {supplier.get()}; } - ByteBuffer buffer = this.buffers[index]; + ByteBuffer buffer = this.buffers[currBufIndex]; if (!buffer.hasRemaining()) { buffer.flip(); buffer = supplier.get(); this.buffers = Utility.append(this.buffers, buffer); - this.index++; + this.currBufIndex++; } int len = buffer.remaining(); int size = 0; @@ -105,7 +105,7 @@ public class JsonByteBufferWriter extends JsonWriter { throw new ConvertException("writeTo char(int.value = " + (int) ch + ") must be less 127"); } expand(1); - this.buffers[index].put((byte) ch); + this.buffers[currBufIndex].put((byte) ch); } @Override @@ -116,16 +116,16 @@ public class JsonByteBufferWriter extends JsonWriter { @Override public void writeTo(final byte ch) { // 只能是 0 - 127 的字符 expand(1); - this.buffers[index].put(ch); + this.buffers[currBufIndex].put(ch); } @Override public void writeTo(final byte[] chs, final int start, final int len) { // 只能是 0 - 127 的字符 int expandsize = expand(len); if (expandsize == 0) { // 只需要一个buffer - this.buffers[index].put(chs, start, len); + this.buffers[currBufIndex].put(chs, start, len); } else { - ByteBuffer buffer = this.buffers[index]; + ByteBuffer buffer = this.buffers[currBufIndex]; int remain = len; int offset = start; while (remain > 0) { @@ -154,7 +154,7 @@ public class JsonByteBufferWriter extends JsonWriter { expandsize = expand(byteLength); } if (expandsize == 0) { // 只需要一个buffer - final ByteBuffer buffer = this.buffers[index]; + final ByteBuffer buffer = this.buffers[currBufIndex]; if (quote) { buffer.put((byte) '"'); } @@ -190,7 +190,7 @@ public class JsonByteBufferWriter extends JsonWriter { } return; } - ByteBuffer buffer = this.buffers[index]; + ByteBuffer buffer = this.buffers[currBufIndex]; if (quote) { if (!buffer.hasRemaining()) { buffer = nextByteBuffer(); @@ -266,8 +266,8 @@ public class JsonByteBufferWriter extends JsonWriter { } private ByteBuffer nextByteBuffer() { - this.buffers[this.index].flip(); - return this.buffers[++this.index]; + this.buffers[this.currBufIndex].flip(); + return this.buffers[++this.currBufIndex]; } protected static int encodeEscapeUTF8Length(final char[] text, final int start, final int len) { @@ -316,7 +316,7 @@ public class JsonByteBufferWriter extends JsonWriter { byte[] bs = Utility.latin1ByteArray(value); int expandsize = expand(bs.length + (quote ? 2 : 0)); if (expandsize == 0) { // 只需要一个buffer - final ByteBuffer buffer = this.buffers[index]; + final ByteBuffer buffer = this.buffers[currBufIndex]; if (quote) { buffer.put((byte) '"'); } @@ -325,7 +325,7 @@ public class JsonByteBufferWriter extends JsonWriter { buffer.put((byte) '"'); } } else { - ByteBuffer buffer = this.buffers[index]; + ByteBuffer buffer = this.buffers[currBufIndex]; if (quote) { if (!buffer.hasRemaining()) { buffer = nextByteBuffer(); @@ -353,11 +353,11 @@ public class JsonByteBufferWriter extends JsonWriter { byte[] bs2 = Utility.latin1ByteArray(String.valueOf(value)); int expandsize = expand(bs1.length + bs2.length); if (expandsize == 0) { // 只需要一个buffer - final ByteBuffer buffer = this.buffers[index]; + final ByteBuffer buffer = this.buffers[currBufIndex]; buffer.put(bs1); buffer.put(bs2); } else { - ByteBuffer buffer = this.buffers[index]; + ByteBuffer buffer = this.buffers[currBufIndex]; for (byte b : bs1) { if (!buffer.hasRemaining()) { buffer = nextByteBuffer(); @@ -379,11 +379,11 @@ public class JsonByteBufferWriter extends JsonWriter { byte[] bs2 = Utility.latin1ByteArray(String.valueOf(value)); int expandsize = expand(bs1.length + bs2.length); if (expandsize == 0) { // 只需要一个buffer - final ByteBuffer buffer = this.buffers[index]; + final ByteBuffer buffer = this.buffers[currBufIndex]; buffer.put(bs1); buffer.put(bs2); } else { - ByteBuffer buffer = this.buffers[index]; + ByteBuffer buffer = this.buffers[currBufIndex]; for (byte b : bs1) { if (!buffer.hasRemaining()) { buffer = nextByteBuffer(); @@ -405,11 +405,11 @@ public class JsonByteBufferWriter extends JsonWriter { byte[] bs2 = Utility.latin1ByteArray(String.valueOf(value)); int expandsize = expand(bs1.length + bs2.length); if (expandsize == 0) { // 只需要一个buffer - final ByteBuffer buffer = this.buffers[index]; + final ByteBuffer buffer = this.buffers[currBufIndex]; buffer.put(bs1); buffer.put(bs2); } else { - ByteBuffer buffer = this.buffers[index]; + ByteBuffer buffer = this.buffers[currBufIndex]; for (byte b : bs1) { if (!buffer.hasRemaining()) { buffer = nextByteBuffer(); @@ -431,13 +431,13 @@ public class JsonByteBufferWriter extends JsonWriter { byte[] bs2 = Utility.latin1ByteArray(value); int expandsize = expand(bs1.length + bs2.length + 2); if (expandsize == 0) { // 只需要一个buffer - final ByteBuffer buffer = this.buffers[index]; + final ByteBuffer buffer = this.buffers[currBufIndex]; buffer.put(bs1); buffer.put((byte) '"'); buffer.put(bs2); buffer.put((byte) '"'); } else { - ByteBuffer buffer = this.buffers[index]; + ByteBuffer buffer = this.buffers[currBufIndex]; for (byte b : bs1) { if (!buffer.hasRemaining()) { buffer = nextByteBuffer(); @@ -471,12 +471,12 @@ public class JsonByteBufferWriter extends JsonWriter { byte[] bs2 = Utility.latin1ByteArray(String.valueOf(value)); int expandsize = expand(bs1.length + bs2.length + 1); if (expandsize == 0) { // 只需要一个buffer - final ByteBuffer buffer = this.buffers[index]; + final ByteBuffer buffer = this.buffers[currBufIndex]; buffer.put(bs1); buffer.put(bs2); buffer.put((byte) '}'); } else { - ByteBuffer buffer = this.buffers[index]; + ByteBuffer buffer = this.buffers[currBufIndex]; for (byte b : bs1) { if (!buffer.hasRemaining()) { buffer = nextByteBuffer(); @@ -504,12 +504,12 @@ public class JsonByteBufferWriter extends JsonWriter { byte[] bs2 = Utility.latin1ByteArray(String.valueOf(value)); int expandsize = expand(bs1.length + bs2.length + 1); if (expandsize == 0) { // 只需要一个buffer - final ByteBuffer buffer = this.buffers[index]; + final ByteBuffer buffer = this.buffers[currBufIndex]; buffer.put(bs1); buffer.put(bs2); buffer.put((byte) '}'); } else { - ByteBuffer buffer = this.buffers[index]; + ByteBuffer buffer = this.buffers[currBufIndex]; for (byte b : bs1) { if (!buffer.hasRemaining()) { buffer = nextByteBuffer(); @@ -537,12 +537,12 @@ public class JsonByteBufferWriter extends JsonWriter { byte[] bs2 = Utility.latin1ByteArray(String.valueOf(value)); int expandsize = expand(bs1.length + bs2.length + 1); if (expandsize == 0) { // 只需要一个buffer - final ByteBuffer buffer = this.buffers[index]; + final ByteBuffer buffer = this.buffers[currBufIndex]; buffer.put(bs1); buffer.put(bs2); buffer.put((byte) '}'); } else { - ByteBuffer buffer = this.buffers[index]; + ByteBuffer buffer = this.buffers[currBufIndex]; for (byte b : bs1) { if (!buffer.hasRemaining()) { buffer = nextByteBuffer(); @@ -574,20 +574,20 @@ public class JsonByteBufferWriter extends JsonWriter { } if (value == null || (tiny() && value.isEmpty())) { expand(1); - this.buffers[index].put((byte) '}'); + this.buffers[currBufIndex].put((byte) '}'); } else { byte[] bs1 = fieldBytes; byte[] bs2 = Utility.latin1ByteArray(value); int expandsize = expand(bs1.length + bs2.length + 3); if (expandsize == 0) { // 只需要一个buffer - final ByteBuffer buffer = this.buffers[index]; + final ByteBuffer buffer = this.buffers[currBufIndex]; buffer.put(bs1); buffer.put((byte) '"'); buffer.put(bs2); buffer.put((byte) '"'); buffer.put((byte) '}'); } else { - ByteBuffer buffer = this.buffers[index]; + ByteBuffer buffer = this.buffers[currBufIndex]; for (byte b : bs1) { if (!buffer.hasRemaining()) { buffer = nextByteBuffer(); @@ -635,11 +635,11 @@ public class JsonByteBufferWriter extends JsonWriter { if (value == null || (tiny() && value.isEmpty())) { int expandsize = expand(2); if (expandsize == 0) { // 只需要一个buffer - ByteBuffer bb = this.buffers[index]; + ByteBuffer bb = this.buffers[currBufIndex]; bb.put((byte) '{'); bb.put((byte) '}'); } else { - ByteBuffer bb = this.buffers[index]; + ByteBuffer bb = this.buffers[currBufIndex]; bb.put((byte) '{'); bb = nextByteBuffer(); bb.put((byte) '}'); @@ -649,14 +649,14 @@ public class JsonByteBufferWriter extends JsonWriter { byte[] bs2 = Utility.latin1ByteArray(value); int expandsize = expand(bs1.length + bs2.length + 3); if (expandsize == 0) { // 只需要一个buffer - final ByteBuffer buffer = this.buffers[index]; + final ByteBuffer buffer = this.buffers[currBufIndex]; buffer.put(bs1); buffer.put((byte) '"'); buffer.put(bs2); buffer.put((byte) '"'); buffer.put((byte) '}'); } else { - ByteBuffer buffer = this.buffers[index]; + ByteBuffer buffer = this.buffers[currBufIndex]; for (byte b : bs1) { if (!buffer.hasRemaining()) { buffer = nextByteBuffer(); @@ -705,14 +705,14 @@ public class JsonByteBufferWriter extends JsonWriter { byte[] bs4 = Utility.latin1ByteArray(String.valueOf(value2)); int expandsize = expand(bs1.length + bs2.length + bs3.length + bs4.length + 1); if (expandsize == 0) { // 只需要一个buffer - final ByteBuffer buffer = this.buffers[index]; + final ByteBuffer buffer = this.buffers[currBufIndex]; buffer.put(bs1); buffer.put(bs2); buffer.put(bs3); buffer.put(bs4); buffer.put((byte) '}'); } else { - ByteBuffer buffer = this.buffers[index]; + ByteBuffer buffer = this.buffers[currBufIndex]; for (byte b : bs1) { if (!buffer.hasRemaining()) { buffer = nextByteBuffer(); @@ -805,7 +805,7 @@ public class JsonByteBufferWriter extends JsonWriter { final int byteLength = 2 + encodeEscapeUTF8Length(chs, 0, chs.length); expandsize = expand(byteLength); if (expandsize == 0) { // 只需要一个buffer - final ByteBuffer buffer = this.buffers[index]; + final ByteBuffer buffer = this.buffers[currBufIndex]; if (quote) { buffer.put((byte) '"'); } diff --git a/src/main/java/org/redkale/convert/pb/ProtobufArrayEncoder.java b/src/main/java/org/redkale/convert/pb/ProtobufArrayEncoder.java index f79e2b9f4..2b41a8eb1 100644 --- a/src/main/java/org/redkale/convert/pb/ProtobufArrayEncoder.java +++ b/src/main/java/org/redkale/convert/pb/ProtobufArrayEncoder.java @@ -19,7 +19,7 @@ public class ProtobufArrayEncoder extends ArrayEncoder { public ProtobufArrayEncoder(ProtobufFactory factory, Type type) { super(factory, type); - this.componentSimpled = getComponentEncoder()instanceof SimpledCoder; + this.componentSimpled = getComponentEncoder() instanceof SimpledCoder; } @Override @@ -40,8 +40,7 @@ public class ProtobufArrayEncoder extends ArrayEncoder { } else { ProtobufWriter tmp = out.pollChild(); itemEncoder.convertTo(tmp, item); - out.writeLength(tmp.count()); - out.writeTo(tmp.toArray()); + out.writeTuple(tmp); out.offerChild(tmp); } } diff --git a/src/main/java/org/redkale/convert/pb/ProtobufByteBufferReader.java b/src/main/java/org/redkale/convert/pb/ProtobufByteBufferReader.java index 1ecd49673..b5863f085 100644 --- a/src/main/java/org/redkale/convert/pb/ProtobufByteBufferReader.java +++ b/src/main/java/org/redkale/convert/pb/ProtobufByteBufferReader.java @@ -17,17 +17,21 @@ public class ProtobufByteBufferReader extends ProtobufReader { private ByteBuffer currentBuffer; - private int currentIndex = 0; + private int currBufIndex = 0; + + protected ProtobufByteBufferReader() { + // do nothing + } protected ProtobufByteBufferReader(ByteBuffer... buffers) { this.buffers = buffers; - this.currentBuffer = buffers[currentIndex]; + this.currentBuffer = buffers[currBufIndex]; } @Override protected boolean recycle() { super.recycle(); - this.currentIndex = 0; + this.currBufIndex = 0; this.currentBuffer = null; this.buffers = null; return false; @@ -39,7 +43,7 @@ public class ProtobufByteBufferReader extends ProtobufReader { return this.currentBuffer.get(); } for (; ; ) { - this.currentBuffer = this.buffers[++this.currentIndex]; + this.currentBuffer = this.buffers[++this.currBufIndex]; if (this.currentBuffer.hasRemaining()) { this.position++; return this.currentBuffer.get(); @@ -67,8 +71,8 @@ public class ProtobufByteBufferReader extends ProtobufReader { array.put(currentBuffer); } int end = buffers.length - 1; - while (this.currentIndex < end) { - this.currentBuffer = this.buffers[++this.currentIndex]; + while (this.currBufIndex < end) { + this.currentBuffer = this.buffers[++this.currBufIndex]; array.put(currentBuffer); } return array.getBytes(); @@ -83,8 +87,8 @@ public class ProtobufByteBufferReader extends ProtobufReader { return true; } int end = buffers.length - 1; - while (this.currentIndex < end) { - this.currentBuffer = this.buffers[++this.currentIndex]; + while (this.currBufIndex < end) { + this.currentBuffer = this.buffers[++this.currBufIndex]; if (this.currentBuffer.hasRemaining()) { return true; } diff --git a/src/main/java/org/redkale/convert/pb/ProtobufByteBufferWriter.java b/src/main/java/org/redkale/convert/pb/ProtobufByteBufferWriter.java index 20fbbe1fd..09de1b353 100644 --- a/src/main/java/org/redkale/convert/pb/ProtobufByteBufferWriter.java +++ b/src/main/java/org/redkale/convert/pb/ProtobufByteBufferWriter.java @@ -6,6 +6,7 @@ package org.redkale.convert.pb; import java.nio.ByteBuffer; +import java.util.Objects; import java.util.function.Supplier; import org.redkale.util.Utility; @@ -16,7 +17,7 @@ public class ProtobufByteBufferWriter extends ProtobufWriter { private ByteBuffer[] buffers; - private int index; + private int currBufIndex; public ProtobufByteBufferWriter(int features, boolean enumtostring, Supplier supplier) { super((byte[]) null); @@ -29,7 +30,7 @@ public class ProtobufByteBufferWriter extends ProtobufWriter { protected boolean recycle() { super.recycle(); this.buffers = null; - this.index = 0; + this.currBufIndex = 0; return false; } @@ -38,7 +39,7 @@ public class ProtobufByteBufferWriter extends ProtobufWriter { if (buffers == null) { return new ByteBuffer[0]; } - for (int i = index; i < this.buffers.length; i++) { + for (int i = currBufIndex; i < this.buffers.length; i++) { ByteBuffer buf = this.buffers[i]; if (buf.position() != 0) { buf.flip(); @@ -47,39 +48,18 @@ public class ProtobufByteBufferWriter extends ProtobufWriter { return this.buffers; } - @Override - public byte[] toArray() { - if (buffers == null) { - return new byte[0]; - } - int pos = 0; - byte[] bytes = new byte[this.count]; - for (ByteBuffer buf : toBuffers()) { - int r = buf.remaining(); - buf.get(bytes, pos, r); - buf.flip(); - pos += r; - } - return bytes; - } - - @Override - public String toString() { - return this.getClass().getSimpleName() + "[count=" + this.count + "]"; - } - @Override protected int expand(final int byteLength) { if (this.buffers == null) { - this.index = 0; + this.currBufIndex = 0; this.buffers = new ByteBuffer[] {supplier.get()}; } - ByteBuffer buffer = this.buffers[index]; + ByteBuffer buffer = this.buffers[currBufIndex]; if (!buffer.hasRemaining()) { buffer.flip(); buffer = supplier.get(); this.buffers = Utility.append(this.buffers, buffer); - this.index++; + this.currBufIndex++; } int len = buffer.remaining(); int size = 0; @@ -92,19 +72,26 @@ public class ProtobufByteBufferWriter extends ProtobufWriter { return size; } + @Override + public void writeTo(final byte ch) { + expand(1); + this.buffers[currBufIndex].put(ch); + count++; + } + @Override public void writeTo(final byte[] chs, final int start, final int len) { if (expand(len) == 0) { - this.buffers[index].put(chs, start, len); + this.buffers[currBufIndex].put(chs, start, len); } else { - ByteBuffer buffer = this.buffers[index]; + ByteBuffer buffer = this.buffers[currBufIndex]; final int end = start + len; int remain = len; // 还剩多少没有写 while (remain > 0) { final int br = buffer.remaining(); if (remain > br) { // 一个buffer写不完 - buffer.put(chs, end - remain, br); - buffer = nextByteBuffer(); + buffer.put(chs, end - remain, br).flip(); + buffer = this.buffers[++this.currBufIndex]; remain -= br; } else { buffer.put(chs, end - remain, remain); @@ -115,30 +102,63 @@ public class ProtobufByteBufferWriter extends ProtobufWriter { this.count += len; } - private ByteBuffer nextByteBuffer() { - this.buffers[this.index].flip(); - return this.buffers[++this.index]; + @Override + protected final void writeUInt32(int value) { + if (value >= 0 && value < TENTHOUSAND_MAX) { + writeTo(TENTHOUSAND_UINT_BYTES[value]); + return; + } else if (value < 0 && value > TENTHOUSAND_MAX) { + writeTo(TENTHOUSAND_UINT_BYTES2[-value]); + return; + } + while (true) { + if ((value & ~0x7F) == 0) { + writeTo((byte) value); + return; + } else { + writeTo((byte) ((value & 0x7F) | 0x80)); + value >>>= 7; + } + } } @Override - public void writeTo(final byte ch) { - expand(1); - this.buffers[index].put(ch); - count++; + protected final void writeUInt64(long value) { + if (value >= 0 && value < TENTHOUSAND_MAX) { + writeTo(TENTHOUSAND_UINT_BYTES[(int) value]); + return; + } else if (value < 0 && value > TENTHOUSAND_MAX) { + writeTo(TENTHOUSAND_UINT_BYTES2[(int) -value]); + return; + } + while (true) { + if ((value & ~0x7FL) == 0) { + writeTo((byte) value); + return; + } else { + writeTo((byte) ((value & 0x7F) | 0x80)); + value >>>= 7; + } + } } @Override - public byte[] content() { + public String toString() { + return Objects.toString(this); + } + + @Override + public final ProtobufWriter clear() { throw new UnsupportedOperationException("Not supported yet."); // 无需实现 } @Override - public int offset() { + public final byte[] toArray() { throw new UnsupportedOperationException("Not supported yet."); // 无需实现 } @Override - public int length() { + public final byte[] content() { throw new UnsupportedOperationException("Not supported yet."); // 无需实现 } } diff --git a/src/main/java/org/redkale/convert/pb/ProtobufCoders.java b/src/main/java/org/redkale/convert/pb/ProtobufCoders.java index b22ad7dac..19df8bac7 100644 --- a/src/main/java/org/redkale/convert/pb/ProtobufCoders.java +++ b/src/main/java/org/redkale/convert/pb/ProtobufCoders.java @@ -16,7 +16,7 @@ import org.redkale.util.*; */ public abstract class ProtobufCoders { - private static final Creator LIST_CREATOR = Creator.load(List.class); + static final Creator LIST_CREATOR = Creator.load(List.class); private ProtobufCoders() { // do nothing diff --git a/src/main/java/org/redkale/convert/pb/ProtobufCollectionEncoder.java b/src/main/java/org/redkale/convert/pb/ProtobufCollectionEncoder.java index 72940c01c..5a0f94422 100644 --- a/src/main/java/org/redkale/convert/pb/ProtobufCollectionEncoder.java +++ b/src/main/java/org/redkale/convert/pb/ProtobufCollectionEncoder.java @@ -39,8 +39,7 @@ public class ProtobufCollectionEncoder extends CollectionEncoder S configWrite(S writer) { - writer.initOffset = writer.count; - return writer; + return (S) writer.configWrite(); } public ProtobufByteBufferWriter pollProtobufWriter(final Supplier supplier) { @@ -826,7 +825,7 @@ public class ProtobufConvert extends BinaryConvert Encodeable createCollectionEncoder(Type type) { + if (type instanceof ParameterizedType) { + ParameterizedType pt = (ParameterizedType) type; + Type componentType = pt.getActualTypeArguments()[0]; + Creator creator = ProtobufCoders.LIST_CREATOR; + if (componentType == Boolean.class) { + return (Encodeable) new ProtobufBoolCollectionSimpledCoder(creator); + } else if (componentType == Byte.class) { + return (Encodeable) new ProtobufByteCollectionSimpledCoder(creator); + } else if (componentType == Character.class) { + return (Encodeable) new ProtobufCharCollectionSimpledCoder(creator); + } else if (componentType == Short.class) { + return (Encodeable) new ProtobufShortCollectionSimpledCoder(creator); + } else if (componentType == Integer.class) { + return (Encodeable) new ProtobufIntCollectionSimpledCoder(creator); + } else if (componentType == Float.class) { + return (Encodeable) new ProtobufFloatCollectionSimpledCoder(creator); + } else if (componentType == Long.class) { + return (Encodeable) new ProtobufLongCollectionSimpledCoder(creator); + } else if (componentType == Double.class) { + return (Encodeable) new ProtobufDoubleCollectionSimpledCoder(creator); + } else if (componentType == AtomicInteger.class) { + return (Encodeable) new ProtobufAtomicIntegerCollectionSimpledCoder(creator); + } else if (componentType == AtomicLong.class) { + return (Encodeable) new ProtobufAtomicLongCollectionSimpledCoder(creator); + } + } return new ProtobufCollectionEncoder(this, type); } @@ -262,6 +288,31 @@ public class ProtobufFactory extends ConvertFactory Encodeable createStreamEncoder(Type type) { + if (type instanceof ParameterizedType) { + ParameterizedType pt = (ParameterizedType) type; + Type componentType = pt.getActualTypeArguments()[0]; + if (componentType == Boolean.class) { + return (Encodeable) ProtobufBoolStreamSimpledCoder.instance; + } else if (componentType == Byte.class) { + return (Encodeable) ProtobufByteStreamSimpledCoder.instance; + } else if (componentType == Character.class) { + return (Encodeable) ProtobufCharStreamSimpledCoder.instance; + } else if (componentType == Short.class) { + return (Encodeable) ProtobufShortStreamSimpledCoder.instance; + } else if (componentType == Integer.class) { + return (Encodeable) ProtobufIntStreamSimpledCoder.instance; + } else if (componentType == Float.class) { + return (Encodeable) ProtobufFloatStreamSimpledCoder.instance; + } else if (componentType == Long.class) { + return (Encodeable) ProtobufLongStreamSimpledCoder.instance; + } else if (componentType == Double.class) { + return (Encodeable) ProtobufDoubleStreamSimpledCoder.instance; + } else if (componentType == AtomicInteger.class) { + return (Encodeable) ProtobufAtomicIntegerStreamSimpledCoder.instance; + } else if (componentType == AtomicLong.class) { + return (Encodeable) ProtobufAtomicLongStreamSimpledCoder.instance; + } + } return new ProtobufStreamEncoder(this, type); } @@ -366,7 +417,7 @@ public class ProtobufFactory extends ConvertFactory extends MapEncoder { kencoder.convertTo(tmp, key); tmp.writeTag(valTag); vencoder.convertTo(tmp, v); - - out.writeLength(tmp.count()); - out.writeTo(tmp.toArray()); + + out.writeTuple(tmp); out.offerChild(tmp); } } diff --git a/src/main/java/org/redkale/convert/pb/ProtobufObjectEncoder.java b/src/main/java/org/redkale/convert/pb/ProtobufObjectEncoder.java index e5aefd321..425c6d7ad 100644 --- a/src/main/java/org/redkale/convert/pb/ProtobufObjectEncoder.java +++ b/src/main/java/org/redkale/convert/pb/ProtobufObjectEncoder.java @@ -33,7 +33,7 @@ public class ProtobufObjectEncoder extends ObjectEncoder { @Override protected ProtobufWriter objectWriter(ProtobufWriter out, T value) { - if (out.count() > out.initOffset) { + if (out.length() > out.initOffset) { return out.pollChild().configParentFunc(out); } return out; diff --git a/src/main/java/org/redkale/convert/pb/ProtobufReader.java b/src/main/java/org/redkale/convert/pb/ProtobufReader.java index 6105fcb27..fa9f724df 100644 --- a/src/main/java/org/redkale/convert/pb/ProtobufReader.java +++ b/src/main/java/org/redkale/convert/pb/ProtobufReader.java @@ -284,7 +284,6 @@ public class ProtobufReader extends Reader { public final int[] readInts() { int len = readRawVarint32(); - System.out.println("等到长度: " + len); List list = new ArrayList<>(len); while (len > 0) { int val = readInt(); diff --git a/src/main/java/org/redkale/convert/pb/ProtobufStreamEncoder.java b/src/main/java/org/redkale/convert/pb/ProtobufStreamEncoder.java index c545d783a..29332a43c 100644 --- a/src/main/java/org/redkale/convert/pb/ProtobufStreamEncoder.java +++ b/src/main/java/org/redkale/convert/pb/ProtobufStreamEncoder.java @@ -40,8 +40,7 @@ public class ProtobufStreamEncoder extends StreamEncoder { } else { ProtobufWriter tmp = out.pollChild(); itemEncoder.convertTo(tmp, item); - out.writeLength(tmp.count()); - out.writeTo(tmp.toArray()); + out.writeTuple(tmp); out.offerChild(tmp); } } diff --git a/src/main/java/org/redkale/convert/pb/ProtobufStreamReader.java b/src/main/java/org/redkale/convert/pb/ProtobufStreamReader.java index cd4eb44b4..bd31f17ec 100644 --- a/src/main/java/org/redkale/convert/pb/ProtobufStreamReader.java +++ b/src/main/java/org/redkale/convert/pb/ProtobufStreamReader.java @@ -38,6 +38,7 @@ class ProtobufStreamReader extends ProtobufByteBufferReader { if (backByte != null) { byte b = backByte; backByte = null; + this.position++; return b; } try { @@ -67,6 +68,7 @@ class ProtobufStreamReader extends ProtobufByteBufferReader { try { int v; while ((v = in.read()) != -1) { + this.position++; array.putByte(v); } } catch (IOException e) { @@ -77,6 +79,12 @@ class ProtobufStreamReader extends ProtobufByteBufferReader { @Override public boolean hasNext() { + if (backByte != null) { + return true; + } + if (this.limit > 0 && (this.position + 1) >= this.limit) { + return false; + } try { int v = in.read(); if (v == -1) { diff --git a/src/main/java/org/redkale/convert/pb/ProtobufStreamWriter.java b/src/main/java/org/redkale/convert/pb/ProtobufStreamWriter.java index d679d187b..aab1fdf25 100644 --- a/src/main/java/org/redkale/convert/pb/ProtobufStreamWriter.java +++ b/src/main/java/org/redkale/convert/pb/ProtobufStreamWriter.java @@ -6,6 +6,7 @@ package org.redkale.convert.pb; import java.io.*; +import java.nio.ByteBuffer; import org.redkale.convert.ConvertException; /** @@ -36,6 +37,7 @@ class ProtobufStreamWriter extends ProtobufByteBufferWriter { } catch (IOException e) { throw new ConvertException(e); } + this.count += len; } @Override @@ -45,5 +47,11 @@ class ProtobufStreamWriter extends ProtobufByteBufferWriter { } catch (IOException e) { throw new ConvertException(e); } + count++; + } + + @Override + public ByteBuffer[] toBuffers() { + throw new UnsupportedOperationException("Not supported yet."); // 无需实现 } } diff --git a/src/main/java/org/redkale/convert/pb/ProtobufWriter.java b/src/main/java/org/redkale/convert/pb/ProtobufWriter.java index 7b5520b33..69a1d39b5 100644 --- a/src/main/java/org/redkale/convert/pb/ProtobufWriter.java +++ b/src/main/java/org/redkale/convert/pb/ProtobufWriter.java @@ -25,22 +25,22 @@ public class ProtobufWriter extends Writer implements ByteTuple { private static final int CHILD_SIZE = 8; - private static final int TENTHOUSAND_MAX = 10001; + protected static final int TENTHOUSAND_MAX = 10001; - private static final byte[][] TENTHOUSAND_UINT_BYTES = new byte[TENTHOUSAND_MAX][]; - private static final byte[][] TENTHOUSAND_UINT_BYTES2 = new byte[TENTHOUSAND_MAX][]; + protected static final byte[][] TENTHOUSAND_UINT_BYTES = new byte[TENTHOUSAND_MAX][]; + protected static final byte[][] TENTHOUSAND_UINT_BYTES2 = new byte[TENTHOUSAND_MAX][]; - private static final byte[][] TENTHOUSAND_SINT32_BYTES = new byte[TENTHOUSAND_MAX][]; - private static final byte[][] TENTHOUSAND_SINT32_BYTES2 = new byte[TENTHOUSAND_MAX][]; + protected static final byte[][] TENTHOUSAND_SINT32_BYTES = new byte[TENTHOUSAND_MAX][]; + protected static final byte[][] TENTHOUSAND_SINT32_BYTES2 = new byte[TENTHOUSAND_MAX][]; - private static final byte[][] TENTHOUSAND_SINT64_BYTES = new byte[TENTHOUSAND_MAX][]; - private static final byte[][] TENTHOUSAND_SINT64_BYTES2 = new byte[TENTHOUSAND_MAX][]; + protected static final byte[][] TENTHOUSAND_SINT64_BYTES = new byte[TENTHOUSAND_MAX][]; + protected static final byte[][] TENTHOUSAND_SINT64_BYTES2 = new byte[TENTHOUSAND_MAX][]; - private static final byte[][] TENTHOUSAND_FIXED32_BYTES = new byte[TENTHOUSAND_MAX][]; - private static final byte[][] TENTHOUSAND_FIXED32_BYTES2 = new byte[TENTHOUSAND_MAX][]; + protected static final byte[][] TENTHOUSAND_FIXED32_BYTES = new byte[TENTHOUSAND_MAX][]; + protected static final byte[][] TENTHOUSAND_FIXED32_BYTES2 = new byte[TENTHOUSAND_MAX][]; - private static final byte[][] TENTHOUSAND_FIXED64_BYTES = new byte[TENTHOUSAND_MAX][]; - private static final byte[][] TENTHOUSAND_FIXED64_BYTES2 = new byte[TENTHOUSAND_MAX][]; + protected static final byte[][] TENTHOUSAND_FIXED64_BYTES = new byte[TENTHOUSAND_MAX][]; + protected static final byte[][] TENTHOUSAND_FIXED64_BYTES2 = new byte[TENTHOUSAND_MAX][]; static { for (int i = 0; i < TENTHOUSAND_UINT_BYTES.length; i++) { @@ -61,16 +61,16 @@ public class ProtobufWriter extends Writer implements ByteTuple { } } - private byte[] content; + protected int initOffset; protected int count; - protected int initOffset; - protected boolean enumtostring; protected ProtobufWriter parent; + private byte[] content; + private ArrayDeque childWriters; protected ProtobufWriter(ProtobufWriter parent) { @@ -87,12 +87,17 @@ public class ProtobufWriter extends Writer implements ByteTuple { } @Override - public ProtobufWriter withFeatures(int features) { + public final ProtobufWriter withFeatures(int features) { super.withFeatures(features); return this; } - protected ProtobufWriter configParentFunc(ProtobufWriter parent) { + protected final ProtobufWriter configWrite() { + this.initOffset = this.count; + return this; + } + + protected final ProtobufWriter configParentFunc(ProtobufWriter parent) { this.parent = parent; if (parent != null) { this.features = parent.features; @@ -101,7 +106,7 @@ public class ProtobufWriter extends Writer implements ByteTuple { return this; } - protected ProtobufWriter configFieldFunc(ProtobufWriter out) { + protected final ProtobufWriter configFieldFunc(ProtobufWriter out) { if (out == null) { return this; } @@ -113,7 +118,7 @@ public class ProtobufWriter extends Writer implements ByteTuple { return this; } - protected BiFunction mapFieldFunc() { + protected final BiFunction mapFieldFunc() { return mapFieldFunc; } @@ -127,6 +132,7 @@ public class ProtobufWriter extends Writer implements ByteTuple { public ProtobufWriter(ByteArray array) { this.content = array.content(); + this.initOffset = array.offset(); this.count = array.length(); } @@ -168,21 +174,21 @@ public class ProtobufWriter extends Writer implements ByteTuple { } } + @Override + public final int length() { + return count; + } + @Override public byte[] content() { return content; } @Override - public int offset() { + public final int offset() { return initOffset; } - @Override - public int length() { - return count; - } - /** * 将本对象的内容引用复制给array * @@ -196,15 +202,6 @@ public class ProtobufWriter extends Writer implements ByteTuple { return new ByteBuffer[] {ByteBuffer.wrap(content, 0, count)}; } - /** - * 直接获取全部数据, 实际数据需要根据count长度来截取 - * - * @return byte[] - */ - public byte[] directBytes() { - return content; - } - public void completed(ConvertBytesHandler handler, Consumer callback) { handler.completed(content, 0, count, callback, this); } @@ -258,77 +255,78 @@ public class ProtobufWriter extends Writer implements ByteTuple { } // ------------------------------------------------------------------------ - public final int count() { - return this.count; - } - @Override public final void writeBoolean(boolean value) { writeTo(value ? (byte) 1 : (byte) 0); } @Override - public void writeNull() { + public final void writeNull() { // do nothing } @Override - public boolean needWriteClassName() { + public final boolean needWriteClassName() { return false; } @Override - public void writeClassName(String clazz) { + public final void writeClassName(String clazz) { // do nothing } @Override @ClassDepends - public void writeObjectB(Object obj) { + public final void writeObjectB(Object obj) { // do nothing } @Override - public void writeObjectE(Object obj) { + public final void writeObjectE(Object obj) { if (parent != null) { - parent.writeLength(count()); - parent.writeTo(toArray()); + parent.writeTuple(this); } } + protected final void writeTuple(ByteTuple tuple) { + int len = tuple.length(); + writeLength(len); + writeTo(tuple.content(), tuple.offset(), len); + } + @Override - public void writeArrayB(int size, Encodeable componentEncoder, Object obj) { + public final void writeArrayB(int size, Encodeable componentEncoder, Object obj) { // do nothing } @Override - public void writeArrayMark() { + public final void writeArrayMark() { // do nothing } @Override - public void writeArrayE() { + public final void writeArrayE() { // do nothing } @Override - public void writeMapB(int size, Encodeable keyEncoder, Encodeable valueEncoder, Object obj) { + public final void writeMapB(int size, Encodeable keyEncoder, Encodeable valueEncoder, Object obj) { // do nothing } @Override - public void writeMapMark() { + public final void writeMapMark() { // do nothing } @Override - public void writeMapE() { + public final void writeMapE() { // do nothing } // 被ObjectEncoder调用 @Override - public void writeFieldName(EnMember member, String fieldName, Type fieldType, int fieldPos) { + public final void writeFieldName(EnMember member, String fieldName, Type fieldType, int fieldPos) { writeTag(member != null ? member.getTag() : fieldPos); } @@ -338,22 +336,22 @@ public class ProtobufWriter extends Writer implements ByteTuple { } @Override - public void writeByte(byte value) { + public final void writeByte(byte value) { writeInt(value); } @Override - public void writeChar(char value) { + public final void writeChar(char value) { writeInt(value); } @Override - public void writeShort(short value) { + public final void writeShort(short value) { writeInt(value); } @Override - public void writeInt(int value) { // writeSInt32 + public final void writeInt(int value) { // writeSInt32 if (value >= 0 && value < TENTHOUSAND_MAX) { writeTo(TENTHOUSAND_SINT32_BYTES[value]); return; @@ -365,7 +363,7 @@ public class ProtobufWriter extends Writer implements ByteTuple { } @Override - public void writeLong(long value) { // writeSInt64 + public final void writeLong(long value) { // writeSInt64 if (value >= 0 && value < TENTHOUSAND_MAX) { writeTo(TENTHOUSAND_SINT64_BYTES[(int) value]); return; @@ -377,28 +375,28 @@ public class ProtobufWriter extends Writer implements ByteTuple { } @Override - public void writeFloat(float value) { + public final void writeFloat(float value) { writeFixed32(Float.floatToRawIntBits(value)); } @Override - public void writeDouble(double value) { + public final void writeDouble(double value) { writeFixed64(Double.doubleToRawLongBits(value)); } @Override - public void writeSmallString(String value) { + public final void writeSmallString(String value) { writeString(value); } @Override - public void writeString(String value) { + public final void writeString(String value) { byte[] bs = Utility.isLatin1(value) ? Utility.latin1ByteArray(value) : value.getBytes(StandardCharsets.UTF_8); writeLength(bs.length); writeTo(bs); } - public void writeStrings(int tag, String[] value) { + public final void writeStrings(int tag, String[] value) { String[] array = value; if (array != null && array.length > 0) { for (String item : array) { @@ -409,7 +407,7 @@ public class ProtobufWriter extends Writer implements ByteTuple { } } - public void writeStrings(int tag, Collection value) { + public final void writeStrings(int tag, Collection value) { Collection array = value; if (array != null && !array.isEmpty()) { for (String item : array) { @@ -420,7 +418,7 @@ public class ProtobufWriter extends Writer implements ByteTuple { } } - public void writeBools(boolean[] value) { + public final void writeBools(boolean[] value) { boolean[] array = value; if (array != null && array.length > 0) { writeLength(array.length); @@ -430,7 +428,7 @@ public class ProtobufWriter extends Writer implements ByteTuple { } } - public void writeBools(Boolean[] value) { + public final void writeBools(Boolean[] value) { Boolean[] array = value; if (array != null && array.length > 0) { writeLength(array.length); @@ -440,7 +438,7 @@ public class ProtobufWriter extends Writer implements ByteTuple { } } - public void writeBools(Collection value) { + public final void writeBools(Collection value) { Collection array = value; if (array != null && !array.isEmpty()) { writeLength(array.size()); @@ -450,13 +448,13 @@ public class ProtobufWriter extends Writer implements ByteTuple { } } - public void writeBools(Stream value) { + public final void writeBools(Stream value) { if (value != null) { writeBools(value.toArray(s -> new Boolean[s])); } } - public void writeBytes(byte[] value) { + public final void writeBytes(byte[] value) { byte[] array = value; if (array != null && array.length > 0) { writeLength(array.length); @@ -464,7 +462,7 @@ public class ProtobufWriter extends Writer implements ByteTuple { } } - public void writeBytes(Byte[] value) { + public final void writeBytes(Byte[] value) { Byte[] array = value; if (array != null && array.length > 0) { writeLength(array.length); @@ -474,7 +472,7 @@ public class ProtobufWriter extends Writer implements ByteTuple { } } - public void writeBytes(Collection value) { + public final void writeBytes(Collection value) { Collection array = value; if (array != null && !array.isEmpty()) { writeLength(array.size()); @@ -484,13 +482,13 @@ public class ProtobufWriter extends Writer implements ByteTuple { } } - public void writeBytes(Stream value) { + public final void writeBytes(Stream value) { if (value != null) { writeBytes(value.toArray(s -> new Byte[s])); } } - public void writeChars(char[] value) { + public final void writeChars(char[] value) { char[] array = value; if (array != null && array.length > 0) { int len = 0; @@ -504,7 +502,7 @@ public class ProtobufWriter extends Writer implements ByteTuple { } } - public void writeChars(Character[] value) { + public final void writeChars(Character[] value) { Character[] array = value; if (array != null && array.length > 0) { int len = 0; @@ -518,7 +516,7 @@ public class ProtobufWriter extends Writer implements ByteTuple { } } - public void writeChars(Collection value) { + public final void writeChars(Collection value) { Collection array = value; if (array != null && !array.isEmpty()) { int len = 0; @@ -532,13 +530,13 @@ public class ProtobufWriter extends Writer implements ByteTuple { } } - public void writeChars(Stream value) { + public final void writeChars(Stream value) { if (value != null) { writeChars(value.toArray(s -> new Character[s])); } } - public void writeShorts(short[] value) { + public final void writeShorts(short[] value) { short[] array = value; if (array != null && array.length > 0) { int len = 0; @@ -552,7 +550,7 @@ public class ProtobufWriter extends Writer implements ByteTuple { } } - public void writeShorts(Short[] value) { + public final void writeShorts(Short[] value) { Short[] array = value; if (array != null && array.length > 0) { int len = 0; @@ -566,7 +564,7 @@ public class ProtobufWriter extends Writer implements ByteTuple { } } - public void writeShorts(Collection value) { + public final void writeShorts(Collection value) { Collection array = value; if (array != null && !array.isEmpty()) { int len = 0; @@ -580,13 +578,13 @@ public class ProtobufWriter extends Writer implements ByteTuple { } } - public void writeShorts(Stream value) { + public final void writeShorts(Stream value) { if (value != null) { writeShorts(value.toArray(s -> new Short[s])); } } - public void writeInts(int[] value) { + public final void writeInts(int[] value) { int[] array = value; if (array != null && array.length > 0) { int len = 0; @@ -600,7 +598,7 @@ public class ProtobufWriter extends Writer implements ByteTuple { } } - public void writeInts(Integer[] value) { + public final void writeInts(Integer[] value) { Integer[] array = value; if (array != null && array.length > 0) { int len = 0; @@ -614,7 +612,7 @@ public class ProtobufWriter extends Writer implements ByteTuple { } } - public void writeInts(Collection value) { + public final void writeInts(Collection value) { Collection array = value; if (array != null && !array.isEmpty()) { int len = 0; @@ -628,13 +626,13 @@ public class ProtobufWriter extends Writer implements ByteTuple { } } - public void writeInts(Stream value) { + public final void writeInts(Stream value) { if (value != null) { writeInts(value.toArray(s -> new Integer[s])); } } - public void writeFloats(float[] value) { + public final void writeFloats(float[] value) { float[] array = value; if (array != null && array.length > 0) { int len = array.length * 4; @@ -645,7 +643,7 @@ public class ProtobufWriter extends Writer implements ByteTuple { } } - public void writeFloats(Float[] value) { + public final void writeFloats(Float[] value) { Float[] array = value; if (array != null && array.length > 0) { int len = array.length * 4; @@ -656,7 +654,7 @@ public class ProtobufWriter extends Writer implements ByteTuple { } } - public void writeFloats(Collection value) { + public final void writeFloats(Collection value) { Collection array = value; if (array != null && !array.isEmpty()) { int len = array.size() * 4; @@ -667,13 +665,13 @@ public class ProtobufWriter extends Writer implements ByteTuple { } } - public void writeFloats(Stream value) { + public final void writeFloats(Stream value) { if (value != null) { writeFloats(value.toArray(s -> new Float[s])); } } - public void writeLongs(long[] value) { + public final void writeLongs(long[] value) { long[] array = value; if (array != null && array.length > 0) { int len = 0; @@ -687,7 +685,7 @@ public class ProtobufWriter extends Writer implements ByteTuple { } } - public void writeLongs(Long[] value) { + public final void writeLongs(Long[] value) { Long[] array = value; if (array != null && array.length > 0) { int len = 0; @@ -701,7 +699,7 @@ public class ProtobufWriter extends Writer implements ByteTuple { } } - public void writeLongs(Collection value) { + public final void writeLongs(Collection value) { Collection array = value; if (array != null && !array.isEmpty()) { int len = 0; @@ -715,13 +713,13 @@ public class ProtobufWriter extends Writer implements ByteTuple { } } - public void writeLongs(Stream value) { + public final void writeLongs(Stream value) { if (value != null) { writeLongs(value.toArray(s -> new Long[s])); } } - public void writeDoubles(double[] value) { + public final void writeDoubles(double[] value) { double[] array = value; if (array != null && array.length > 0) { int len = array.length * 8; @@ -732,7 +730,7 @@ public class ProtobufWriter extends Writer implements ByteTuple { } } - public void writeDoubles(Double[] value) { + public final void writeDoubles(Double[] value) { Double[] array = value; if (array != null && array.length > 0) { int len = array.length * 8; @@ -743,7 +741,7 @@ public class ProtobufWriter extends Writer implements ByteTuple { } } - public void writeDoubles(Collection value) { + public final void writeDoubles(Collection value) { Collection array = value; if (array != null && !array.isEmpty()) { int len = array.size() * 8; @@ -754,13 +752,13 @@ public class ProtobufWriter extends Writer implements ByteTuple { } } - public void writeDoubles(Stream value) { + public final void writeDoubles(Stream value) { if (value != null) { writeDoubles(value.toArray(s -> new Double[s])); } } - public void writeAtomicIntegers(AtomicInteger[] value) { + public final void writeAtomicIntegers(AtomicInteger[] value) { AtomicInteger[] array = value; if (array != null && array.length > 0) { int len = 0; @@ -774,7 +772,7 @@ public class ProtobufWriter extends Writer implements ByteTuple { } } - public void writeAtomicIntegers(Collection value) { + public final void writeAtomicIntegers(Collection value) { Collection array = value; if (array != null && !array.isEmpty()) { int len = 0; @@ -788,13 +786,13 @@ public class ProtobufWriter extends Writer implements ByteTuple { } } - public void writeAtomicIntegers(Stream value) { + public final void writeAtomicIntegers(Stream value) { if (value != null) { writeAtomicIntegers(value.toArray(s -> new AtomicInteger[s])); } } - public void writeAtomicLongs(AtomicLong[] value) { + public final void writeAtomicLongs(AtomicLong[] value) { AtomicLong[] array = value; if (array != null && array.length > 0) { int len = 0; @@ -808,7 +806,7 @@ public class ProtobufWriter extends Writer implements ByteTuple { } } - public void writeAtomicLongs(Collection value) { + public final void writeAtomicLongs(Collection value) { Collection array = value; if (array != null && !array.isEmpty()) { int len = 0; @@ -822,460 +820,411 @@ public class ProtobufWriter extends Writer implements ByteTuple { } } - public void writeAtomicLongs(Stream value) { + public final void writeAtomicLongs(Stream value) { if (value != null) { writeAtomicLongs(value.toArray(s -> new AtomicLong[s])); } } @Override - public void writeWrapper(StringWrapper value) { + public final void writeWrapper(StringWrapper value) { if (value != null) { writeString(value.getValue()); } } @ClassDepends - public void writeFieldValue(int tag, boolean value) { + public final void writeFieldValue(int tag, boolean value) { if (value) { writeTag(tag); writeBoolean(value); - this.comma = true; } } @ClassDepends - public void writeFieldValue(int tag, byte value) { + public final void writeFieldValue(int tag, byte value) { if (value != 0) { writeTag(tag); writeByte(value); - this.comma = true; } } @ClassDepends - public void writeFieldValue(int tag, char value) { + public final void writeFieldValue(int tag, char value) { if (value != 0) { writeTag(tag); writeChar(value); - this.comma = true; } } @ClassDepends - public void writeFieldValue(int tag, short value) { + public final void writeFieldValue(int tag, short value) { if (value != 0) { writeTag(tag); writeShort(value); - this.comma = true; } } @ClassDepends - public void writeFieldValue(int tag, int value) { + public final void writeFieldValue(int tag, int value) { if (value != 0) { writeTag(tag); writeInt(value); - this.comma = true; } } @ClassDepends - public void writeFieldValue(int tag, float value) { + public final void writeFieldValue(int tag, float value) { if (value != 0) { writeTag(tag); writeFloat(value); - this.comma = true; } } @ClassDepends - public void writeFieldValue(int tag, long value) { + public final void writeFieldValue(int tag, long value) { if (value != 0) { writeTag(tag); writeLong(value); - this.comma = true; } } @ClassDepends - public void writeFieldValue(int tag, double value) { + public final void writeFieldValue(int tag, double value) { if (value != 0) { writeTag(tag); writeDouble(value); - this.comma = true; } } @ClassDepends - public void writeFieldValue(int tag, Boolean value) { + public final void writeFieldValue(int tag, Boolean value) { if (value != null && !value) { writeTag(tag); writeBoolean(value); - this.comma = true; } } @ClassDepends - public void writeFieldValue(int tag, Byte value) { + public final void writeFieldValue(int tag, Byte value) { if (value != null && value != 0) { writeTag(tag); writeByte(value); - this.comma = true; } } @ClassDepends - public void writeFieldValue(int tag, Character value) { + public final void writeFieldValue(int tag, Character value) { if (value != null && value != 0) { writeTag(tag); writeChar(value); - this.comma = true; } } @ClassDepends - public void writeFieldValue(int tag, Short value) { + public final void writeFieldValue(int tag, Short value) { if (value != null && value != 0) { writeTag(tag); writeShort(value); - this.comma = true; } } @ClassDepends - public void writeFieldValue(int tag, Integer value) { + public final void writeFieldValue(int tag, Integer value) { if (value != null && value != 0) { writeTag(tag); writeInt(value); - this.comma = true; } } @ClassDepends - public void writeFieldValue(int tag, Float value) { + public final void writeFieldValue(int tag, Float value) { if (value != null && value != 0) { writeTag(tag); writeFloat(value); - this.comma = true; } } @ClassDepends - public void writeFieldValue(int tag, Long value) { + public final void writeFieldValue(int tag, Long value) { if (value != null && value != 0) { writeTag(tag); writeLong(value); - this.comma = true; } } @ClassDepends - public void writeFieldValue(int tag, Double value) { + public final void writeFieldValue(int tag, Double value) { if (value != null && value != 0) { writeTag(tag); writeDouble(value); - this.comma = true; } } @ClassDepends - public void writeFieldValue(int tag, AtomicInteger value) { + public final void writeFieldValue(int tag, AtomicInteger value) { if (value != null && value.get() != 0) { writeTag(tag); writeInt(value.get()); - this.comma = true; } } @ClassDepends - public void writeFieldValue(int tag, AtomicLong value) { + public final void writeFieldValue(int tag, AtomicLong value) { if (value != null && value.get() != 0) { writeTag(tag); writeLong(value.get()); - this.comma = true; } } @ClassDepends - public void writeFieldValue(int tag, boolean[] value) { + public final void writeFieldValue(int tag, boolean[] value) { if (value != null && value.length > 0) { writeTag(tag); writeBools(value); - this.comma = true; } } @ClassDepends - public void writeFieldValue(int tag, byte[] value) { + public final void writeFieldValue(int tag, byte[] value) { if (value != null && value.length > 0) { writeTag(tag); writeBytes(value); - this.comma = true; } } @ClassDepends - public void writeFieldValue(int tag, char[] value) { + public final void writeFieldValue(int tag, char[] value) { if (value != null && value.length > 0) { writeTag(tag); writeChars(value); - this.comma = true; } } @ClassDepends - public void writeFieldValue(int tag, short[] value) { + public final void writeFieldValue(int tag, short[] value) { if (value != null && value.length > 0) { writeTag(tag); writeShorts(value); - this.comma = true; } } @ClassDepends - public void writeFieldValue(int tag, int[] value) { + public final void writeFieldValue(int tag, int[] value) { if (value != null && value.length > 0) { writeTag(tag); writeInts(value); - this.comma = true; } } @ClassDepends - public void writeFieldValue(int tag, float[] value) { + public final void writeFieldValue(int tag, float[] value) { if (value != null && value.length > 0) { writeTag(tag); writeFloats(value); - this.comma = true; } } @ClassDepends - public void writeFieldValue(int tag, long[] value) { + public final void writeFieldValue(int tag, long[] value) { if (value != null && value.length > 0) { writeTag(tag); writeLongs(value); - this.comma = true; } } @ClassDepends - public void writeFieldValue(int tag, double[] value) { + public final void writeFieldValue(int tag, double[] value) { if (value != null && value.length > 0) { writeTag(tag); writeDoubles(value); - this.comma = true; } } @ClassDepends - public void writeFieldValue(int tag, Boolean[] value) { + public final void writeFieldValue(int tag, Boolean[] value) { if (value != null && value.length > 0) { writeTag(tag); writeBools(value); - this.comma = true; } } @ClassDepends - public void writeFieldValue(int tag, Byte[] value) { + public final void writeFieldValue(int tag, Byte[] value) { if (value != null && value.length > 0) { writeTag(tag); writeBytes(value); - this.comma = true; } } @ClassDepends - public void writeFieldValue(int tag, Character[] value) { + public final void writeFieldValue(int tag, Character[] value) { if (value != null && value.length > 0) { writeTag(tag); writeChars(value); - this.comma = true; } } @ClassDepends - public void writeFieldValue(int tag, Short[] value) { + public final void writeFieldValue(int tag, Short[] value) { if (value != null && value.length > 0) { writeTag(tag); writeShorts(value); - this.comma = true; } } @ClassDepends - public void writeFieldValue(int tag, Integer[] value) { + public final void writeFieldValue(int tag, Integer[] value) { if (value != null && value.length > 0) { writeTag(tag); writeInts(value); - this.comma = true; } } @ClassDepends - public void writeFieldValue(int tag, Float[] value) { + public final void writeFieldValue(int tag, Float[] value) { if (value != null && value.length > 0) { writeTag(tag); writeFloats(value); - this.comma = true; } } @ClassDepends - public void writeFieldValue(int tag, Long[] value) { + public final void writeFieldValue(int tag, Long[] value) { if (value != null && value.length > 0) { writeTag(tag); writeLongs(value); - this.comma = true; } } @ClassDepends - public void writeFieldValue(int tag, Double[] value) { + public final void writeFieldValue(int tag, Double[] value) { if (value != null && value.length > 0) { writeTag(tag); writeDoubles(value); - this.comma = true; } } @ClassDepends - public void writeFieldValue(int tag, AtomicInteger[] value) { + public final void writeFieldValue(int tag, AtomicInteger[] value) { if (value != null && value.length > 0) { writeTag(tag); writeAtomicIntegers(value); - this.comma = true; } } @ClassDepends - public void writeFieldValue(int tag, AtomicLong[] value) { + public final void writeFieldValue(int tag, AtomicLong[] value) { if (value != null && value.length > 0) { writeTag(tag); writeAtomicLongs(value); - this.comma = true; } } @ClassDepends - public void writeFieldValue(int tag, String[] value) { + public final void writeFieldValue(int tag, String[] value) { if (value != null && value.length > 0) { writeStrings(tag, value); - this.comma = true; } } @ClassDepends - public void writeFieldBoolsValue(int tag, Collection value) { + public final void writeFieldBoolsValue(int tag, Collection value) { if (value != null && !value.isEmpty()) { writeTag(tag); writeBools(value); - this.comma = true; } } @ClassDepends - public void writeFieldBytesValue(int tag, Collection value) { + public final void writeFieldBytesValue(int tag, Collection value) { if (value != null && !value.isEmpty()) { writeTag(tag); writeBytes(value); - this.comma = true; } } @ClassDepends - public void writeFieldCharsValue(int tag, Collection value) { + public final void writeFieldCharsValue(int tag, Collection value) { if (value != null && !value.isEmpty()) { writeTag(tag); writeChars(value); - this.comma = true; } } @ClassDepends - public void writeFieldShortsValue(int tag, Collection value) { + public final void writeFieldShortsValue(int tag, Collection value) { if (value != null && !value.isEmpty()) { writeTag(tag); writeShorts(value); - this.comma = true; } } @ClassDepends - public void writeFieldIntsValue(int tag, Collection value) { + public final void writeFieldIntsValue(int tag, Collection value) { if (value != null && !value.isEmpty()) { writeTag(tag); writeInts(value); - this.comma = true; } } @ClassDepends - public void writeFieldFloatsValue(int tag, Collection value) { + public final void writeFieldFloatsValue(int tag, Collection value) { if (value != null && !value.isEmpty()) { writeTag(tag); writeFloats(value); - this.comma = true; } } @ClassDepends - public void writeFieldLongsValue(int tag, Collection value) { + public final void writeFieldLongsValue(int tag, Collection value) { if (value != null && !value.isEmpty()) { writeTag(tag); writeLongs(value); - this.comma = true; } } @ClassDepends - public void writeFieldDoublesValue(int tag, Collection value) { + public final void writeFieldDoublesValue(int tag, Collection value) { if (value != null && !value.isEmpty()) { writeTag(tag); writeDoubles(value); - this.comma = true; } } @ClassDepends - public void writeFieldAtomicIntegersValue(int tag, Collection value) { + public final void writeFieldAtomicIntegersValue(int tag, Collection value) { if (value != null && !value.isEmpty()) { writeTag(tag); writeAtomicIntegers(value); - this.comma = true; } } @ClassDepends - public void writeFieldAtomicLongsValue(int tag, Collection value) { + public final void writeFieldAtomicLongsValue(int tag, Collection value) { if (value != null && !value.isEmpty()) { writeTag(tag); writeAtomicLongs(value); - this.comma = true; } } @ClassDepends - public void writeFieldStringsValue(int tag, Collection value) { + public final void writeFieldStringsValue(int tag, Collection value) { if (value != null && !value.isEmpty()) { writeStrings(tag, value); - this.comma = true; } } @ClassDepends - public void writeFieldValue(int tag, String value) { + public final void writeFieldValue(int tag, String value) { if (value != null && (!value.isEmpty() || !tiny())) { writeTag(tag); writeString(value); - this.comma = true; } } @ClassDepends - public void writeFieldValue(int tag, Enum value) { + public final void writeFieldValue(int tag, Enum value) { if (value != null) { writeTag(tag); if (enumtostring) { @@ -1283,22 +1232,20 @@ public class ProtobufWriter extends Writer implements ByteTuple { } else { writeUInt32(value.ordinal()); } - this.comma = true; } } @ClassDepends - public void writeFieldValue(int tag, SimpledCoder encoder, Object value) { + public final void writeFieldValue(int tag, SimpledCoder encoder, Object value) { if (value != null) { writeTag(tag); encoder.convertTo(this, value); - this.comma = true; } } @Override @ClassDepends - public void writeObjectField(final EnMember member, Object obj) { + public final void writeObjectField(final EnMember member, Object obj) { Object value; if (objFieldFunc == null) { value = member.getFieldValue(obj); @@ -1326,11 +1273,10 @@ public class ProtobufWriter extends Writer implements ByteTuple { this.writeFieldName(member); encoder.convertTo(this, value); } - this.comma = true; } @ClassDepends - public void writeTag(int tag) { + public final void writeTag(int tag) { if (tag < 128) { writeTo((byte) tag); } else { @@ -1338,7 +1284,7 @@ public class ProtobufWriter extends Writer implements ByteTuple { } } - protected void writeLength(int value) { + protected final void writeLength(int value) { if (value < 128) { writeTo((byte) value); } else { @@ -1392,7 +1338,7 @@ public class ProtobufWriter extends Writer implements ByteTuple { } } - protected void writeFixed32(int value) { + protected final void writeFixed32(int value) { if (value >= 0 && value < TENTHOUSAND_MAX) { writeTo(TENTHOUSAND_FIXED32_BYTES[value]); return; @@ -1407,7 +1353,7 @@ public class ProtobufWriter extends Writer implements ByteTuple { (byte) ((value >> 24) & 0xFF)); // 3 } - protected void writeFixed64(long value) { + protected final void writeFixed64(long value) { if (value >= 0 && value < TENTHOUSAND_MAX) { writeTo(TENTHOUSAND_FIXED64_BYTES[(int) value]); return; diff --git a/src/test/java/org/redkale/test/convert/GenericEntityTest.java b/src/test/java/org/redkale/test/convert/GenericEntityTest.java index b0faf9695..9161add09 100644 --- a/src/test/java/org/redkale/test/convert/GenericEntityTest.java +++ b/src/test/java/org/redkale/test/convert/GenericEntityTest.java @@ -47,7 +47,7 @@ public class GenericEntityTest { System.out.println("-------------------- runJson1 ---------------------------------"); JsonConvert convert = JsonConvert.root(); GenericEntity bean = createBean(); - String json = convert.convertTo(bean); + String json = convert.convertTo(ENTITY_TYPE, bean); System.out.println(json); System.out.println(convert.convertFrom(ENTITY_TYPE, json).toString()); Assertions.assertEquals(JSON, json); @@ -55,7 +55,6 @@ public class GenericEntityTest { @Test public void runJson2() throws Exception { - System.out.println("-------------------- runJson2 ---------------------------------"); JsonConvert convert = JsonConvert.root(); ByteBuffer in = ConvertHelper.createByteBuffer(createBytes()); GenericEntity bean = convert.convertFrom(ENTITY_TYPE, in); @@ -67,7 +66,6 @@ public class GenericEntityTest { @Test public void runJson3() throws Exception { - System.out.println("-------------------- runJson3 ---------------------------------"); JsonConvert convert = JsonConvert.root(); InputStream in = ConvertHelper.createInputStream(createBytes()); GenericEntity bean = convert.convertFrom(ENTITY_TYPE, in); @@ -82,10 +80,9 @@ public class GenericEntityTest { System.out.println("-------------------- runPb1 ---------------------------------"); ProtobufConvert convert = ProtobufConvert.root(); GenericEntity bean = createBean(); - byte[] bs = convert.convertTo(bean); - Utility.println("proto", bs); + byte[] bs = convert.convertTo(ENTITY_TYPE, bean); + Utility.println("proto0 ", bs); String rs = convert.convertFrom(ENTITY_TYPE, bs).toString(); - System.out.println(); Assertions.assertEquals(JSON, rs); } @@ -94,13 +91,16 @@ public class GenericEntityTest { System.out.println("-------------------- runPb2 ---------------------------------"); ProtobufConvert convert = ProtobufConvert.root(); GenericEntity bean = createBean(); - byte[] bs = convert.convertTo(bean); + byte[] bs = convert.convertTo(ENTITY_TYPE, bean); ByteBuffer in = ConvertHelper.createByteBuffer(bs); GenericEntity rs = convert.convertFrom(ENTITY_TYPE, in); Assertions.assertEquals(JSON, rs.toString()); Supplier out = ConvertHelper.createSupplier(); ByteBuffer[] buffers = convert.convertTo(out, ENTITY_TYPE, rs); - Assertions.assertArrayEquals(bs, ConvertHelper.toBytes(buffers)); + byte[] bs2 = ConvertHelper.toBytes(buffers); + Utility.println("proto1 ", bs); + Utility.println("proto2 ", bs2); + Assertions.assertArrayEquals(bs, bs2); } @Test @@ -108,13 +108,16 @@ public class GenericEntityTest { System.out.println("-------------------- runPb3 ---------------------------------"); ProtobufConvert convert = ProtobufConvert.root(); GenericEntity bean = createBean(); - byte[] bs = convert.convertTo(bean); + byte[] bs = convert.convertTo(ENTITY_TYPE, bean); + Utility.println("proto1 ", bs); InputStream in = ConvertHelper.createInputStream(bs); - // GenericEntity rs = convert.convertFrom(ENTITY_TYPE, in); - // Assertions.assertEquals(JSON, rs.toString()); - // ByteArrayOutputStream out = new ByteArrayOutputStream(); - // convert.convertTo(out, ENTITY_TYPE, rs); - // Assertions.assertArrayEquals(bs, out.toByteArray()); + GenericEntity rs = convert.convertFrom(ENTITY_TYPE, in); + Assertions.assertEquals(JSON, rs.toString()); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + convert.convertTo(out, ENTITY_TYPE, rs); + byte[] bs2 = out.toByteArray(); + Utility.println("proto2 ", bs2); + Assertions.assertArrayEquals(bs, bs2); } @Test @@ -173,26 +176,26 @@ public class GenericEntityTest { public static class GenericEntity { - @ConvertColumn(index = 3) - private K oneName; + @ConvertColumn(index = 1) + private Entry oneEntry; @ConvertColumn(index = 2) private List oneList; - @ConvertColumn(index = 1) - private Entry oneEntry; + @ConvertColumn(index = 3) + private K oneName; @Override public String toString() { return JsonConvert.root().convertTo(this); } - public K getOneName() { - return oneName; + public Entry getOneEntry() { + return oneEntry; } - public void setOneName(K oneName) { - this.oneName = oneName; + public void setOneEntry(Entry oneEntry) { + this.oneEntry = oneEntry; } public List getOneList() { @@ -203,12 +206,12 @@ public class GenericEntityTest { this.oneList = oneList; } - public Entry getOneEntry() { - return oneEntry; + public K getOneName() { + return oneName; } - public void setOneEntry(Entry oneEntry) { - this.oneEntry = oneEntry; + public void setOneName(K oneName) { + this.oneName = oneName; } }