use of io.nuls.core.utils.io.NulsOutputStreamBuffer in project nuls by nuls-io.
the class BaseNulsData method serialize.
/**
* First, serialize the version field
*
* @return
*/
public final byte[] serialize() throws IOException {
ByteArrayOutputStream bos = null;
try {
int size = size();
bos = new UnsafeByteArrayOutputStream(size);
NulsOutputStreamBuffer buffer = new NulsOutputStreamBuffer(bos);
if (size == 0) {
bos.write(NulsConstant.PLACE_HOLDER);
} else {
serializeToStream(buffer);
}
byte[] bytes = bos.toByteArray();
if (bytes.length != this.size()) {
throw new NulsRuntimeException(ErrorCode.FAILED, "序列化和size长度不一致:" + this.getClass());
}
return bytes;
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
throw e;
}
}
}
}
Aggregations