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

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

ASN1Object (and all classes that inherit it) has a getEncoded()方法即应该returnDER/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");
    }
}

除非 child class 覆盖了 getEncoded,并且在此上下文中 IOException 表示所涉及的一个或多个 objects 为空。