FileSingleReportOutput.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 * $Id: $
   12 *******************************************************************************/
   13package org.jacoco.report;
   14
   15import static java.lang.String.format;
   16
   17import java.io.BufferedOutputStream;
   18import java.io.File;
   19import java.io.FileOutputStream;
   20import java.io.IOException;
   21import java.io.OutputStream;
   22
   23/**
   24 * Implementation of {@link ISingleReportOutput} that writes the file directly
   25 * to a given location.
   26 * 
   27 * @author Marc R. Hoffmann
   28 * @version $Revision: $
   29 */
   30public class FileSingleReportOutput implements ISingleReportOutput {
   31
   32    private final File file;
   33
   34    /**
   35     * Creates a new instance for document output to the given location.
   36     * 
   37     * @param file
   38     */
   39    public FileSingleReportOutput(final File file) {
   40        this.file = file;
   41    }
   42
   43    public OutputStream createFile() throws IOException {
   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}