private static void checkCRC(Element element) throws CRCMismatchException { Element CRCElement = element.getChild(CRC_ELEMENT); if (CRCElement != null) { String crc = CRCElement.getText(); // the length in bytes is the number of characters divided by 2 int crcLength = crc.length() / 2; if (!crc.equals(element.getHash(crcLength, CRC_ELEMENT))) { throw new CRCMismatchException(); } } }
// public static void write(OutputStream stream, Element element, int hashLength) throws // XMLStreamException { // XMLOutputFactory xof = XMLOutputFactory.newInstance(); // IndentingXMLStreamWriter xtw = new // IndentingXMLStreamWriter(xof.createXMLStreamWriter(stream)); // writeXMLStreamWriter(xtw, element, hashLength); // } // private static void writeXMLStreamWriter(XMLStreamWriter xtw, Element element, int hashLength) throws XMLStreamException { if (hashLength > 0) { String hash = element.getHash(hashLength); Element hashElement = new Element(CRC_ELEMENT); hashElement.setText(hash); element.addChild(hashElement); } writeElement(xtw, element); xtw.writeEndDocument(); xtw.flush(); xtw.close(); if (hashLength > 0) { element.removeChildren(CRC_ELEMENT); } }