From bf355cce28bc225bde7ec4687802acaad006807f Mon Sep 17 00:00:00 2001 From: Redkale <22250530@qq.com> Date: Thu, 23 Feb 2017 15:15:43 +0800 Subject: [PATCH] --- src/org/redkale/net/http/HttpResponse.java | 22 +--------- src/org/redkale/util/Utility.java | 49 +++++++++++++++++++++- 2 files changed, 50 insertions(+), 21 deletions(-) diff --git a/src/org/redkale/net/http/HttpResponse.java b/src/org/redkale/net/http/HttpResponse.java index e7696a3e9..6242aed5e 100644 --- a/src/org/redkale/net/http/HttpResponse.java +++ b/src/org/redkale/net/http/HttpResponse.java @@ -184,14 +184,7 @@ public class HttpResponse extends Response { * @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 { * @return HttpResponse */ public HttpResponse addCookie(Collection 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; } diff --git a/src/org/redkale/util/Utility.java b/src/org/redkale/util/Utility.java index b1e3b9bd2..e8f55f2b3 100644 --- a/src/org/redkale/util/Utility.java +++ b/src/org/redkale/util/Utility.java @@ -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 泛型 + * @param array 原数组 + * @param objs 待追加数据 + * + * @return 新数组 + */ + public static 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 泛型 + * @param array 原数组 + * @param objs 待追加数据 + * + * @return 新数组 + */ + public static T[] append(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()); + System.arraycopy(array, 0, news, 0, array.length); + int index = -1; + for (T t : objs) { + news[array.length + (++index)] = t; + } + return news; + } + /** * 删除掉字符串数组中包含指定的字符串 *