移除ConvertMask功能
This commit is contained in:
@@ -89,8 +89,6 @@ public abstract class Convert<R extends Reader, W extends Writer> {
|
|||||||
|
|
||||||
public abstract <T> T convertFrom(final Type type, final ByteBuffer... buffers);
|
public abstract <T> T convertFrom(final Type type, final ByteBuffer... buffers);
|
||||||
|
|
||||||
public abstract <T> T convertFrom(final Type type, final ConvertMask mask, final ByteBuffer... buffers);
|
|
||||||
|
|
||||||
public final void convertTo(final W writer, final Object value) {
|
public final void convertTo(final W writer, final Object value) {
|
||||||
convertTo(writer, (Type) null, value);
|
convertTo(writer, (Type) null, value);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,25 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -26,10 +26,7 @@ public class BsonByteBufferReader extends BsonReader {
|
|||||||
|
|
||||||
private ByteBuffer currentBuffer;
|
private ByteBuffer currentBuffer;
|
||||||
|
|
||||||
protected ConvertMask mask;
|
protected BsonByteBufferReader(ByteBuffer... buffers) {
|
||||||
|
|
||||||
protected BsonByteBufferReader(ConvertMask mask, ByteBuffer... buffers) {
|
|
||||||
this.mask = mask;
|
|
||||||
this.buffers = buffers;
|
this.buffers = buffers;
|
||||||
if (buffers != null && buffers.length > 0) {
|
if (buffers != null && buffers.length > 0) {
|
||||||
this.currentBuffer = buffers[currentIndex];
|
this.currentBuffer = buffers[currentIndex];
|
||||||
@@ -42,13 +39,12 @@ 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 mask == null ? currentBuffer.get(currentBuffer.position()) : mask.unmask(currentBuffer.get(currentBuffer.position()));
|
return currentBuffer.get(currentBuffer.position());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -102,13 +98,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 mask == null ? this.currentBuffer.get() : mask.unmask(this.currentBuffer.get());
|
return 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 mask == null ? this.currentBuffer.get() : mask.unmask(this.currentBuffer.get());
|
return this.currentBuffer.get();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -119,11 +115,7 @@ 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;
|
||||||
if (mask == null) {
|
return this.currentBuffer.getChar();
|
||||||
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()));
|
||||||
@@ -135,11 +127,7 @@ 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;
|
||||||
if (mask == null) {
|
return this.currentBuffer.getShort();
|
||||||
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()));
|
||||||
@@ -151,14 +139,7 @@ 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;
|
||||||
if (mask == null) {
|
return this.currentBuffer.getInt();
|
||||||
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);
|
||||||
@@ -170,18 +151,7 @@ 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;
|
||||||
if (mask == null) {
|
return this.currentBuffer.getLong();
|
||||||
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)
|
||||||
@@ -211,19 +181,9 @@ 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);
|
||||||
|
|||||||
@@ -86,7 +86,7 @@ public class BsonConvert extends BinaryConvert<BsonReader, BsonWriter> {
|
|||||||
|
|
||||||
//------------------------------ reader -----------------------------------------------------------
|
//------------------------------ reader -----------------------------------------------------------
|
||||||
public BsonReader pollReader(final ByteBuffer... buffers) {
|
public BsonReader pollReader(final ByteBuffer... buffers) {
|
||||||
return new BsonByteBufferReader((ConvertMask) null, buffers);
|
return new BsonByteBufferReader(buffers);
|
||||||
}
|
}
|
||||||
|
|
||||||
public BsonReader pollReader(final InputStream in) {
|
public BsonReader pollReader(final InputStream in) {
|
||||||
@@ -175,16 +175,7 @@ public class BsonConvert extends BinaryConvert<BsonReader, BsonWriter> {
|
|||||||
if (type == null || buffers.length < 1) {
|
if (type == null || buffers.length < 1) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return (T) factory.loadDecoder(type).convertFrom(new BsonByteBufferReader((ConvertMask) null, buffers));
|
return (T) factory.loadDecoder(type).convertFrom(new BsonByteBufferReader(buffers));
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
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));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ class BsonStreamReader extends BsonByteBufferReader {
|
|||||||
private byte currByte;
|
private byte currByte;
|
||||||
|
|
||||||
protected BsonStreamReader(InputStream in) {
|
protected BsonStreamReader(InputStream in) {
|
||||||
super((ConvertMask) null);
|
super();
|
||||||
this.in = in;
|
this.in = in;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -29,10 +29,7 @@ public class JsonByteBufferReader extends JsonReader {
|
|||||||
|
|
||||||
private ByteBuffer currentBuffer;
|
private ByteBuffer currentBuffer;
|
||||||
|
|
||||||
protected ConvertMask mask;
|
protected JsonByteBufferReader(ByteBuffer... buffers) {
|
||||||
|
|
||||||
protected JsonByteBufferReader(ConvertMask mask, ByteBuffer... buffers) {
|
|
||||||
this.mask = mask;
|
|
||||||
this.buffers = buffers;
|
this.buffers = buffers;
|
||||||
if (buffers != null && buffers.length > 0) {
|
if (buffers != null && buffers.length > 0) {
|
||||||
this.currentBuffer = buffers[currentIndex];
|
this.currentBuffer = buffers[currentIndex];
|
||||||
@@ -46,20 +43,19 @@ public class JsonByteBufferReader extends JsonReader {
|
|||||||
this.buffers = null;
|
this.buffers = null;
|
||||||
this.currentIndex = 0;
|
this.currentIndex = 0;
|
||||||
this.currentBuffer = null;
|
this.currentBuffer = 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 mask == null ? this.currentBuffer.get() : mask.unmask(this.currentBuffer.get());
|
return 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 mask == null ? this.currentBuffer.get() : mask.unmask(this.currentBuffer.get());
|
return this.currentBuffer.get();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -207,20 +207,7 @@ public class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
|||||||
decoder = factory.loadDecoder(type);
|
decoder = factory.loadDecoder(type);
|
||||||
this.lastConvertDecodeable = decoder;
|
this.lastConvertDecodeable = decoder;
|
||||||
}
|
}
|
||||||
return (T) decoder.convertFrom(new JsonByteBufferReader((ConvertMask) null, buffers));
|
return (T) decoder.convertFrom(new JsonByteBufferReader(buffers));
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public <T> T convertFrom(final Type type, final ConvertMask mask, final ByteBuffer... buffers) {
|
|
||||||
if (type == null || buffers == null || buffers.length == 0) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
Decodeable decoder = this.lastConvertDecodeable;
|
|
||||||
if (decoder == null || decoder.getType() != type) {
|
|
||||||
decoder = factory.loadDecoder(type);
|
|
||||||
this.lastConvertDecodeable = decoder;
|
|
||||||
}
|
|
||||||
return (T) decoder.convertFrom(new JsonByteBufferReader(mask, buffers));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -279,15 +266,7 @@ public class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
|||||||
if (buffers == null || buffers.length == 0) {
|
if (buffers == null || buffers.length == 0) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return (V) new AnyDecoder(factory).convertFrom(new JsonByteBufferReader((ConvertMask) null, buffers));
|
return (V) new AnyDecoder(factory).convertFrom(new JsonByteBufferReader(buffers));
|
||||||
}
|
|
||||||
|
|
||||||
//返回非null的值是由String、ArrayList、HashMap任意组合的对象
|
|
||||||
public <V> V convertFrom(final ConvertMask mask, final ByteBuffer... buffers) {
|
|
||||||
if (buffers == null || buffers.length == 0) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return (V) new AnyDecoder(factory).convertFrom(new JsonByteBufferReader(mask, buffers));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//返回非null的值是由String、ArrayList、HashMap任意组合的对象
|
//返回非null的值是由String、ArrayList、HashMap任意组合的对象
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ class JsonStreamReader extends JsonByteBufferReader {
|
|||||||
private InputStream in;
|
private InputStream in;
|
||||||
|
|
||||||
protected JsonStreamReader(InputStream in) {
|
protected JsonStreamReader(InputStream in) {
|
||||||
super((ConvertMask) null);
|
super();
|
||||||
this.in = in;
|
this.in = in;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,6 @@
|
|||||||
package org.redkale.convert.protobuf;
|
package org.redkale.convert.protobuf;
|
||||||
|
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import org.redkale.convert.*;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@@ -20,10 +19,7 @@ public class ProtobufByteBufferReader extends ProtobufReader {
|
|||||||
|
|
||||||
private ByteBuffer currentBuffer;
|
private ByteBuffer currentBuffer;
|
||||||
|
|
||||||
protected ConvertMask mask;
|
protected ProtobufByteBufferReader(ByteBuffer... buffers) {
|
||||||
|
|
||||||
protected ProtobufByteBufferReader(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];
|
||||||
}
|
}
|
||||||
@@ -34,25 +30,24 @@ public class ProtobufByteBufferReader extends ProtobufReader {
|
|||||||
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 mask == null ? currentBuffer.get(currentBuffer.position()) : mask.unmask(currentBuffer.get(currentBuffer.position()));
|
return currentBuffer.get(currentBuffer.position());
|
||||||
}
|
}
|
||||||
|
|
||||||
protected byte nextByte() {
|
protected byte nextByte() {
|
||||||
if (this.currentBuffer.hasRemaining()) {
|
if (this.currentBuffer.hasRemaining()) {
|
||||||
this.position++;
|
this.position++;
|
||||||
return mask == null ? this.currentBuffer.get() : mask.unmask(this.currentBuffer.get());
|
return 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 mask == null ? this.currentBuffer.get() : mask.unmask(this.currentBuffer.get());
|
return this.currentBuffer.get();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -534,27 +534,7 @@ public class ProtobufConvert extends BinaryConvert<ProtobufReader, ProtobufWrite
|
|||||||
if (!(decoder instanceof ObjectDecoder)) {
|
if (!(decoder instanceof ObjectDecoder)) {
|
||||||
throw new ConvertException(this.getClass().getSimpleName() + " not supported type(" + type + ")");
|
throw new ConvertException(this.getClass().getSimpleName() + " not supported type(" + type + ")");
|
||||||
}
|
}
|
||||||
return (T) decoder.convertFrom(new ProtobufByteBufferReader((ConvertMask) null, buffers));
|
return (T) decoder.convertFrom(new ProtobufByteBufferReader(buffers));
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
public <T> T convertFrom(final Type type, final ConvertMask mask, final ByteBuffer... buffers) {
|
|
||||||
if (true) {
|
|
||||||
throw new ConvertException(this.getClass().getSimpleName() + " not supported convertFrom ByteBuffer");
|
|
||||||
}
|
|
||||||
if (type == null || buffers.length < 1) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
Decodeable decoder = this.lastConvertDecodeable;
|
|
||||||
if (decoder == null || decoder.getType() != type) {
|
|
||||||
decoder = factory.loadDecoder(type);
|
|
||||||
this.lastConvertDecodeable = decoder;
|
|
||||||
}
|
|
||||||
if (!(decoder instanceof ObjectDecoder)) {
|
|
||||||
throw new ConvertException(this.getClass().getSimpleName() + " not supported type(" + type + ")");
|
|
||||||
}
|
|
||||||
return (T) decoder.convertFrom(new ProtobufByteBufferReader(mask, buffers));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ class ProtobufStreamReader extends ProtobufByteBufferReader {
|
|||||||
private byte currByte;
|
private byte currByte;
|
||||||
|
|
||||||
protected ProtobufStreamReader(InputStream in) {
|
protected ProtobufStreamReader(InputStream in) {
|
||||||
super((ConvertMask) null);
|
super();
|
||||||
this.in = in;
|
this.in = in;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user