[英]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
?
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.