This commit is contained in:
25
src/org/redkale/convert/ConvertMask.java
Normal file
25
src/org/redkale/convert/ConvertMask.java
Normal file
@@ -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接口
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* 详情见: https://redkale.org
|
||||||
|
*
|
||||||
|
* @author zhangjx
|
||||||
|
*/
|
||||||
|
public interface ConvertMask {
|
||||||
|
|
||||||
|
default byte mask(byte value) {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
default byte unmask(byte value) {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -25,7 +25,10 @@ public class BsonByteBufferReader extends BsonReader {
|
|||||||
|
|
||||||
private ByteBuffer currentBuffer;
|
private ByteBuffer currentBuffer;
|
||||||
|
|
||||||
protected BsonByteBufferReader(ByteBuffer... buffers) {
|
protected ConvertMask mask;
|
||||||
|
|
||||||
|
protected BsonByteBufferReader(ConvertMask mask, ByteBuffer... buffers) {
|
||||||
|
this.mask = mask;
|
||||||
this.buffers = buffers;
|
this.buffers = buffers;
|
||||||
if (buffers != null && buffers.length > 0) this.currentBuffer = buffers[currentIndex];
|
if (buffers != null && buffers.length > 0) this.currentBuffer = buffers[currentIndex];
|
||||||
}
|
}
|
||||||
@@ -36,12 +39,13 @@ public class BsonByteBufferReader extends BsonReader {
|
|||||||
this.currentIndex = 0;
|
this.currentIndex = 0;
|
||||||
this.currentBuffer = null;
|
this.currentBuffer = null;
|
||||||
this.buffers = null;
|
this.buffers = null;
|
||||||
|
this.mask = null;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected byte currentByte() {
|
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() {
|
public byte readByte() {
|
||||||
if (this.currentBuffer.hasRemaining()) {
|
if (this.currentBuffer.hasRemaining()) {
|
||||||
this.position++;
|
this.position++;
|
||||||
return this.currentBuffer.get();
|
return mask == null ? this.currentBuffer.get() : mask.unmask(this.currentBuffer.get());
|
||||||
}
|
}
|
||||||
for (;;) {
|
for (;;) {
|
||||||
this.currentBuffer = this.buffers[++this.currentIndex];
|
this.currentBuffer = this.buffers[++this.currentIndex];
|
||||||
if (this.currentBuffer.hasRemaining()) {
|
if (this.currentBuffer.hasRemaining()) {
|
||||||
this.position++;
|
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();
|
int remain = this.currentBuffer.remaining();
|
||||||
if (remain >= 2) {
|
if (remain >= 2) {
|
||||||
this.position += 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()));
|
return (char) ((0xff00 & (readByte() << 8)) | (0xff & readByte()));
|
||||||
@@ -96,7 +104,11 @@ public class BsonByteBufferReader extends BsonReader {
|
|||||||
int remain = this.currentBuffer.remaining();
|
int remain = this.currentBuffer.remaining();
|
||||||
if (remain >= 2) {
|
if (remain >= 2) {
|
||||||
this.position += 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()));
|
return (short) ((0xff00 & (readByte() << 8)) | (0xff & readByte()));
|
||||||
@@ -108,7 +120,14 @@ public class BsonByteBufferReader extends BsonReader {
|
|||||||
int remain = this.currentBuffer.remaining();
|
int remain = this.currentBuffer.remaining();
|
||||||
if (remain >= 4) {
|
if (remain >= 4) {
|
||||||
this.position += 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);
|
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();
|
int remain = this.currentBuffer.remaining();
|
||||||
if (remain >= 8) {
|
if (remain >= 8) {
|
||||||
this.position += 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)
|
return ((((long) readByte() & 0xff) << 56)
|
||||||
@@ -150,9 +180,19 @@ public class BsonByteBufferReader extends BsonReader {
|
|||||||
if (remain >= len) {
|
if (remain >= len) {
|
||||||
this.position += len;
|
this.position += len;
|
||||||
this.currentBuffer.get(bs, pos, 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;
|
return;
|
||||||
}
|
}
|
||||||
this.currentBuffer.get(bs, pos, remain);
|
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.position += remain;
|
||||||
this.currentBuffer = this.buffers[++this.currentIndex];
|
this.currentBuffer = this.buffers[++this.currentIndex];
|
||||||
read(bs, pos + remain);
|
read(bs, pos + remain);
|
||||||
|
|||||||
@@ -61,7 +61,7 @@ public final class BsonConvert extends Convert<BsonReader, BsonWriter> {
|
|||||||
|
|
||||||
//------------------------------ reader -----------------------------------------------------------
|
//------------------------------ reader -----------------------------------------------------------
|
||||||
public BsonReader pollBsonReader(final ByteBuffer... buffers) {
|
public BsonReader pollBsonReader(final ByteBuffer... buffers) {
|
||||||
return new BsonByteBufferReader(buffers);
|
return new BsonByteBufferReader((ConvertMask) null, buffers);
|
||||||
}
|
}
|
||||||
|
|
||||||
public BsonReader pollBsonReader(final InputStream in) {
|
public BsonReader pollBsonReader(final InputStream in) {
|
||||||
@@ -116,7 +116,12 @@ public final class BsonConvert extends Convert<BsonReader, BsonWriter> {
|
|||||||
|
|
||||||
public <T> T convertFrom(final Type type, final ByteBuffer... buffers) {
|
public <T> T convertFrom(final Type type, final ByteBuffer... buffers) {
|
||||||
if (type == null || buffers.length < 1) return null;
|
if (type == null || buffers.length < 1) return null;
|
||||||
return (T) factory.loadDecoder(type).convertFrom(new BsonByteBufferReader(buffers));
|
return (T) factory.loadDecoder(type).convertFrom(new BsonByteBufferReader((ConvertMask) null, buffers));
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> T convertFrom(final Type type, final ConvertMask mask, final ByteBuffer... buffers) {
|
||||||
|
if (type == null || buffers.length < 1) return null;
|
||||||
|
return (T) factory.loadDecoder(type).convertFrom(new BsonByteBufferReader(mask, buffers));
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T> T convertFrom(final Type type, final BsonReader reader) {
|
public <T> T convertFrom(final Type type, final BsonReader reader) {
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ class BsonStreamReader extends BsonByteBufferReader {
|
|||||||
private byte currByte;
|
private byte currByte;
|
||||||
|
|
||||||
protected BsonStreamReader(InputStream in) {
|
protected BsonStreamReader(InputStream in) {
|
||||||
|
super((ConvertMask) null);
|
||||||
this.in = in;
|
this.in = in;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -29,7 +29,10 @@ public class JsonByteBufferReader extends JsonReader {
|
|||||||
|
|
||||||
private ByteBuffer currentBuffer;
|
private ByteBuffer currentBuffer;
|
||||||
|
|
||||||
protected JsonByteBufferReader(ByteBuffer... buffers) {
|
protected ConvertMask mask;
|
||||||
|
|
||||||
|
protected JsonByteBufferReader(ConvertMask mask, ByteBuffer... buffers) {
|
||||||
|
this.mask = mask;
|
||||||
this.buffers = buffers;
|
this.buffers = buffers;
|
||||||
if (buffers != null && buffers.length > 0) this.currentBuffer = buffers[currentIndex];
|
if (buffers != null && buffers.length > 0) this.currentBuffer = buffers[currentIndex];
|
||||||
}
|
}
|
||||||
@@ -41,19 +44,20 @@ public class JsonByteBufferReader extends JsonReader {
|
|||||||
this.currentChar = 0;
|
this.currentChar = 0;
|
||||||
this.currentBuffer = null;
|
this.currentBuffer = null;
|
||||||
this.buffers = null;
|
this.buffers = null;
|
||||||
|
this.mask = null;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected byte nextByte() {
|
protected byte nextByte() {
|
||||||
if (this.currentBuffer.hasRemaining()) {
|
if (this.currentBuffer.hasRemaining()) {
|
||||||
this.position++;
|
this.position++;
|
||||||
return this.currentBuffer.get();
|
return mask == null ? this.currentBuffer.get() : mask.unmask(this.currentBuffer.get());
|
||||||
}
|
}
|
||||||
for (;;) {
|
for (;;) {
|
||||||
this.currentBuffer = this.buffers[++this.currentIndex];
|
this.currentBuffer = this.buffers[++this.currentIndex];
|
||||||
if (this.currentBuffer.hasRemaining()) {
|
if (this.currentBuffer.hasRemaining()) {
|
||||||
this.position++;
|
this.position++;
|
||||||
return this.currentBuffer.get();
|
return mask == null ? this.currentBuffer.get() : mask.unmask(this.currentBuffer.get());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ public final class JsonConvert extends Convert<JsonReader, JsonWriter> {
|
|||||||
|
|
||||||
//------------------------------ reader -----------------------------------------------------------
|
//------------------------------ reader -----------------------------------------------------------
|
||||||
public JsonReader pollJsonReader(final ByteBuffer... buffers) {
|
public JsonReader pollJsonReader(final ByteBuffer... buffers) {
|
||||||
return new JsonByteBufferReader(buffers);
|
return new JsonByteBufferReader((ConvertMask) null, buffers);
|
||||||
}
|
}
|
||||||
|
|
||||||
public JsonReader pollJsonReader(final InputStream in) {
|
public JsonReader pollJsonReader(final InputStream in) {
|
||||||
@@ -111,7 +111,12 @@ public final class JsonConvert extends Convert<JsonReader, JsonWriter> {
|
|||||||
|
|
||||||
public <T> T convertFrom(final Type type, final ByteBuffer... buffers) {
|
public <T> T convertFrom(final Type type, final ByteBuffer... buffers) {
|
||||||
if (type == null || buffers == null || buffers.length == 0) return null;
|
if (type == null || buffers == null || buffers.length == 0) return null;
|
||||||
return (T) factory.loadDecoder(type).convertFrom(new JsonByteBufferReader(buffers));
|
return (T) factory.loadDecoder(type).convertFrom(new JsonByteBufferReader((ConvertMask) null, buffers));
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> T convertFrom(final Type type, final ConvertMask mask, final ByteBuffer... buffers) {
|
||||||
|
if (type == null || buffers == null || buffers.length == 0) return null;
|
||||||
|
return (T) factory.loadDecoder(type).convertFrom(new JsonByteBufferReader(mask, buffers));
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T> T convertFrom(final Type type, final JsonReader reader) {
|
public <T> T convertFrom(final Type type, final JsonReader reader) {
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ class JsonStreamReader extends JsonByteBufferReader {
|
|||||||
private InputStream in;
|
private InputStream in;
|
||||||
|
|
||||||
protected JsonStreamReader(InputStream in) {
|
protected JsonStreamReader(InputStream in) {
|
||||||
|
super((ConvertMask) null);
|
||||||
this.in = in;
|
this.in = in;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -178,6 +178,23 @@ public final class WebSocketPacket {
|
|||||||
return buffers;
|
return buffers;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* <blockquote><pre>
|
||||||
|
* 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);
|
||||||
|
* }
|
||||||
|
* </pre></blockquote>
|
||||||
|
*/
|
||||||
/**
|
/**
|
||||||
* 消息解码 <br>
|
* 消息解码 <br>
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user