diff --git a/src/org/redkale/convert/ConvertMask.java b/src/org/redkale/convert/ConvertMask.java new file mode 100644 index 000000000..11829f57f --- /dev/null +++ b/src/org/redkale/convert/ConvertMask.java @@ -0,0 +1,25 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package org.redkale.convert; + +/** + * Mask接口 + * + *
+ * 详情见: https://redkale.org
+ *
+ * @author zhangjx
+ */
+public interface ConvertMask {
+
+ default byte mask(byte value) {
+ return value;
+ }
+
+ default byte unmask(byte value) {
+ return value;
+ }
+}
diff --git a/src/org/redkale/convert/bson/BsonByteBufferReader.java b/src/org/redkale/convert/bson/BsonByteBufferReader.java
index 7aae4d143..4dbdef5b4 100644
--- a/src/org/redkale/convert/bson/BsonByteBufferReader.java
+++ b/src/org/redkale/convert/bson/BsonByteBufferReader.java
@@ -25,7 +25,10 @@ public class BsonByteBufferReader extends BsonReader {
private ByteBuffer currentBuffer;
- protected BsonByteBufferReader(ByteBuffer... buffers) {
+ protected ConvertMask mask;
+
+ protected BsonByteBufferReader(ConvertMask mask, ByteBuffer... buffers) {
+ this.mask = mask;
this.buffers = buffers;
if (buffers != null && buffers.length > 0) this.currentBuffer = buffers[currentIndex];
}
@@ -36,12 +39,13 @@ public class BsonByteBufferReader extends BsonReader {
this.currentIndex = 0;
this.currentBuffer = null;
this.buffers = null;
+ this.mask = null;
return false;
}
@Override
protected byte currentByte() {
- return currentBuffer.get(currentBuffer.position());
+ return mask == null ? currentBuffer.get(currentBuffer.position()) : mask.unmask(currentBuffer.get(currentBuffer.position()));
}
/**
@@ -67,13 +71,13 @@ public class BsonByteBufferReader extends BsonReader {
public byte readByte() {
if (this.currentBuffer.hasRemaining()) {
this.position++;
- return this.currentBuffer.get();
+ return mask == null ? this.currentBuffer.get() : mask.unmask(this.currentBuffer.get());
}
for (;;) {
this.currentBuffer = this.buffers[++this.currentIndex];
if (this.currentBuffer.hasRemaining()) {
this.position++;
- return this.currentBuffer.get();
+ return mask == null ? this.currentBuffer.get() : mask.unmask(this.currentBuffer.get());
}
}
}
@@ -84,7 +88,11 @@ public class BsonByteBufferReader extends BsonReader {
int remain = this.currentBuffer.remaining();
if (remain >= 2) {
this.position += 2;
- return this.currentBuffer.getChar();
+ if (mask == null) {
+ return this.currentBuffer.getChar();
+ } else {
+ return (char) ((0xff00 & (mask.unmask(this.currentBuffer.get()) << 8)) | (0xff & mask.unmask(this.currentBuffer.get())));
+ }
}
}
return (char) ((0xff00 & (readByte() << 8)) | (0xff & readByte()));
@@ -96,7 +104,11 @@ public class BsonByteBufferReader extends BsonReader {
int remain = this.currentBuffer.remaining();
if (remain >= 2) {
this.position += 2;
- return this.currentBuffer.getShort();
+ if (mask == null) {
+ return this.currentBuffer.getShort();
+ } else {
+ return (short) ((0xff00 & (mask.unmask(this.currentBuffer.get()) << 8)) | (0xff & mask.unmask(this.currentBuffer.get())));
+ }
}
}
return (short) ((0xff00 & (readByte() << 8)) | (0xff & readByte()));
@@ -108,7 +120,14 @@ public class BsonByteBufferReader extends BsonReader {
int remain = this.currentBuffer.remaining();
if (remain >= 4) {
this.position += 4;
- return this.currentBuffer.getInt();
+ if (mask == null) {
+ return this.currentBuffer.getInt();
+ } else {
+ return ((mask.unmask(this.currentBuffer.get()) & 0xff) << 24)
+ | ((mask.unmask(this.currentBuffer.get()) & 0xff) << 16)
+ | ((mask.unmask(this.currentBuffer.get()) & 0xff) << 8)
+ | (mask.unmask(this.currentBuffer.get()) & 0xff);
+ }
}
}
return ((readByte() & 0xff) << 24) | ((readByte() & 0xff) << 16) | ((readByte() & 0xff) << 8) | (readByte() & 0xff);
@@ -120,7 +139,18 @@ public class BsonByteBufferReader extends BsonReader {
int remain = this.currentBuffer.remaining();
if (remain >= 8) {
this.position += 8;
- return this.currentBuffer.getLong();
+ if (mask == null) {
+ return this.currentBuffer.getLong();
+ } else {
+ return ((((long) mask.unmask(this.currentBuffer.get()) & 0xff) << 56)
+ | (((long) mask.unmask(this.currentBuffer.get()) & 0xff) << 48)
+ | (((long) mask.unmask(this.currentBuffer.get()) & 0xff) << 40)
+ | (((long) mask.unmask(this.currentBuffer.get()) & 0xff) << 32)
+ | (((long) mask.unmask(this.currentBuffer.get()) & 0xff) << 24)
+ | (((long) mask.unmask(this.currentBuffer.get()) & 0xff) << 16)
+ | (((long) mask.unmask(this.currentBuffer.get()) & 0xff) << 8)
+ | (((long) mask.unmask(this.currentBuffer.get()) & 0xff)));
+ }
}
}
return ((((long) readByte() & 0xff) << 56)
@@ -150,9 +180,19 @@ public class BsonByteBufferReader extends BsonReader {
if (remain >= len) {
this.position += len;
this.currentBuffer.get(bs, pos, len);
+ if (mask != null) {
+ for (int i = pos, end = pos + len; i < end; i++) {
+ bs[i] = mask.unmask(bs[i]);
+ }
+ }
return;
}
this.currentBuffer.get(bs, pos, remain);
+ if (mask != null) {
+ for (int i = pos, end = pos + remain; i < end; i++) {
+ bs[i] = mask.unmask(bs[i]);
+ }
+ }
this.position += remain;
this.currentBuffer = this.buffers[++this.currentIndex];
read(bs, pos + remain);
diff --git a/src/org/redkale/convert/bson/BsonConvert.java b/src/org/redkale/convert/bson/BsonConvert.java
index e0e862ab5..d59e0ea98 100644
--- a/src/org/redkale/convert/bson/BsonConvert.java
+++ b/src/org/redkale/convert/bson/BsonConvert.java
@@ -61,7 +61,7 @@ public final class BsonConvert extends Convert
+ */
/**
* 消息解码
+ * public static void main(String[] args) throws Throwable {
+ * byte[] mask = new byte[]{(byte) 0x8f, (byte) 0xf8, (byte) 0x6d, (byte) 0x94};
+ * ByteBuffer buffer = ByteBuffer.wrap(new byte[]{(byte) 0x67, (byte) 0x47, (byte) 0xf4, (byte) 0x70, (byte) 0x37, (byte) 0x52, (byte) 0x8b, (byte) 0x0c, (byte) 0x20, (byte) 0x1e, (byte) 0xdb, (byte) 0x1c, (byte) 0x69, (byte) 0x79, (byte) 0xc2});
+ * ConvertMask masker = new ConvertMask() {
+ * private int index = 0;
+ * public byte unmask(byte value) {
+ * return (byte) (value ^ mask[index++ % 4]);
+ * }
+ * };
+ * String rs = JsonConvert.root().convertFrom(String.class, masker, buffer);
+ * System.out.println(rs);
+ * }
+ *
*