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 *******************************************************************************/
12package org.jacoco.core.data;
13
14import java.io.ByteArrayOutputStream;
15import java.io.IOException;
16import java.io.OutputStream;
17
18/**
19 * Serialization of execution data into binary streams.
20 *
21 * @author Marc R. Hoffmann
22 * @version 0.4.1.20101007204400
23 */
24public class ExecutionDataWriter implements ISessionInfoVisitor,
25 IExecutionDataVisitor {
26
27 /** File format version, will be incremented for each incompatible change. */
28 public static final char FORMAT_VERSION = 0x1005;
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 session information. */
37 public static final byte BLOCK_SESSIONINFO = 0x10;
38
39 /** Block identifier for execution data of a single class. */
40 public static final byte BLOCK_EXECUTIONDATA = 0x11;
41
42 /** Underlying data output */
43 protected final CompactDataOutput out;
44
45 /**
46 * Creates a new writer based on the given output stream. Depending on the
47 * nature of the underlying stream output should be buffered as most data is
48 * written in single bytes.
49 *
50 * @param output
51 * binary stream to write execution data to
52 * @throws IOException
53 * if the header can't be written
54 */
55 public ExecutionDataWriter(final OutputStream output) throws IOException {
56 this.out = new CompactDataOutput(output);
57 writeHeader();
58 }
59
60 /**
61 * Writes an file header to identify the stream and its protocol version.
62 *
63 * @throws IOException
64 */
65 private void writeHeader() throws IOException {
66 out.writeByte(BLOCK_HEADER);
67 out.writeChar(MAGIC_NUMBER);
68 out.writeChar(FORMAT_VERSION);
69 }
70
71 /**
72 * Flushes the underlying stream.
73 *
74 * @throws IOException
75 */
76 public void flush() throws IOException {
77 out.flush();
78 }
79
80 public void visitSessionInfo(final SessionInfo info) {
81 try {
82 out.writeByte(BLOCK_SESSIONINFO);
83 out.writeUTF(info.getId());
84 out.writeLong(info.getStartTimeStamp());
85 out.writeLong(info.getDumpTimeStamp());
86 } catch (final IOException e) {
87 throw new RuntimeException(e);
88 }
89 }
90
91 public void visitClassExecution(final ExecutionData data) {
92 try {
93 out.writeByte(BLOCK_EXECUTIONDATA);
94 out.writeLong(data.getId());
95 out.writeUTF(data.getName());
96 out.writeBooleanArray(data.getData());
97 } catch (final IOException e) {
98 throw new RuntimeException(e);
99 }
100 }
101
102 /**
103 * Returns the first bytes of a file that represents a valid execution data
104 * file. In any case every execution data file starts with the three bytes
105 * <code>0x01 0xC0 0xC0</code>.
106 *
107 * @return first bytes of a execution data file
108 */
109 public static final byte[] getFileHeader() {
110 final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
111 try {
112 new ExecutionDataWriter(buffer);
113 } catch (final IOException e) {
114 // Must not happen with ByteArrayOutputStream
115 throw new AssertionError(e);
116 }
117 return buffer.toByteArray();
118 }
119
120}