public int read(byte[] b,
int off,
int len) throws IOException {
try {
int read = 0;
outer: while (len > 0) {
if (buffer == null || offset == buffer.length) {
// Refill buffer
while (true) {
if (!reader.hasNext()) {
break outer;
}
int eventType = reader.next();
if (eventType == XMLStreamReader.CHARACTERS ||
eventType == XMLStreamReader.CDATA) {
// Note: this is not entirely correct for encodings such as UTF-16.
// Once IO-158 is implemented, we could avoid this by implementing a
// Reader and using ReaderInputStream.
buffer = reader.getText().getBytes(charset);
offset = 0;
break;
}
}
}
int c = Math.min(len, buffer.length-offset);
System.arraycopy(buffer, offset, b, off, c);
offset += c;
off += c;
len -= c;
read += c;
}
return read == 0 ? -1 : read;
}
catch (XMLStreamException ex) {
IOException ioException = new IOException("Unable to read from XMLStreamReader");
ioException.initCause(ex);
throw ioException;
}
}
|