ExecutionDataWriter.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.core.data;
   14
   15import java.io.ByteArrayOutputStream;
   16import java.io.IOException;
   17import java.io.OutputStream;
   18
   19/**
   20 * Serialization of execution data into binary streams.
   21 * 
   22 * @author Marc R. Hoffmann
   23 * @version $Revision: $
   24 */
   25public class ExecutionDataWriter implements IExecutionDataVisitor {
   26
   27    /** File format version, will be incremented for each incompatible change. */
   28    public static final char FORMAT_VERSION = 0x1004;
   29
   30    /** Magic number in header for file format identification. */
   31    public static final char MAGIC_NUMBER = 0xC0C0;
   32
   33    /** Block identifier for file headers. */
   34    public static final byte BLOCK_HEADER = 0x01;
   35
   36    /** Block identifier for execution data of a single class. */
   37    public static final byte BLOCK_EXECUTIONDATA = 0x10;
   38
   39    private final CompactDataOutput out;
   40
   41    /**
   42     * Creates a new writer based on the given output stream. Depending on the
   43     * nature of the underlying stream output should be buffered as most data is
   44     * written in single bytes.
   45     * 
   46     * @param output
   47     *            binary stream to write execution data to
   48     */
   49    public ExecutionDataWriter(final OutputStream output) {
   50        this.out = new CompactDataOutput(output);
   51    }
   52
   53    /**
   54     * Writes an file header to identify the stream and its protocol version.
   55     * 
   56     * @throws IOException
   57     */
   58    public void writeHeader() throws IOException {
   59        out.writeByte(BLOCK_HEADER);
   60        out.writeChar(MAGIC_NUMBER);
   61        out.writeChar(FORMAT_VERSION);
   62    }
   63
   64    public void visitClassExecution(final long id, final String name,
   65            final boolean[] data) {
   66        try {
   67            out.writeByte(BLOCK_EXECUTIONDATA);
   68            out.writeLong(id);
   69            out.writeUTF(name);
   70            out.writeVarInt(data.length);
   71            for (final boolean b : data) {
   72                out.writePackedBoolean(b);
   73            }
   74            out.finishPackedBoolean();
   75        } catch (final IOException e) {
   76            throw new RuntimeException(e);
   77        }
   78    }
   79
   80    /**
   81     * Returns the first bytes of a file that represents a valid execution data
   82     * file. In any case every execution data file starts with the three bytes
   83     * <code>0x01 0xC0 0xC0</code>.
   84     * 
   85     * @return first bytes of a execution data file
   86     */
   87    public static final byte[] getFileHeader() {
   88        final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
   89        try {
   90            new ExecutionDataWriter(buffer).writeHeader();
   91        } catch (final IOException e) {
   92            // Must not happen with ByteArrayOutputStream
   93            throw new RuntimeException(e);
   94        }
   95        return buffer.toByteArray();
   96    }
   97
   98}