1/*******************************************************************************
2 * Copyright (c) 2009, 2015 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.internal.data;
13
14import java.io.DataInputStream;
15import java.io.IOException;
16import java.io.InputStream;
17
18/**
19 * Additional data input methods for compact storage of data structures.
20 *
21 * @see CompactDataOutput
22 */
23public class CompactDataInput extends DataInputStream {
24
25	/**
26	 * Creates a new {@link CompactDataInput} that uses the specified underlying
27	 * input stream.
28	 *
29	 * @param in
30	 *            underlying input stream
31	 */
32	public CompactDataInput(final InputStream in) {
33		super(in);
34	}
35
36	/**
37	 * Reads a variable length representation of an integer value.
38	 *
39	 * @return read value
40	 * @throws IOException
41	 *             if thrown by the underlying stream
42	 */
43	public int readVarInt() throws IOException {
44		final int value = 0xFF & readByte();
45		if ((value & 0x80) == 0) {
46			return value;
47		}
48		return (value & 0x7F) | (readVarInt() << 7);
49	}
50
51	/**
52	 * Reads a boolean array.
53	 *
54	 * @return boolean array
55	 * @throws IOException
56	 *             if thrown by the underlying stream
57	 */
58	public boolean[] readBooleanArray() throws IOException {
59		final boolean[] value = new boolean[readVarInt()];
60		int buffer = 0;
61		for (int i = 0; i < value.length; i++) {
62			if ((i % 8) == 0) {
63				buffer = readByte();
64			}
65			value[i] = (buffer & 0x01) != 0;
66			buffer >>>= 1;
67		}
68		return value;
69	}
70
71}
72