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 *******************************************************************************/
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 ISingleReportOutput} that writes the file directly
24 * to a given location.
25 *
26 * @author Marc R. Hoffmann
27 * @version 0.4.1.20101007204400
28 */
29public class FileSingleReportOutput implements ISingleReportOutput {
30
31 private final File file;
32
33 /**
34 * Creates a new instance for document output to the given location.
35 *
36 * @param file
37 */
38 public FileSingleReportOutput(final File file) {
39 this.file = file;
40 }
41
42 public OutputStream createFile() throws IOException {
43 final File parent = file.getParentFile();
44 parent.mkdirs();
45 if (!parent.isDirectory()) {
46 throw new IOException(format("Can't create directory %s.", parent));
47 }
48 return new BufferedOutputStream(new FileOutputStream(file));
49 }
50
51}