本文整理汇总了Java中javax.xml.stream.XMLStreamReader.getEncoding方法的典型用法代码示例。如果您正苦于以下问题:Java XMLStreamReader.getEncoding方法的具体用法?Java XMLStreamReader.getEncoding怎么用?Java XMLStreamReader.getEncoding使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.xml.stream.XMLStreamReader
的用法示例。
在下文中一共展示了XMLStreamReader.getEncoding方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: binaryInputStreamStreamToReader
import javax.xml.stream.XMLStreamReader; //导入方法依赖的package包/类
private Reader binaryInputStreamStreamToReader(ByteArrayOutputStream out) {
try {
// There's got to be an easier way to do this, but I don't feel like coding up Appendix F of the XML Spec myself, when there's a reusable way to do
// it, and we can warn folks away from BINARY xml streams that have to be parsed to determine the character encoding :P
String encoding = "UTF-8";
try {
ByteArrayInputStream bIn = new ByteArrayInputStream(out.toByteArray());
XMLStreamReader reader = this.inputFactory.createXMLStreamReader(bIn);
int eventType = 0;
while ((eventType = reader.next()) != XMLStreamReader.END_DOCUMENT) {
if (eventType == XMLStreamReader.START_DOCUMENT) {
String possibleEncoding = reader.getEncoding();
if (possibleEncoding != null) {
encoding = possibleEncoding;
}
break;
}
}
} catch (Throwable t) {
// ignore, dealt with later when the string can't be parsed into valid XML
}
return new StringReader(new String(out.toByteArray(), encoding));
} catch (UnsupportedEncodingException badEnc) {
throw new RuntimeException(badEnc);
}
}