
org.dellroad.stuff.jibx.XMLDocumentInputStream Maven / Gradle / Ivy
Show all versions of dellroad-stuff-jibx Show documentation
/*
* Copyright (C) 2011 Archie L. Cobbs. All rights reserved.
*/
package org.dellroad.stuff.jibx;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.dellroad.stuff.io.InputStreamReader;
import org.jibx.runtime.JiBXException;
/**
* {@link InputStream} over which XML documents are passed. This class is a companion to {@link XMLDocumentOutputStream}.
*
*
* XML documents are converted into Java objects via {@link JiBXUtil#readObject(Class, InputStream) JiBXUtil.readObject()}.
*
*
* Instances of this class are thread-safe.
*
* @param XML document type
* @see XMLDocumentOutputStream
*/
public class XMLDocumentInputStream {
private final Class type;
private final InputStreamReader input;
/**
* Constructor.
*
* @param type Java type for XML documents
* @param input data source
*/
public XMLDocumentInputStream(Class type, InputStream input) {
if (type == null)
throw new IllegalArgumentException("null type");
if (input == null)
throw new IllegalArgumentException("null input");
this.type = type;
this.input = new InputStreamReader(new BufferedInputStream(input));
}
/**
* Read the next XML document, parsed and objectified.
*
* @return decoded object or {@code null} on EOF
* @throws IOException if an I/O error occurs
* @throws JiBXException if JiBX parse fails
*/
public T read() throws IOException, JiBXException {
InputStream xml = this.input.read();
if (xml == null)
return null;
try {
return JiBXUtil.readObject(this.type, xml);
} finally {
try {
xml.close();
} catch (IOException e) {
// ignore
}
}
}
public void close() throws IOException {
this.input.close();
}
}