Convert增加convertMapToBytes方法
This commit is contained in:
@@ -65,6 +65,8 @@ public abstract class Convert<R extends Reader, W extends Writer> {
|
|||||||
|
|
||||||
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);
|
||||||
|
|
||||||
|
public abstract byte[] convertMapToBytes(final Object... values);
|
||||||
|
|
||||||
public abstract ByteBuffer[] convertMapTo(final Supplier<ByteBuffer> supplier, final Object... values);
|
public abstract ByteBuffer[] convertMapTo(final Supplier<ByteBuffer> supplier, final Object... values);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -247,6 +247,17 @@ public class BsonConvert extends BinaryConvert<BsonReader, BsonWriter> {
|
|||||||
return out.toBuffers();
|
return out.toBuffers();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public byte[] convertMapToBytes(final Object... values) {
|
||||||
|
BsonWriter out = pollBsonWriter();
|
||||||
|
if (values == null) {
|
||||||
|
out.writeNull();
|
||||||
|
} else {
|
||||||
|
((AnyEncoder) factory.getAnyEncoder()).convertMapTo(out, values);
|
||||||
|
}
|
||||||
|
return out.toArray();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ByteBuffer[] convertMapTo(final Supplier<ByteBuffer> supplier, final Object... values) {
|
public ByteBuffer[] convertMapTo(final Supplier<ByteBuffer> supplier, final Object... values) {
|
||||||
if (supplier == null) return null;
|
if (supplier == null) return null;
|
||||||
|
|||||||
@@ -366,6 +366,17 @@ public class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
|||||||
return out.toBuffers();
|
return out.toBuffers();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public byte[] convertMapToBytes(final Object... values) {
|
||||||
|
JsonWriter out = pollJsonWriter();
|
||||||
|
if (values == null) {
|
||||||
|
out.writeNull();
|
||||||
|
} else {
|
||||||
|
((AnyEncoder) factory.getAnyEncoder()).convertMapTo(out, values);
|
||||||
|
}
|
||||||
|
return out.toBytes();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ByteBuffer[] convertMapTo(final Supplier<ByteBuffer> supplier, final Object... values) {
|
public ByteBuffer[] convertMapTo(final Supplier<ByteBuffer> supplier, final Object... values) {
|
||||||
if (supplier == null) return null;
|
if (supplier == null) return null;
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ import java.nio.channels.*;
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import org.redkale.net.*;
|
|
||||||
import org.redkale.util.*;
|
import org.redkale.util.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -25,6 +25,8 @@ import org.redkale.util.ObjectPool;
|
|||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public abstract class Response<C extends Context, R extends Request<C>> {
|
public abstract class Response<C extends Context, R extends Request<C>> {
|
||||||
|
|
||||||
|
protected static final boolean respConvertByBuffer = Boolean.getBoolean("resp.convert.bytebuffer");
|
||||||
|
|
||||||
protected final C context;
|
protected final C context;
|
||||||
|
|
||||||
protected final ObjectPool<Response> responsePool; //虚拟构建的Response可能不存在responsePool
|
protected final ObjectPool<Response> responsePool; //虚拟构建的Response可能不存在responsePool
|
||||||
|
|||||||
@@ -327,7 +327,11 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
|||||||
public void finishMapJson(final Object... objs) {
|
public void finishMapJson(final Object... objs) {
|
||||||
this.contentType = this.jsonContentType;
|
this.contentType = this.jsonContentType;
|
||||||
if (this.recycleListener != null) this.output = objs;
|
if (this.recycleListener != null) this.output = objs;
|
||||||
finish(request.getRespConvert().convertMapTo(getBodyBufferSupplier(), objs));
|
if (respConvertByBuffer) {
|
||||||
|
finish(request.getRespConvert().convertMapTo(getBodyBufferSupplier(), objs));
|
||||||
|
} else {
|
||||||
|
finish(request.getRespConvert().convertMapToBytes(objs));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -339,7 +343,11 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
|||||||
public void finishJson(final JsonConvert convert, final Object obj) {
|
public void finishJson(final JsonConvert convert, final Object obj) {
|
||||||
this.contentType = this.jsonContentType;
|
this.contentType = this.jsonContentType;
|
||||||
if (this.recycleListener != null) this.output = obj;
|
if (this.recycleListener != null) this.output = obj;
|
||||||
finish(convert.convertTo(getBodyBufferSupplier(), obj));
|
if (respConvertByBuffer) {
|
||||||
|
finish(convert.convertTo(getBodyBufferSupplier(), obj));
|
||||||
|
} else {
|
||||||
|
finish(convert.convertToBytes(obj));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -352,7 +360,11 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
|||||||
public void finishMapJson(final JsonConvert convert, final Object... objs) {
|
public void finishMapJson(final JsonConvert convert, final Object... objs) {
|
||||||
this.contentType = this.jsonContentType;
|
this.contentType = this.jsonContentType;
|
||||||
if (this.recycleListener != null) this.output = objs;
|
if (this.recycleListener != null) this.output = objs;
|
||||||
finish(convert.convertMapTo(getBodyBufferSupplier(), objs));
|
if (respConvertByBuffer) {
|
||||||
|
finish(convert.convertMapTo(getBodyBufferSupplier(), objs));
|
||||||
|
} else {
|
||||||
|
finish(convert.convertMapToBytes(objs));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -364,7 +376,11 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
|||||||
public void finishJson(final Type type, final Object obj) {
|
public void finishJson(final Type type, final Object obj) {
|
||||||
this.contentType = this.jsonContentType;
|
this.contentType = this.jsonContentType;
|
||||||
this.output = obj;
|
this.output = obj;
|
||||||
finish(request.getRespConvert().convertTo(getBodyBufferSupplier(), type, obj));
|
if (respConvertByBuffer) {
|
||||||
|
finish(request.getRespConvert().convertTo(getBodyBufferSupplier(), type, obj));
|
||||||
|
} else {
|
||||||
|
finish(request.getRespConvert().convertToBytes(type, obj));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -377,7 +393,11 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
|||||||
public void finishJson(final JsonConvert convert, final Type type, final Object obj) {
|
public void finishJson(final JsonConvert convert, final Type type, final Object obj) {
|
||||||
this.contentType = this.jsonContentType;
|
this.contentType = this.jsonContentType;
|
||||||
if (this.recycleListener != null) this.output = obj;
|
if (this.recycleListener != null) this.output = obj;
|
||||||
finish(convert.convertTo(getBodyBufferSupplier(), type, obj));
|
if (respConvertByBuffer) {
|
||||||
|
finish(convert.convertTo(getBodyBufferSupplier(), type, obj));
|
||||||
|
} else {
|
||||||
|
finish(convert.convertToBytes(type, obj));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -388,7 +408,11 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
|||||||
public void finishJson(final Object... objs) {
|
public void finishJson(final Object... objs) {
|
||||||
this.contentType = this.jsonContentType;
|
this.contentType = this.jsonContentType;
|
||||||
if (this.recycleListener != null) this.output = objs;
|
if (this.recycleListener != null) this.output = objs;
|
||||||
finish(request.getRespConvert().convertTo(getBodyBufferSupplier(), objs));
|
if (respConvertByBuffer) {
|
||||||
|
finish(request.getRespConvert().convertTo(getBodyBufferSupplier(), objs));
|
||||||
|
} else {
|
||||||
|
finish(request.getRespConvert().convertToBytes(objs));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -408,7 +432,11 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
|||||||
}
|
}
|
||||||
Convert convert = ret == null ? null : ret.convert();
|
Convert convert = ret == null ? null : ret.convert();
|
||||||
if (convert == null) convert = request.getRespConvert();
|
if (convert == null) convert = request.getRespConvert();
|
||||||
finish(convert.convertTo(getBodyBufferSupplier(), ret));
|
if (respConvertByBuffer) {
|
||||||
|
finish(convert.convertTo(getBodyBufferSupplier(), ret));
|
||||||
|
} else {
|
||||||
|
finish(convert.convertToBytes(ret));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -427,7 +455,11 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
|||||||
this.header.addValue("retcode", String.valueOf(ret.getRetcode()));
|
this.header.addValue("retcode", String.valueOf(ret.getRetcode()));
|
||||||
this.header.addValue("retinfo", ret.getRetinfo());
|
this.header.addValue("retinfo", ret.getRetinfo());
|
||||||
}
|
}
|
||||||
finish(convert.convertTo(getBodyBufferSupplier(), ret));
|
if (respConvertByBuffer) {
|
||||||
|
finish(convert.convertTo(getBodyBufferSupplier(), ret));
|
||||||
|
} else {
|
||||||
|
finish(convert.convertToBytes(ret));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -575,13 +607,19 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
|||||||
this.header.addValue("retcode", String.valueOf(ret.getRetcode())).addValue("retinfo", ret.getRetinfo());
|
this.header.addValue("retcode", String.valueOf(ret.getRetcode())).addValue("retinfo", ret.getRetinfo());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.channel == null) { //虚拟的HttpResponse
|
if (this.channel == null) { //虚拟的HttpResponse
|
||||||
finish(type == null ? convert.convertToBytes(obj) : convert.convertToBytes(type, obj));
|
finish(type == null ? convert.convertToBytes(obj) : convert.convertToBytes(type, obj));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ByteBuffer[] buffers = type == null ? convert.convertTo(getBodyBufferSupplier(), obj)
|
if (respConvertByBuffer) {
|
||||||
: convert.convertTo(getBodyBufferSupplier(), type, obj);
|
ByteBuffer[] buffers = type == null ? convert.convertTo(getBodyBufferSupplier(), obj)
|
||||||
finish(buffers);
|
: convert.convertTo(getBodyBufferSupplier(), type, obj);
|
||||||
|
finish(buffers);
|
||||||
|
} else {
|
||||||
|
byte[] bs = type == null ? convert.convertToBytes(obj) : convert.convertToBytes(type, obj);
|
||||||
|
finish(bs);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user