繁体   English   中英

为什么ASN1Object.getEncoded()在BouncyCastle中抛出IOException?

[英]Why does ASN1Object.getEncoded() throw IOException in BouncyCastle?

ASN1Object (以及所有继承它的类)具有一个getEncoded()方法,该方法应返回DER / BER编码。 但是,它被标记为引发IOException。

我希望编码可以在内存中完成,而无需执行任何IO。 此外,这需要抛出或尝试捕获来使周围的代码复杂化。

为什么在那里throws IOException

ASN1Object.getEncoded

public byte[] getEncoded()
    throws IOException
{
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    ASN1OutputStream      aOut = new ASN1OutputStream(bOut);

    aOut.writeObject(this); // This is where the throws happens.

    return bOut.toByteArray();
}

ASN1OutputStream.writeObject

public void writeObject(
    ASN1Encodable obj)
    throws IOException
{
    if (obj != null)
    {
        obj.toASN1Primitive().encode(this);
    }
    else
    {
        throw new IOException("null object detected");
    }
}

除非子类重写了getEncoded ,否则在此上下文中的IOException意味着所涉及的一个或多个对象为null。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM