Home | History | Annotate | Download | only in data
      1 /*******************************************************************************
      2  * Copyright (c) 2009, 2018 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  *******************************************************************************/
     12 package org.jacoco.core.internal.data;
     13 
     14 import java.io.DataOutputStream;
     15 import java.io.IOException;
     16 import java.io.OutputStream;
     17 
     18 /**
     19  * Additional data output methods for compact storage of data structures.
     20  *
     21  * @see CompactDataInput
     22  */
     23 public class CompactDataOutput extends DataOutputStream {
     24 
     25 	/**
     26 	 * Creates a new {@link CompactDataOutput} instance that writes data to the
     27 	 * specified underlying output stream
     28 	 *
     29 	 * @param out
     30 	 *            underlying output stream
     31 	 */
     32 	public CompactDataOutput(final OutputStream out) {
     33 		super(out);
     34 	}
     35 
     36 	/**
     37 	 * Writes a variable length representation of an integer value that reduces
     38 	 * the number of written bytes for small positive values. Depending on the
     39 	 * given value 1 to 5 bytes will be written to the underlying stream.
     40 	 *
     41 	 * @param value
     42 	 *            value to write
     43 	 * @throws IOException
     44 	 *             if thrown by the underlying stream
     45 	 */
     46 	public void writeVarInt(final int value) throws IOException {
     47 		if ((value & 0xFFFFFF80) == 0) {
     48 			writeByte(value);
     49 		} else {
     50 			writeByte(0x80 | (value & 0x7F));
     51 			writeVarInt(value >>> 7);
     52 		}
     53 	}
     54 
     55 	/**
     56 	 * Writes a boolean array. Internally a sequence of boolean values is packed
     57 	 * into single bits.
     58 	 *
     59 	 * @param value
     60 	 *            boolean array
     61 	 * @throws IOException
     62 	 *             if thrown by the underlying stream
     63 	 */
     64 	public void writeBooleanArray(final boolean[] value) throws IOException {
     65 		writeVarInt(value.length);
     66 		int buffer = 0;
     67 		int bufferSize = 0;
     68 		for (final boolean b : value) {
     69 			if (b) {
     70 				buffer |= 0x01 << bufferSize;
     71 			}
     72 			if (++bufferSize == 8) {
     73 				writeByte(buffer);
     74 				buffer = 0;
     75 				bufferSize = 0;
     76 			}
     77 		}
     78 		if (bufferSize > 0) {
     79 			writeByte(buffer);
     80 		}
     81 	}
     82 
     83 }
     84