1
아래 코드 ERROR: 'Invalid byte 1 of 1-byte UTF-8 sequence.'
인코딩 문제 추출하면서 EXI 압축 XML
@Test
void testExiCompression() {
def xml = '<Root><Child id="1">Text</Child><EmptyTag/></Root>'
def compressed = ExiCompressionUtils.compress(xml)
assert ExiCompressionUtils.extract(compressed) == xml
}
class ExiCompressionUtils {
static Transformer transformer = TransformerFactory.newInstance().newTransformer()
static byte[] compress(String xml) {
ByteArrayOutputStream exiOS = new ByteArrayOutputStream()
EXIResult exiResult = new EXIResult(outputStream : exiOS)
XMLReader xmlReader = XMLReaderFactory.createXMLReader()
xmlReader.contentHandler = exiResult.handler
xmlReader.parse(new InputSource(new StringReader(xml)))
def compressed = exiOS.toByteArray()
exiOS.close()
return compressed
}
static String extract(byte[] compressed) {
SAXSource exiSource = new SAXSource(new InputSource(new ByteArrayInputStream(compressed)))
exiSource.setXMLReader(exiSource.reader)
ByteArrayOutputStream exiOS = new ByteArrayOutputStream()
transformer.transform(exiSource, new StreamResult(exiOS)) // fails here
def extracted = exiOS.toString()
exiOS.close()
return compressed
}
}
아래 테스트가 실패하여 EXI 압축 및 압축 해제를 수행하기 위해 필요한 설정을 단순화하기 위해 시도 이것의 밑바닥에 도달 할 수있는 인코딩 전문가가 있습니까?
나는 이해할 수 없다 : 그것은 진정한 자바입니까? 세미콜론이 생략 된 이유는 무엇입니까? 'def 압축 ... '이란 무엇입니까? – Andremoniy
Groovy ... close만큼이나 .class 파일로 컴파일합니다. Groovy로 가져온 몇 가지 바로 가기가 아닌 일반 Java가 될 수 있기 때문에 Java로 레이블을 지정했습니다. 그들은 요즘 어쨌든 대부분의 목적으로 상호 교환이 가능합니다. – jkschneider
@jkschneider 생각해 보았지만 * UTF-8 *로 인코딩 된 테스트 케이스 파일 (XML 포함)은 무엇입니까? 나는 exi에 대해 많이 알지 못한다.하지만 그 오류는 전에는 보아왔다. 보통은 UTF-8 *을 따르지 않는 XML의 인코딩과 관련이있다. – BPaasch