This commit is contained in:
Redkale
2017-03-21 09:14:32 +08:00
parent 74f4ddf50b
commit 15e03c0459
2 changed files with 82 additions and 4 deletions

View File

@@ -493,6 +493,37 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
}
}
/**
* 异步输出指定内容
*
* @param <A> 泛型
* @param buffers 输出内容
* @param attachment 异步回调参数
* @param handler 异步回调函数
*/
public <A> void sendBody(ByteBuffer[] buffers, A attachment, AsyncHandler<Integer, A> 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);
}
}
/**
* 将指定文件按响应结果输出
*

View File

@@ -96,7 +96,7 @@ public final class Utility {
* @return Map
*/
public static Map<String, String> ofMap(String... items) {
HashMap<String, String> map = new HashMap<>();
HashMap<String, String> 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<Object, Object> ofMap(Object... items) {
HashMap<Object, Object> map = new HashMap<>();
HashMap<Object, Object> 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 <T> 泛型
* @param array 原数组
* @param objs 待追加数据
*
* @return 新数组
*/
public static <T> 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 <T> 泛型
* @param array 原数组
* @param objs 待追加数据
*
* @return 新数组
*/
public static <T> T[] unshift(final T[] array, final Collection<T> 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 <T> 泛型
* @param array 原数组
@@ -176,7 +223,7 @@ public final class Utility {
}
/**
* 数组上追加数据
* 将一个或多个新元素添加到数组结尾
*
* @param <T> 泛型
* @param array 原数组