This commit is contained in:
Redkale
2019-03-14 13:47:57 +08:00
parent 30c9be303f
commit 2a19ea709b
5 changed files with 29 additions and 82 deletions

View File

@@ -37,8 +37,6 @@ public abstract class Convert<R extends Reader, W extends Writer> {
public abstract <T> T convertFrom(final Type type, final ConvertMask mask, final ByteBuffer... buffers); public abstract <T> T convertFrom(final Type type, final ConvertMask mask, final ByteBuffer... buffers);
public abstract <T> T convertFrom(final Type type, final ConvertMask[] masks, final ByteBuffer... buffers);
public abstract ByteBuffer[] convertTo(final Supplier<ByteBuffer> supplier, final Object value); public abstract ByteBuffer[] convertTo(final Supplier<ByteBuffer> supplier, final Object value);
public abstract ByteBuffer[] convertTo(final Supplier<ByteBuffer> supplier, final Type type, final Object value); public abstract ByteBuffer[] convertTo(final Supplier<ByteBuffer> supplier, final Type type, final Object value);

View File

@@ -26,16 +26,7 @@ public class BsonByteBufferReader extends BsonReader {
private ByteBuffer currentBuffer; private ByteBuffer currentBuffer;
protected ConvertMask mask; //mask二选一 protected ConvertMask mask;
protected ConvertMask[] masks; //mask二选一
protected BsonByteBufferReader(ConvertMask[] masks, ByteBuffer... buffers) {
this.masks = masks;
this.buffers = buffers;
if (buffers != null && buffers.length > 0) this.currentBuffer = buffers[currentIndex];
if (this.masks.length != buffers.length) throw new RuntimeException("masks.length must equals buffers.length");
}
protected BsonByteBufferReader(ConvertMask mask, ByteBuffer... buffers) { protected BsonByteBufferReader(ConvertMask mask, ByteBuffer... buffers) {
this.mask = mask; this.mask = mask;
@@ -50,15 +41,12 @@ public class BsonByteBufferReader extends BsonReader {
this.currentBuffer = null; this.currentBuffer = null;
this.buffers = null; this.buffers = null;
this.mask = null; this.mask = null;
this.masks = null;
return false; return false;
} }
@Override @Override
protected byte currentByte() { protected byte currentByte() {
if (mask != null) return mask.unmask(currentBuffer.get(currentBuffer.position())); return mask == null ? currentBuffer.get(currentBuffer.position()) : mask.unmask(currentBuffer.get(currentBuffer.position()));
if (masks != null) return masks[this.currentIndex].unmask(currentBuffer.get(currentBuffer.position()));
return currentBuffer.get(currentBuffer.position());
} }
@Override @Override
@@ -97,12 +85,6 @@ public class BsonByteBufferReader extends BsonReader {
} }
//------------------------------------------------------------ //------------------------------------------------------------
protected byte readMaskByte() {
if (mask != null) return mask.unmask(this.currentBuffer.get());
if (masks != null) return masks[this.currentIndex].unmask(this.currentBuffer.get());
return this.currentBuffer.get();
}
@Override @Override
public final boolean readBoolean() { public final boolean readBoolean() {
return readByte() == 1; return readByte() == 1;
@@ -112,17 +94,13 @@ public class BsonByteBufferReader extends BsonReader {
public byte readByte() { public byte readByte() {
if (this.currentBuffer.hasRemaining()) { if (this.currentBuffer.hasRemaining()) {
this.position++; this.position++;
if (mask != null) return mask.unmask(this.currentBuffer.get()); return mask == null ? this.currentBuffer.get() : mask.unmask(this.currentBuffer.get());
if (masks != null) return masks[this.currentIndex].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++;
if (mask != null) return mask.unmask(this.currentBuffer.get()); return mask == null ? this.currentBuffer.get() : mask.unmask(this.currentBuffer.get());
if (masks != null) return masks[this.currentIndex].unmask(this.currentBuffer.get());
return this.currentBuffer.get();
} }
} }
} }
@@ -133,10 +111,10 @@ 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 && masks == null) { if (mask == null) {
return this.currentBuffer.getChar(); return this.currentBuffer.getChar();
} else { } else {
return (char) ((0xff00 & (readMaskByte() << 8)) | (0xff & readMaskByte())); return (char) ((0xff00 & (mask.unmask(this.currentBuffer.get()) << 8)) | (0xff & mask.unmask(this.currentBuffer.get())));
} }
} }
} }
@@ -149,10 +127,10 @@ 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 && masks == null) { if (mask == null) {
return this.currentBuffer.getShort(); return this.currentBuffer.getShort();
} else { } else {
return (short) ((0xff00 & (readMaskByte() << 8)) | (0xff & readMaskByte())); return (short) ((0xff00 & (mask.unmask(this.currentBuffer.get()) << 8)) | (0xff & mask.unmask(this.currentBuffer.get())));
} }
} }
} }
@@ -165,13 +143,13 @@ 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 && masks == null) { if (mask == null) {
return this.currentBuffer.getInt(); return this.currentBuffer.getInt();
} else { } else {
return ((readMaskByte() & 0xff) << 24) return ((mask.unmask(this.currentBuffer.get()) & 0xff) << 24)
| ((readMaskByte() & 0xff) << 16) | ((mask.unmask(this.currentBuffer.get()) & 0xff) << 16)
| ((readMaskByte() & 0xff) << 8) | ((mask.unmask(this.currentBuffer.get()) & 0xff) << 8)
| (readMaskByte() & 0xff); | (mask.unmask(this.currentBuffer.get()) & 0xff);
} }
} }
} }
@@ -184,17 +162,17 @@ 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 && masks == null) { if (mask == null) {
return this.currentBuffer.getLong(); return this.currentBuffer.getLong();
} else { } else {
return ((((long) readMaskByte() & 0xff) << 56) return ((((long) mask.unmask(this.currentBuffer.get()) & 0xff) << 56)
| (((long) readMaskByte() & 0xff) << 48) | (((long) mask.unmask(this.currentBuffer.get()) & 0xff) << 48)
| (((long) readMaskByte() & 0xff) << 40) | (((long) mask.unmask(this.currentBuffer.get()) & 0xff) << 40)
| (((long) readMaskByte() & 0xff) << 32) | (((long) mask.unmask(this.currentBuffer.get()) & 0xff) << 32)
| (((long) readMaskByte() & 0xff) << 24) | (((long) mask.unmask(this.currentBuffer.get()) & 0xff) << 24)
| (((long) readMaskByte() & 0xff) << 16) | (((long) mask.unmask(this.currentBuffer.get()) & 0xff) << 16)
| (((long) readMaskByte() & 0xff) << 8) | (((long) mask.unmask(this.currentBuffer.get()) & 0xff) << 8)
| (((long) readMaskByte() & 0xff))); | (((long) mask.unmask(this.currentBuffer.get()) & 0xff)));
} }
} }
} }
@@ -222,22 +200,20 @@ public class BsonByteBufferReader extends BsonReader {
return; return;
} }
int len = bs.length - pos; int len = bs.length - pos;
ConvertMask oneMask = mask;
if (masks != null) oneMask = masks[this.currentIndex];
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 (oneMask != null) { if (mask != null) {
for (int i = pos, end = pos + len; i < end; i++) { for (int i = pos, end = pos + len; i < end; i++) {
bs[i] = oneMask.unmask(bs[i]); bs[i] = mask.unmask(bs[i]);
} }
} }
return; return;
} }
this.currentBuffer.get(bs, pos, remain); this.currentBuffer.get(bs, pos, remain);
if (oneMask != null) { if (mask != null) {
for (int i = pos, end = pos + remain; i < end; i++) { for (int i = pos, end = pos + remain; i < end; i++) {
bs[i] = oneMask.unmask(bs[i]); bs[i] = mask.unmask(bs[i]);
} }
} }
this.position += remain; this.position += remain;

View File

@@ -130,13 +130,6 @@ public final class BsonConvert extends BinaryConvert<BsonReader, BsonWriter> {
return (T) factory.loadDecoder(type).convertFrom(new BsonByteBufferReader(mask, buffers)); return (T) factory.loadDecoder(type).convertFrom(new BsonByteBufferReader(mask, buffers));
} }
@Override
@SuppressWarnings("unchecked")
public <T> T convertFrom(final Type type, final ConvertMask[] masks, final ByteBuffer... buffers) {
if (type == null || buffers.length < 1) return null;
return (T) factory.loadDecoder(type).convertFrom(new BsonByteBufferReader(masks, buffers));
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> T convertFrom(final Type type, final BsonReader reader) { public <T> T convertFrom(final Type type, final BsonReader reader) {
if (type == null) return null; if (type == null) return null;

View File

@@ -29,16 +29,7 @@ public class JsonByteBufferReader extends JsonReader {
private ByteBuffer currentBuffer; private ByteBuffer currentBuffer;
protected ConvertMask mask; //mask二选一 protected ConvertMask mask;
protected ConvertMask[] masks; //mask二选一
protected JsonByteBufferReader(ConvertMask[] masks, ByteBuffer... buffers) {
this.masks = masks;
this.buffers = buffers;
if (buffers != null && buffers.length > 0) this.currentBuffer = buffers[currentIndex];
if (this.masks.length != buffers.length) throw new RuntimeException("masks.length must equals buffers.length");
}
protected JsonByteBufferReader(ConvertMask mask, ByteBuffer... buffers) { protected JsonByteBufferReader(ConvertMask mask, ByteBuffer... buffers) {
this.mask = mask; this.mask = mask;
@@ -54,24 +45,19 @@ public class JsonByteBufferReader extends JsonReader {
this.currentBuffer = null; this.currentBuffer = null;
this.buffers = null; this.buffers = null;
this.mask = null; this.mask = null;
this.masks = null;
return false; return false;
} }
protected byte nextByte() { protected byte nextByte() {
if (this.currentBuffer.hasRemaining()) { if (this.currentBuffer.hasRemaining()) {
this.position++; this.position++;
if (mask != null) return mask.unmask(this.currentBuffer.get()); return mask == null ? this.currentBuffer.get() : mask.unmask(this.currentBuffer.get());
if (masks != null) return masks[this.currentIndex].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++;
if (mask != null) return mask.unmask(this.currentBuffer.get()); return mask == null ? this.currentBuffer.get() : mask.unmask(this.currentBuffer.get());
if (masks != null) return masks[this.currentIndex].unmask(this.currentBuffer.get());
return this.currentBuffer.get();
} }
} }
} }

View File

@@ -121,12 +121,6 @@ public final class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
return (T) factory.loadDecoder(type).convertFrom(new JsonByteBufferReader(mask, buffers)); return (T) factory.loadDecoder(type).convertFrom(new JsonByteBufferReader(mask, buffers));
} }
@Override
public <T> T convertFrom(final Type type, final ConvertMask[] masks, final ByteBuffer... buffers) {
if (type == null || buffers == null || buffers.length == 0) return null;
return (T) factory.loadDecoder(type).convertFrom(new JsonByteBufferReader(masks, buffers));
}
public <T> T convertFrom(final Type type, final JsonReader reader) { public <T> T convertFrom(final Type type, final JsonReader reader) {
if (type == null) return null; if (type == null) return null;
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")