diff --git a/src/org/redkale/net/http/HttpRequest.java b/src/org/redkale/net/http/HttpRequest.java index b9d4b193c..1748f5004 100644 --- a/src/org/redkale/net/http/HttpRequest.java +++ b/src/org/redkale/net/http/HttpRequest.java @@ -109,15 +109,15 @@ public class HttpRequest extends Request { } if (this.requestURI.contains("../")) return -1; index = ++offset; - this.protocol = array.toString(index, array.count() - index, charset).trim(); + this.protocol = array.toString(index, array.size() - index, charset).trim(); while (readLine(buffer, array)) { - if (array.count() < 2) break; + if (array.size() < 2) break; index = 0; offset = array.find(index, ':'); if (offset <= 0) return -1; String name = array.toString(index, offset, charset).trim(); index = offset + 1; - String value = array.toString(index, array.count() - index, charset).trim(); + String value = array.toString(index, array.size() - index, charset).trim(); switch (name) { case "Content-Type": this.contentType = value; @@ -143,15 +143,15 @@ public class HttpRequest extends Request { header.addValue(name, value); } } - array.clear(); - if (buffer.hasRemaining()) array.add(buffer, buffer.remaining()); + array.reset(); + if (buffer.hasRemaining()) array.write(buffer, buffer.remaining()); if (this.contentType != null && this.contentType.contains("boundary=")) { this.boundary = true; } - if(this.boundary) this.keepAlive = false; //文件上传必须设置keepAlive为false,因为文件过大时用户不一定会skip掉多余的数据 + if (this.boundary) this.keepAlive = false; //文件上传必须设置keepAlive为false,因为文件过大时用户不一定会skip掉多余的数据 if (this.contentLength > 0 && (this.contentType == null || !this.boundary)) { if (this.contentLength > context.getMaxbody()) return -1; - int lr = (int) this.contentLength - array.count(); + int lr = (int) this.contentLength - array.size(); return lr > 0 ? lr : 0; } return 0; @@ -160,7 +160,7 @@ public class HttpRequest extends Request { @Override protected int readBody(ByteBuffer buffer) { int len = buffer.remaining(); - array.add(buffer, len); + array.write(buffer, len); return len; } @@ -170,7 +170,7 @@ public class HttpRequest extends Request { private void parseBody() { if (this.boundary || bodyparsed) return; - addParameter(array, 0, array.count()); + addParameter(array, 0, array.size()); bodyparsed = true; } @@ -195,15 +195,15 @@ public class HttpRequest extends Request { private boolean readLine(ByteBuffer buffer, ByteArray bytes) { byte lasted = '\r'; - bytes.clear(); + bytes.size(); for (;;) { if (!buffer.hasRemaining()) { - if (lasted != '\r') bytes.add(lasted); + if (lasted != '\r') bytes.write(lasted); return false; } byte b = buffer.get(); if (b == -1 || (lasted == '\r' && b == '\n')) break; - if (lasted != '\r') bytes.add(lasted); + if (lasted != '\r') bytes.write(lasted); lasted = b; } return true; @@ -286,10 +286,10 @@ public class HttpRequest extends Request { */ public final MultiContext getMultiContext() { return new MultiContext(context.getCharset(), this.getContentType(), this.params, - new BufferedInputStream(Channels.newInputStream(this.channel), Math.max(array.count(), 8192)) { + new BufferedInputStream(Channels.newInputStream(this.channel), Math.max(array.size(), 8192)) { { - array.write(this.buf); - this.count = array.count(); + array.copyTo(this.buf); + this.count = array.size(); } }, null); } @@ -321,7 +321,7 @@ public class HttpRequest extends Request { this.header.clear(); this.params.clear(); - this.array.clear(); + this.array.reset(); super.recycle(); } diff --git a/src/org/redkale/net/http/MultiContext.java b/src/org/redkale/net/http/MultiContext.java index 76aefaa1f..d9434fa31 100644 --- a/src/org/redkale/net/http/MultiContext.java +++ b/src/org/redkale/net/http/MultiContext.java @@ -214,22 +214,22 @@ public final class MultiContext { private String readLine(boolean bd) throws IOException { // bd : 是否是读取boundary byte lasted = '\r'; - buf.clear(); + buf.reset(); final int bc = this.endboundarray.length; int c = 0; for (;;) { int b = in.read(); c++; if (b == -1 || (lasted == '\r' && b == '\n')) break; - if (lasted != '\r') buf.add(lasted); + if (lasted != '\r') buf.write(lasted); lasted = (byte) b; if (bd && bc == c) { - buf.add(lasted); + buf.write(lasted); if (buf.equal(this.endboundarray)) break; buf.removeLastByte(); } } - if (buf.count() == 0) return ""; + if (buf.size() == 0) return ""; return buf.toString(this.charset).trim(); } diff --git a/src/org/redkale/util/ByteArray.java b/src/org/redkale/util/ByteArray.java index d45635470..fc908c180 100644 --- a/src/org/redkale/util/ByteArray.java +++ b/src/org/redkale/util/ByteArray.java @@ -30,7 +30,7 @@ public final class ByteArray { content = new byte[Math.max(128, size)]; } - public void clear() { + public void reset() { this.count = 0; } @@ -50,7 +50,7 @@ public final class ByteArray { return count == 0; } - public int count() { + public int size() { return count; } @@ -58,7 +58,11 @@ public final class ByteArray { return content[index]; } - public void write(byte[] buf) { + public byte getLastByte() { + return content[count - 1]; + } + + public void copyTo(byte[] buf) { System.arraycopy(this.content, 0, buf, 0, count); } @@ -91,11 +95,11 @@ public final class ByteArray { if (count > 0) count--; } - public void addInt(int value) { - add((byte) (value >> 24 & 0xFF), (byte) (value >> 16 & 0xFF), (byte) (value >> 8 & 0xFF), (byte) (value & 0xFF)); + public void writeInt(int value) { + write((byte) (value >> 24 & 0xFF), (byte) (value >> 16 & 0xFF), (byte) (value >> 8 & 0xFF), (byte) (value & 0xFF)); } - public void add(byte value) { + public void write(byte value) { if (count >= content.length - 1) { byte[] ns = new byte[content.length + 8]; System.arraycopy(content, 0, ns, 0, count); @@ -104,7 +108,7 @@ public final class ByteArray { content[count++] = value; } - public void add(byte... values) { + public void write(byte... values) { if (count >= content.length - values.length) { byte[] ns = new byte[content.length + values.length]; System.arraycopy(content, 0, ns, 0, count); @@ -114,7 +118,7 @@ public final class ByteArray { count += values.length; } - public void add(ByteBuffer buffer, int len) { + public void write(ByteBuffer buffer, int len) { if (len < 1) return; if (count >= content.length - len) { byte[] ns = new byte[content.length + len];