本文整理汇总了Java中com.alibaba.dubbo.common.io.UnsafeByteArrayOutputStream.close方法的典型用法代码示例。如果您正苦于以下问题:Java UnsafeByteArrayOutputStream.close方法的具体用法?Java UnsafeByteArrayOutputStream.close怎么用?Java UnsafeByteArrayOutputStream.close使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.alibaba.dubbo.common.io.UnsafeByteArrayOutputStream
的用法示例。
在下文中一共展示了UnsafeByteArrayOutputStream.close方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: writeTo
import com.alibaba.dubbo.common.io.UnsafeByteArrayOutputStream; //导入方法依赖的package包/类
@Override
public void writeTo(Serializable obj, GenericObjectOutput out) throws IOException
{
if( obj == null )
{
out.write0(OBJECT_NULL);
}
else
{
out.write0(OBJECT_STREAM);
UnsafeByteArrayOutputStream bos = new UnsafeByteArrayOutputStream();
CompactedObjectOutputStream oos = new CompactedObjectOutputStream(bos);
oos.writeObject(obj);
oos.flush();
bos.close();
byte[] b = bos.toByteArray();
out.writeUInt(b.length);
out.write0(b, 0, b.length);
}
}
示例2: writeTo
import com.alibaba.dubbo.common.io.UnsafeByteArrayOutputStream; //导入方法依赖的package包/类
@Override
public void writeTo(Serializable obj, GenericObjectOutput out) throws IOException {
if (obj == null) {
out.write0(OBJECT_NULL);
} else {
out.write0(OBJECT_STREAM);
UnsafeByteArrayOutputStream bos = new UnsafeByteArrayOutputStream();
CompactedObjectOutputStream oos = new CompactedObjectOutputStream(bos);
oos.writeObject(obj);
oos.flush();
bos.close();
byte[] b = bos.toByteArray();
out.writeUInt(b.length);
out.write0(b, 0, b.length);
}
}
示例3: encodeRequest
import com.alibaba.dubbo.common.io.UnsafeByteArrayOutputStream; //导入方法依赖的package包/类
protected void encodeRequest(Channel channel, OutputStream os, Request req) throws IOException {
Serialization serialization = CodecSupport.getSerialization(channel.getUrl());
// header.
byte[] header = new byte[HEADER_LENGTH];
// set magic number.
Bytes.short2bytes(MAGIC, header);
// set request and serialization flag.
header[2] = (byte) (FLAG_REQUEST | serialization.getContentTypeId());
if (req.isTwoWay()) header[2] |= FLAG_TWOWAY;
if (req.isEvent()) header[2] |= FLAG_EVENT;
// set request id.
Bytes.long2bytes(req.getId(), header, 4);
// encode request data.
UnsafeByteArrayOutputStream bos = new UnsafeByteArrayOutputStream(1024);
ObjectOutput out = serialization.serialize(channel.getUrl(), bos);
if (req.isEvent()) {
encodeEventData(channel, out, req.getData());
} else {
encodeRequestData(channel, out, req.getData());
}
out.flushBuffer();
bos.flush();
bos.close();
byte[] data = bos.toByteArray();
checkPayload(channel, data.length);
Bytes.int2bytes(data.length, header, 12);
// write
os.write(header); // write header.
os.write(data); // write data.
}
示例4: getRequestBytes
import com.alibaba.dubbo.common.io.UnsafeByteArrayOutputStream; //导入方法依赖的package包/类
private byte[] getRequestBytes(Object obj, byte[] header) throws IOException{
// encode request data.
UnsafeByteArrayOutputStream bos = new UnsafeByteArrayOutputStream(1024);
ObjectOutput out = serialization.serialize(url, bos);
out.writeObject(obj);
out.flushBuffer();
bos.flush();
bos.close();
byte[] data = bos.toByteArray();
byte[] len = Bytes.int2bytes(data.length);
System.arraycopy(len, 0, header, 12, 4);
byte[] request = join(header, data);
return request;
}
示例5: encodeResponse
import com.alibaba.dubbo.common.io.UnsafeByteArrayOutputStream; //导入方法依赖的package包/类
protected void encodeResponse(Channel channel, OutputStream os, Response res) throws IOException {
try {
Serialization serialization = CodecSupport.getSerialization(channel.getUrl());
// header.
byte[] header = new byte[HEADER_LENGTH];
// set magic number.
Bytes.short2bytes(MAGIC, header);
// set request and serialization flag.
header[2] = serialization.getContentTypeId();
if (res.isHeartbeat()) header[2] |= FLAG_EVENT;
// set response status.
byte status = res.getStatus();
header[3] = status;
// set request id.
Bytes.long2bytes(res.getId(), header, 4);
UnsafeByteArrayOutputStream bos = new UnsafeByteArrayOutputStream(1024);
ObjectOutput out = serialization.serialize(channel.getUrl(), bos);
// encode response data or error message.
if (status == Response.OK) {
if (res.isHeartbeat()) {
encodeHeartbeatData(channel, out, res.getResult());
} else {
encodeResponseData(channel, out, res.getResult());
}
} else out.writeUTF(res.getErrorMessage());
out.flushBuffer();
bos.flush();
bos.close();
byte[] data = bos.toByteArray();
checkPayload(channel, data.length);
Bytes.int2bytes(data.length, header, 12);
// write
os.write(header); // write header.
os.write(data); // write data.
} catch (Throwable t) {
// 发送失败信息给Consumer,否则Consumer只能等超时了
if (!res.isEvent() && res.getStatus() != Response.BAD_RESPONSE) {
try {
// FIXME 在Codec中打印出错日志?在IoHanndler的caught中统一处理?
logger.warn("Fail to encode response: " + res + ", send bad_response info instead, cause: " + t.getMessage(), t);
Response r = new Response(res.getId(), res.getVersion());
r.setStatus(Response.BAD_RESPONSE);
r.setErrorMessage("Failed to send response: " + res + ", cause: " + StringUtils.toString(t));
channel.send(r);
return;
} catch (RemotingException e) {
logger.warn("Failed to send bad_response info back: " + res + ", cause: " + e.getMessage(), e);
}
}
// 重新抛出收到的异常
if (t instanceof IOException) {
throw (IOException) t;
} else if (t instanceof RuntimeException) {
throw (RuntimeException) t;
} else if (t instanceof Error) {
throw (Error) t;
} else {
throw new RuntimeException(t.getMessage(), t);
}
}
}