FileMultiReportOutput.java

    1/*******************************************************************************
    2 * Copyright (c) 2009, 2010 Mountainminds GmbH & Co. KG and Contributors
    3 * All rights reserved. This program and the accompanying materials
    4 * are made available under the terms of the Eclipse Public License v1.0
    5 * which accompanies this distribution, and is available at
    6 * http://www.eclipse.org/legal/epl-v10.html
    7 *
    8 * Contributors:
    9 *    Marc R. Hoffmann - initial API and implementation
   10 *    
   11 *******************************************************************************/
   12package org.jacoco.report;
   13
   14import static java.lang.String.format;
   15
   16import java.io.BufferedOutputStream;
   17import java.io.File;
   18import java.io.FileOutputStream;
   19import java.io.IOException;
   20import java.io.OutputStream;
   21
   22/**
   23 * Implementation of {@link IMultiReportOutput} that writes files directly to a
   24 * given directory.
   25 * 
   26 * @author Marc R. Hoffmann
   27 * @version 0.4.1.20101007204400
   28 */
   29public class FileMultiReportOutput implements IMultiReportOutput {
   30
   31    private final File basedir;
   32
   33    /**
   34     * Creates a new instance for document output in the given base directory.
   35     * 
   36     * @param basedir
   37     */
   38    public FileMultiReportOutput(final File basedir) {
   39        this.basedir = basedir;
   40    }
   41
   42    public OutputStream createFile(final String path) throws IOException {
   43        final File file = new File(basedir, path);
   44        final File parent = file.getParentFile();
   45        parent.mkdirs();
   46        if (!parent.isDirectory()) {
   47            throw new IOException(format("Can't create directory %s.", parent));
   48        }
   49        return new BufferedOutputStream(new FileOutputStream(file));
   50    }
   51
   52}