use of com.firenio.component.FastThreadLocal in project baseio by generallycloud.
the class HttpCodec method encode.
public void encode(final Channel ch, ByteBuf buf, Frame frame) {
HttpFrame f = (HttpFrame) frame;
FastThreadLocal l = FastThreadLocal.get();
List<byte[]> bytes_list = (List<byte[]>) l.getList();
byte[] content = (byte[]) f.getContent();
byte[] head_bytes = f.getStatus().getLine();
byte[] conn_bytes = f.getConnection().getLine();
byte[] type_bytes = f.getContentType().getLine();
byte[] date_bytes = f.getDate();
int write_size = content.length;
byte[] cl_len_bytes;
int cl_len;
if (write_size < 1024) {
cl_len_bytes = cl_bytes[write_size];
cl_len = cl_len_bytes.length;
} else {
cl_len_bytes = cl_buf.array();
int tmp_len = cl_buf.limit();
int len_idx = Util.valueOf(write_size, cl_len_bytes);
int num_len = cl_len_bytes.length - len_idx;
System.arraycopy(cl_len_bytes, len_idx, cl_len_bytes, tmp_len, num_len);
cl_len = tmp_len + num_len;
}
int h_size = 0;
IntObjectMap<byte[]> headers = f.getResponseHeaders();
if (headers != null) {
for (headers.scan(); headers.hasNext(); ) {
byte[] k = HttpHeader.get(headers.getKey()).getBytes();
byte[] v = headers.getValue();
h_size++;
bytes_list.add(k);
bytes_list.add(v);
}
}
buf.writeBytes(head_bytes);
buf.writeBytes(cl_len_bytes, 0, cl_len);
if (conn_bytes != null) {
buf.writeBytes(conn_bytes);
}
if (type_bytes != null) {
buf.writeBytes(type_bytes);
}
if (date_bytes != null) {
buf.writeBytes(date_bytes);
}
buf.writeByte(R);
buf.writeByte(N);
if (h_size > 0) {
put_headers(buf, bytes_list, h_size);
}
buf.writeByte(R);
buf.writeByte(N);
buf.writeBytes(content);
}
use of com.firenio.component.FastThreadLocal in project baseio by generallycloud.
the class HttpCodec method encode.
@Override
public ByteBuf encode(final Channel ch, Frame frame) {
HttpFrame f = (HttpFrame) frame;
FastThreadLocal l = FastThreadLocal.get();
List<byte[]> bytes_array = (List<byte[]>) l.getList();
Object content = f.getContent();
ByteBuf content_buf = null;
byte[] content_array = null;
byte[] head_bytes = f.getStatus().getLine();
byte[] conn_bytes = f.getConnection().getLine();
byte[] type_bytes = f.getContentType().getLine();
byte[] date_bytes = f.getDate();
boolean is_array = false;
int write_size = 0;
if (content instanceof ByteBuf) {
content_buf = (ByteBuf) content;
write_size = content_buf.readableBytes();
} else if (content instanceof byte[]) {
is_array = true;
content_array = (byte[]) content;
write_size = content_array.length;
}
byte[] cl_len_bytes;
int cl_len;
if (write_size < 1024) {
cl_len_bytes = cl_bytes[write_size];
cl_len = cl_len_bytes.length;
} else {
cl_len_bytes = cl_buf.array();
int tmp_len = cl_buf.limit();
int len_idx = Util.valueOf(write_size, cl_len_bytes);
int num_len = cl_len_bytes.length - len_idx;
System.arraycopy(cl_len_bytes, len_idx, cl_len_bytes, tmp_len, num_len);
cl_len = tmp_len + num_len;
}
int hlen = head_bytes.length;
int tlen = type_bytes == null ? 0 : type_bytes.length;
int clen = conn_bytes == null ? 0 : conn_bytes.length;
int dlen = date_bytes == null ? 0 : date_bytes.length;
int len = hlen + cl_len + dlen + 2 + clen + tlen;
int h_size = 0;
IntObjectMap<byte[]> headers = f.getResponseHeaders();
if (headers != null) {
for (headers.scan(); headers.hasNext(); ) {
byte[] k = HttpHeader.get(headers.getKey()).getBytes();
byte[] v = headers.getValue();
h_size++;
bytes_array.add(k);
bytes_array.add(v);
len += 4;
len += k.length;
len += v.length;
}
}
len += 2;
if (write_size <= cthreshold) {
len += write_size;
}
ByteBuf buf;
if (Develop.BUF_DEBUG) {
buf = ch.alloc().allocate(1);
} else {
buf = ch.alloc().allocate(len);
}
buf.writeBytes(head_bytes);
buf.writeBytes(cl_len_bytes, 0, cl_len);
if (conn_bytes != null) {
buf.writeBytes(conn_bytes);
}
if (type_bytes != null) {
buf.writeBytes(type_bytes);
}
if (date_bytes != null) {
buf.writeBytes(date_bytes);
}
buf.writeByte(R);
buf.writeByte(N);
if (h_size > 0) {
put_headers(buf, bytes_array, h_size);
}
buf.writeByte(R);
buf.writeByte(N);
if (write_size > cthreshold) {
ch.write(buf);
if (is_array) {
ch.write(ByteBuf.wrap(content_array));
} else {
ch.write(content_buf);
}
return null;
} else {
if (write_size > 0) {
if (is_array) {
buf.writeBytes(content_array);
} else {
buf.writeBytes(content_buf);
}
}
return buf;
}
}
Aggregations