This commit is contained in:
Redkale
2020-09-16 21:50:50 +08:00
parent f981e4f886
commit 41f0061dca
3 changed files with 16 additions and 6 deletions

View File

@@ -50,6 +50,9 @@ public abstract class Convert<R extends Reader, W extends Writer> {
public abstract <T> T convertFrom(final Type type, final byte[] bytes); public abstract <T> T convertFrom(final Type type, final byte[] bytes);
//@since 2.2.0
public abstract <T> T convertFrom(final Type type, final byte[] bytes, final int offset, final int length);
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 abstract <T> T convertFrom(final Type type, final ConvertMask mask, final ByteBuffer... buffers);

View File

@@ -115,11 +115,12 @@ public class BsonConvert extends BinaryConvert<BsonReader, BsonWriter> {
return convertFrom(type, bytes, 0, bytes.length); return convertFrom(type, bytes, 0, bytes.length);
} }
@Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> T convertFrom(final Type type, final byte[] bytes, final int start, final int len) { public <T> T convertFrom(final Type type, final byte[] bytes, final int offset, final int len) {
if (type == null) return null; if (type == null) return null;
final BsonReader in = readerPool.get(); final BsonReader in = readerPool.get();
in.setBytes(bytes, start, len); in.setBytes(bytes, offset, len);
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
T rs = (T) factory.loadDecoder(type).convertFrom(in); T rs = (T) factory.loadDecoder(type).convertFrom(in);
readerPool.accept(in); readerPool.accept(in);

View File

@@ -110,6 +110,12 @@ public class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
return convertFrom(type, new String(bytes, StandardCharsets.UTF_8)); return convertFrom(type, new String(bytes, StandardCharsets.UTF_8));
} }
@Override
public <T> T convertFrom(final Type type, final byte[] bytes, final int offset, final int length) {
if (bytes == null) return null;
return convertFrom(type, new String(bytes, offset, length, StandardCharsets.UTF_8));
}
public <T> T convertFrom(final Type type, final String text) { public <T> T convertFrom(final Type type, final String text) {
if (text == null) return null; if (text == null) return null;
return convertFrom(type, Utility.charArray(text)); return convertFrom(type, Utility.charArray(text));
@@ -120,10 +126,10 @@ public class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
return convertFrom(type, text, 0, text.length); return convertFrom(type, text, 0, text.length);
} }
public <T> T convertFrom(final Type type, final char[] text, final int start, final int len) { public <T> T convertFrom(final Type type, final char[] text, final int offset, final int length) {
if (text == null || type == null) return null; if (text == null || type == null) return null;
final JsonReader in = readerPool.get(); final JsonReader in = readerPool.get();
in.setText(text, start, len); in.setText(text, offset, length);
T rs = (T) factory.loadDecoder(type).convertFrom(in); T rs = (T) factory.loadDecoder(type).convertFrom(in);
readerPool.accept(in); readerPool.accept(in);
return rs; return rs;
@@ -166,10 +172,10 @@ public class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
} }
//返回非null的值是由String、ArrayList、HashMap任意组合的对象 //返回非null的值是由String、ArrayList、HashMap任意组合的对象
public <V> V convertFrom(final char[] text, final int start, final int len) { public <V> V convertFrom(final char[] text, final int offset, final int length) {
if (text == null) return null; if (text == null) return null;
final JsonReader in = readerPool.get(); final JsonReader in = readerPool.get();
in.setText(text, start, len); in.setText(text, offset, length);
Object rs = new AnyDecoder(factory).convertFrom(in); Object rs = new AnyDecoder(factory).convertFrom(in);
readerPool.accept(in); readerPool.accept(in);
return (V) rs; return (V) rs;