This commit is contained in:
Redkale
2017-02-23 15:15:43 +08:00
parent 58d08c5787
commit bf355cce28
2 changed files with 50 additions and 21 deletions

View File

@@ -184,14 +184,7 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
* @return HttpResponse
*/
public HttpResponse addCookie(HttpCookie... cookies) {
if (this.cookies == null) {
this.cookies = cookies;
} else {
HttpCookie[] news = new HttpCookie[this.cookies.length + cookies.length];
System.arraycopy(this.cookies, 0, news, 0, this.cookies.length);
System.arraycopy(cookies, 0, news, this.cookies.length, cookies.length);
this.cookies = news;
}
this.cookies = Utility.append(this.cookies, cookies);
return this;
}
@@ -203,18 +196,7 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
* @return HttpResponse
*/
public HttpResponse addCookie(Collection<HttpCookie> cookies) {
if (cookies == null || cookies.isEmpty()) return this;
if (this.cookies == null) {
this.cookies = cookies.toArray(new HttpCookie[cookies.size()]);
} else {
HttpCookie[] news = new HttpCookie[this.cookies.length + cookies.size()];
System.arraycopy(this.cookies, 0, news, 0, this.cookies.length);
int i = this.cookies.length;
for (HttpCookie cookie : cookies) {
news[i++] = cookie;
}
this.cookies = news;
}
this.cookies = Utility.append(this.cookies, cookies);
return this;
}

View File

@@ -5,7 +5,7 @@
package org.redkale.util;
import java.io.*;
import java.lang.reflect.Field;
import java.lang.reflect.*;
import java.net.*;
import java.nio.ByteBuffer;
import java.nio.charset.*;
@@ -128,6 +128,53 @@ public final class Utility {
return UUID.randomUUID().toString().replace("-", "");
}
/**
* 数组上追加数据
*
* @param <T> 泛型
* @param array 原数组
* @param objs 待追加数据
*
* @return 新数组
*/
public static <T> T[] append(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(array, 0, news, 0, array.length);
System.arraycopy(objs, 0, news, array.length, objs.length);
return news;
}
/**
* 数组上追加数据
*
* @param <T> 泛型
* @param array 原数组
* @param objs 待追加数据
*
* @return 新数组
*/
public static <T> T[] append(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());
System.arraycopy(array, 0, news, 0, array.length);
int index = -1;
for (T t : objs) {
news[array.length + (++index)] = t;
}
return news;
}
/**
* 删除掉字符串数组中包含指定的字符串
*