简体   繁体   English

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

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

ASN1Object (and all classes that inherit it) has a getEncoded() method that is supposed to return the DER/BER encoding. ASN1Object (以及所有继承它的类)具有一个getEncoded()方法,该方法应返回DER / BER编码。 However it's marked as throwing an IOException. 但是,它被标记为引发IOException。

I expected the encoding to be done in memory and no IO to be performed. 我希望编码可以在内存中完成,而无需执行任何IO。 Furthermore, this complicates the surrounding code by requiring throws or try catches. 此外,这需要抛出或尝试捕获来使周围的代码复杂化。

Why is the throws IOException there? 为什么在那里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 : ASN1OutputStream.writeObject

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

Unless a child class has overriden getEncoded , an IOException in this context means one or more of the objects involved were null. 除非子类重写了getEncoded ,否则在此上下文中的IOException意味着所涉及的一个或多个对象为null。

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

相关问题 为什么会抛出 IOException - Why Does It Throw an IOException 为什么HttpServlet抛出IOException? - Why does HttpServlet throw an IOException? 为什么此代码会引发IOException? - Why does this code throw an IOException? [BouncyCastle] PKCS10CertificationRequest.getEncoded()返回什么编码? - What encoding does [BouncyCastle] PKCS10CertificationRequest.getEncoded() return? 如果应该打开普通/文本消息,为什么Javamail会抛出IOException? - Why does Javamail throw an IOException if a plain/text message should be opened? 需要帮助来了解org.bouncycastle.asn1.ASN1InputStream“ java.io.IOException:DER长度超过4个字节:63” - Need help understanding org.bouncycastle.asn1.ASN1InputStream “java.io.IOException: DER length more than 4 bytes: 63” java中如何使用bouncycastle解析ASN.1对象并获取数据 - How to parse the ASN.1 object and get the data using bouncycastle in java close是否会抛出IOException? - Does close ever throw an IOException? org.bouncycastle.asn1.ASN1ObjectIdentifier“的签名者信息与签名者信息不匹配 - org.bouncycastle.asn1.ASN1ObjectIdentifier"'s signer information does not match signer information 在java中为什么FileWriter抛出IOException而FileOutputStream抛出FileNotFoundException的原因完全相同 - In java why does FileWriter throw IOException while FileOutputStream throw FileNotFoundException for the exact same reasons
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM