ExecutionDataWriter.java revision d83c6185fa2419d7f01671611eecdc5297325fbc
1/*******************************************************************************
2 * Copyright (c) 2009 Mountainminds GmbH & Co. KG and others
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.core.data;
14
15import java.io.DataOutput;
16import java.io.DataOutputStream;
17import java.io.IOException;
18import java.io.OutputStream;
19
20/**
21 * Serialization of execution data into binary streams.
22 *
23 * TODO: Add "magic number" header protocol version, more efficient storage
24 *
25 * @author Marc R. Hoffmann
26 * @version $Revision: $
27 */
28public class ExecutionDataWriter implements IExecutionDataVisitor {
29
30	private final DataOutput output;
31
32	/**
33	 * Creates a new writer based on the given data output.
34	 *
35	 * @param output
36	 *            data output to write execution data to
37	 */
38	public ExecutionDataWriter(final DataOutput output) {
39		this.output = output;
40	}
41
42	/**
43	 * Creates a new writer based on the given output stream.
44	 *
45	 * @param output
46	 *            binary stream to write execution data to
47	 */
48	public ExecutionDataWriter(final OutputStream output) {
49		this((DataOutput) new DataOutputStream(output));
50	}
51
52	public void visitClassExecution(final long id, final boolean[][] blockdata) {
53		try {
54			output.writeLong(id);
55			output.writeInt(blockdata.length);
56			for (final boolean[] m : blockdata) {
57				output.writeInt(m.length);
58				for (final boolean b : m) {
59					output.writeBoolean(b);
60				}
61			}
62		} catch (final IOException e) {
63			throw new RuntimeException(e);
64		}
65	}
66
67}
68