diff --git a/src/org/redkale/net/http/HttpResponse.java b/src/org/redkale/net/http/HttpResponse.java index 8d4e1d4c6..46336bb43 100644 --- a/src/org/redkale/net/http/HttpResponse.java +++ b/src/org/redkale/net/http/HttpResponse.java @@ -493,6 +493,37 @@ public class HttpResponse extends Response { } } + /** + * 异步输出指定内容 + * + * @param 泛型 + * @param buffers 输出内容 + * @param attachment 异步回调参数 + * @param handler 异步回调函数 + */ + public void sendBody(ByteBuffer[] buffers, A attachment, AsyncHandler handler) { + if (!this.headsended) { + if (this.contentLength < 0) { + int len = 0; + if (buffers != null && buffers.length > 0) { + for (ByteBuffer b : buffers) { + len += b.remaining(); + } + } + this.contentLength = len; + } + ByteBuffer headbuf = createHeader(); + headbuf.flip(); + if (buffers == null || buffers.length == 0) { + super.send(headbuf, attachment, handler); + } else { + super.send(Utility.unshift(buffers, headbuf), attachment, handler); + } + } else { + super.send(buffers, attachment, handler); + } + } + /** * 将指定文件按响应结果输出 * diff --git a/src/org/redkale/util/Utility.java b/src/org/redkale/util/Utility.java index e72735f7b..25dc52741 100644 --- a/src/org/redkale/util/Utility.java +++ b/src/org/redkale/util/Utility.java @@ -96,7 +96,7 @@ public final class Utility { * @return Map */ public static Map ofMap(String... items) { - HashMap map = new HashMap<>(); + HashMap map = new LinkedHashMap<>(); int len = items.length / 2; for (int i = 0; i < len; i++) { map.put(items[i * 2], items[i * 2 + 1]); @@ -113,7 +113,7 @@ public final class Utility { * @return Map */ public static Map ofMap(Object... items) { - HashMap map = new HashMap<>(); + HashMap map = new LinkedHashMap<>(); int len = items.length / 2; for (int i = 0; i < len; i++) { map.put(items[i * 2], items[i * 2 + 1]); @@ -159,7 +159,54 @@ public final class Utility { } /** - * 数组上追加数据 + * 将一个或多个新元素添加到数组开始,数组中的元素自动后移 + * + * @param 泛型 + * @param array 原数组 + * @param objs 待追加数据 + * + * @return 新数组 + */ + public static T[] unshift(final T[] array, final T... objs) { + if (array == null || array.length == 0) return objs; + final T[] news = (T[]) Array.newInstance(array.getClass().getComponentType(), array.length + objs.length); + System.arraycopy(objs, 0, news, 0, objs.length); + System.arraycopy(array, 0, news, objs.length, array.length); + return news; + } + + /** + * 将一个或多个新元素添加到数组开始,数组中的元素自动后移 + * + * @param 泛型 + * @param array 原数组 + * @param objs 待追加数据 + * + * @return 新数组 + */ + public static T[] unshift(final T[] array, final Collection objs) { + if (objs == null || objs.isEmpty()) return array; + if (array == null) { + T one = null; + for (T t : objs) { + if (t != null) one = t; + break; + } + if (one == null) return array; + T[] news = (T[]) Array.newInstance(one.getClass(), objs.size()); + return objs.toArray(news); + } + T[] news = (T[]) Array.newInstance(array.getClass().getComponentType(), array.length + objs.size()); + int index = -1; + for (T t : objs) { + news[(++index)] = t; + } + System.arraycopy(array, 0, news, objs.size(), array.length); + return news; + } + + /** + * 将一个或多个新元素添加到数组结尾 * * @param 泛型 * @param array 原数组 @@ -176,7 +223,7 @@ public final class Utility { } /** - * 数组上追加数据 + * 将一个或多个新元素添加到数组结尾 * * @param 泛型 * @param array 原数组