Search in sources :

Example 1 with UnsafeByteArrayOutputStream

use of io.nuls.core.crypto.UnsafeByteArrayOutputStream 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;
            }
        }
    }
}
Also used : NulsOutputStreamBuffer(io.nuls.core.utils.io.NulsOutputStreamBuffer) UnsafeByteArrayOutputStream(io.nuls.core.crypto.UnsafeByteArrayOutputStream) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) UnsafeByteArrayOutputStream(io.nuls.core.crypto.UnsafeByteArrayOutputStream) IOException(java.io.IOException)

Aggregations

UnsafeByteArrayOutputStream (io.nuls.core.crypto.UnsafeByteArrayOutputStream)1 NulsRuntimeException (io.nuls.core.exception.NulsRuntimeException)1 NulsOutputStreamBuffer (io.nuls.core.utils.io.NulsOutputStreamBuffer)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1