@Override public int read(char[] cbuf, int off, int len) throws IOException { int i = 0; int c = 0; for (i = 0; i < len; i++) { c = read(); if (c == -1) { if (i == 0) { return -1; } break; } cbuf[i+off] = (char)c; } return i; }
@Override public Reader getCharacterStream() throws SQLException { try { return new XMLReader(getSource(StAXSource.class), XMLOutputFactory.newFactory()); } catch (XMLStreamException e) { throw new SQLException(e); } catch (FactoryConfigurationError e) { throw new SQLException(e); } }
@Test public void testStreaming() throws Exception { StringBuilder xmlBuilder = new StringBuilder(); xmlBuilder.append("<root>"); for (int i = 0; i < 1000; i++) { xmlBuilder.append("<a></a>"); xmlBuilder.append("<b></b>"); } xmlBuilder.append("</root>"); String xml = xmlBuilder.toString(); StAXSource source = new StAXSource(XMLType.getXmlInputFactory().createXMLEventReader(new StringReader(xml))); XMLReader is = new XMLReader(source, XMLOutputFactory.newFactory()); String str = ObjectConverterUtil.convertToString(is); assertTrue(str, str.startsWith("<?xml version=\"1.0\"")); //omit document declaration assertEquals(xml, str.substring(str.indexOf("><") + 1)); }