Home | History | Annotate | Download | only in report
      1 /*******************************************************************************
      2  * Copyright (c) 2009, 2018 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  *******************************************************************************/
     12 package org.jacoco.report;
     13 
     14 import static java.lang.String.format;
     15 
     16 import java.io.BufferedOutputStream;
     17 import java.io.File;
     18 import java.io.FileOutputStream;
     19 import java.io.IOException;
     20 import java.io.OutputStream;
     21 
     22 /**
     23  * Implementation of {@link IMultiReportOutput} that writes files directly to a
     24  * given directory.
     25  */
     26 public class FileMultiReportOutput implements IMultiReportOutput {
     27 
     28 	private final File basedir;
     29 
     30 	/**
     31 	 * Creates a new instance for document output in the given base directory.
     32 	 *
     33 	 * @param basedir
     34 	 *            base directory
     35 	 */
     36 	public FileMultiReportOutput(final File basedir) {
     37 		this.basedir = basedir;
     38 	}
     39 
     40 	public OutputStream createFile(final String path) throws IOException {
     41 		final File file = new File(basedir, path);
     42 		final File parent = file.getParentFile();
     43 		parent.mkdirs();
     44 		if (!parent.isDirectory()) {
     45 			throw new IOException(format("Can't create directory %s.", parent));
     46 		}
     47 		return new BufferedOutputStream(new FileOutputStream(file));
     48 	}
     49 
     50 	public void close() throws IOException {
     51 		// nothing to do here
     52 	}
     53 
     54 }
     55