All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.discovery.jasperreports.jasper2word.SimpleJ2WOutputStreamExporterOutput Maven / Gradle / Ivy

The newest version!
package it.discovery.jasperreports.jasper2word;

import net.sf.jasperreports.engine.JRRuntimeException;
import net.sf.jasperreports.export.OutputStreamExporterOutput;

import java.io.*;

/**
 * The exporter output stream for report.
 * @author discovery
 * @date 10/08/15 11.42
 */
public class SimpleJ2WOutputStreamExporterOutput implements OutputStreamExporterOutput {
    /** Close stream after export */
    private boolean toClose;
    /** The output stream */
    private OutputStream output;

    /**
     * Construct the output exporter with the specified output stream.
     * @param output The output stream.
     * @param toClose {@code true} to close after exported, {@code false} otherwise.
     * @throws JRRuntimeException In case {@code output} is {@code null}.
     */
    public SimpleJ2WOutputStreamExporterOutput(OutputStream output, boolean toClose) throws JRRuntimeException {
        if (output == null)
            throw new JRRuntimeException("Invalid output stream: null");
        this.toClose = toClose;
        this.output = output;
    }

    /**
     * Construct the output exporter with the specified file.
     * @param file The output file.
     * @param toClose {@code true} to close after exported, {@code false} otherwise.
     * @throws JRRuntimeException In case {@code file} is {@code null} or IO errors.
     */
    public SimpleJ2WOutputStreamExporterOutput(File file, boolean toClose) throws JRRuntimeException {
        if (file == null)
            throw new JRRuntimeException("Invalid output file: null");
        this.toClose = toClose;
        try {
            this.output = new FileOutputStream(file);
        } catch (FileNotFoundException e) {
            throw new JRRuntimeException("Could not create output file", e);
        }
    }

    /**
     * Construct the output exporter into a {@link ByteArrayOutputStream ByteArrayOutputStream}.
     * @param toClose {@code true} to close after exported, {@code false} otherwise.
     */
    public SimpleJ2WOutputStreamExporterOutput(boolean toClose) {
        this.toClose = toClose;
        this.output = new ByteArrayOutputStream();
    }

    /**
     * Return the output stream.
     * @return The output stream.
     */
    public OutputStream getOutputStream() {
        return this.output;
    }

    /**
     * Return if the stream must be closed.
     * @return {@code true} if the stream must be closed, {@code false} otherwise.
     */
    public boolean isToClose() {
        return toClose;
    }

    /**
     * Close the exporter, if the stream must be closed.
     * @see #isToClose()
     */
    public void close() {
        if (this.toClose) {
            try {
                this.output.close();
            } catch (IOException e) {
                throw new JRRuntimeException("Closing output stream error", e);
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy