优化toBuffers方法
This commit is contained in:
@@ -39,6 +39,7 @@ public class BsonWriter extends Writer {
|
||||
}
|
||||
|
||||
public ByteBuffer[] toBuffers(final Supplier<ByteBuffer> supplier) {
|
||||
if (supplier == null) return new ByteBuffer[]{ByteBuffer.wrap(content, 0, count)};
|
||||
return ByteBufferWriter.create(supplier).put(content, 0, count).toBuffers();
|
||||
}
|
||||
|
||||
|
||||
@@ -55,8 +55,12 @@ public class JsonByteBufferWriter extends JsonWriter {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuffer[] toBuffers() {
|
||||
return toBuffers(supplier);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuffer[] toBuffers(Supplier<ByteBuffer> supplier) {
|
||||
if (buffers == null) return new ByteBuffer[0];
|
||||
for (int i = index; i < this.buffers.length; i++) {
|
||||
ByteBuffer buf = this.buffers[i];
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
package org.redkale.convert.json;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.function.Supplier;
|
||||
import org.redkale.convert.*;
|
||||
import org.redkale.util.*;
|
||||
|
||||
@@ -106,8 +107,9 @@ public class JsonWriter extends Writer {
|
||||
return true;
|
||||
}
|
||||
|
||||
public ByteBuffer[] toBuffers() {
|
||||
return new ByteBuffer[]{ByteBuffer.wrap(Utility.encodeUTF8(content, 0, count))};
|
||||
public ByteBuffer[] toBuffers(Supplier<ByteBuffer> supplier) {
|
||||
if (supplier == null) return new ByteBuffer[]{ByteBuffer.wrap(Utility.encodeUTF8(content, 0, count))};
|
||||
return ByteBufferWriter.create(supplier).put(Utility.encodeUTF8(content, 0, count)).toBuffers();
|
||||
}
|
||||
|
||||
public byte[] toBytes() {
|
||||
|
||||
@@ -10,6 +10,7 @@ import java.nio.ByteBuffer;
|
||||
import java.nio.channels.CompletionHandler;
|
||||
import java.util.function.*;
|
||||
import java.util.logging.Level;
|
||||
import org.redkale.util.ByteBufferWriter;
|
||||
|
||||
/**
|
||||
* 协议响应对象
|
||||
@@ -274,7 +275,7 @@ public abstract class Response<C extends Context, R extends Request<C>> {
|
||||
buffer.flip();
|
||||
this.finish(buffer);
|
||||
} else {
|
||||
this.finish(ByteBuffer.wrap(bs));
|
||||
this.finish(ByteBufferWriter.toBuffers(this.context.getBufferSupplier(), bs));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -636,7 +636,7 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
||||
super.finish(false, headbuf);
|
||||
} else {
|
||||
headbuf.flip();
|
||||
super.finish(false, new ByteBuffer[]{headbuf, ByteBuffer.wrap(content)});
|
||||
super.finish(false, Utility.append(new ByteBuffer[]{headbuf}, ByteBufferWriter.toBuffers(context.getBufferSupplier(), content)));
|
||||
}
|
||||
} else {
|
||||
if (this.context.getBufferCapacity() >= content.length) {
|
||||
@@ -645,7 +645,7 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
||||
buffer.flip();
|
||||
this.finish(false, buffer);
|
||||
} else {
|
||||
this.finish(false, ByteBuffer.wrap(content));
|
||||
this.finish(false, ByteBufferWriter.toBuffers(context.getBufferSupplier(), content));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -63,6 +63,22 @@ public class ByteBufferReader {
|
||||
return new ByteBufferReader(buffers);
|
||||
}
|
||||
|
||||
public static byte[] toBytes(ByteBuffer[] buffers) {
|
||||
if (buffers == null) return null;
|
||||
int size = 0;
|
||||
for (ByteBuffer buffer : buffers) {
|
||||
size += buffer.remaining();
|
||||
}
|
||||
byte[] bs = new byte[size];
|
||||
int index = 0;
|
||||
for (ByteBuffer buffer : buffers) {
|
||||
int remain = buffer.remaining();
|
||||
buffer.get(bs, index, remain);
|
||||
index += remain;
|
||||
}
|
||||
return bs;
|
||||
}
|
||||
|
||||
public boolean hasRemaining() {
|
||||
return this.currBuffer.hasRemaining();
|
||||
}
|
||||
|
||||
@@ -34,6 +34,14 @@ public class ByteBufferWriter {
|
||||
return new ByteBufferWriter(supplier);
|
||||
}
|
||||
|
||||
public static ByteBuffer[] toBuffers(Supplier<ByteBuffer> supplier, byte[] content) {
|
||||
return new ByteBufferWriter(supplier).put(content, 0, content.length).toBuffers();
|
||||
}
|
||||
|
||||
public static ByteBuffer[] toBuffers(Supplier<ByteBuffer> supplier, byte[] content, int offset, int length) {
|
||||
return new ByteBufferWriter(supplier).put(content, offset, length).toBuffers();
|
||||
}
|
||||
|
||||
private ByteBuffer getLastBuffer(int size) {
|
||||
if (this.buffers == null) {
|
||||
ByteBuffer buf = supplier.get();
|
||||
@@ -173,19 +181,4 @@ public class ByteBufferWriter {
|
||||
return this;
|
||||
}
|
||||
|
||||
public static byte[] toBytes(ByteBuffer[] buffers) {
|
||||
if (buffers == null) return null;
|
||||
int size = 0;
|
||||
for (ByteBuffer buffer : buffers) {
|
||||
size += buffer.remaining();
|
||||
}
|
||||
byte[] bs = new byte[size];
|
||||
int index = 0;
|
||||
for (ByteBuffer buffer : buffers) {
|
||||
int remain = buffer.remaining();
|
||||
buffer.get(bs, index, remain);
|
||||
index += remain;
|
||||
}
|
||||
return bs;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user