ExecutionDataWriter.java

    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.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 = 0x1003;
   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[][] blockdata) {
   66        try {
   67            out.writeByte(BLOCK_EXECUTIONDATA);
   68            out.writeLong(id);
   69            out.writeUTF(name);
   70            out.writeVarInt(blockdata.length);
   71            // 1. Write all block sizes
   72            for (final boolean[] m : blockdata) {
   73                out.writeVarInt(m.length);
   74            }
   75            // 2. Write block data in one sequence for better packing
   76            for (final boolean[] m : blockdata) {
   77                for (final boolean b : m) {
   78                    out.writePackedBoolean(b);
   79                }
   80            }
   81            out.finishPackedBoolean();
   82        } catch (final IOException e) {
   83            throw new RuntimeException(e);
   84        }
   85    }
   86
   87    /**
   88     * Returns the first bytes of a file that represents a valid execution data
   89     * file. In any case every execution data file starts with the three bytes
   90     * <code>0x01 0xC0 0xC0</code>.
   91     * 
   92     * @return first bytes of a execution data file
   93     */
   94    public static final byte[] getFileHeader() {
   95        final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
   96        try {
   97            new ExecutionDataWriter(buffer).writeHeader();
   98        } catch (final IOException e) {
   99            // Must not happen with ByteArrayOutputStream
  100            throw new RuntimeException(e);
  101        }
  102        return buffer.toByteArray();
  103    }
  104
  105}