Home | History | Annotate | Download | only in internal
      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;
     13 
     14 import java.io.ByteArrayInputStream;
     15 import java.io.ByteArrayOutputStream;
     16 import java.io.FilterInputStream;
     17 import java.io.IOException;
     18 import java.io.InputStream;
     19 import java.io.OutputStream;
     20 import java.util.jar.JarInputStream;
     21 import java.util.jar.JarOutputStream;
     22 import java.util.jar.Pack200;
     23 
     24 /**
     25  * Internal wrapper for the weird Pack200 Java API to allow usage with streams.
     26  */
     27 public final class Pack200Streams {
     28 
     29 	/**
     30 	 * Unpack a stream in Pack200 format into a stream in JAR/ZIP format.
     31 	 *
     32 	 * @param input
     33 	 *            stream in Pack200 format
     34 	 * @return stream in JAR/ZIP format
     35 	 * @throws IOException
     36 	 *             in case of errors with the streams
     37 	 */
     38 	public static InputStream unpack(final InputStream input)
     39 			throws IOException {
     40 		final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
     41 		final JarOutputStream jar = new JarOutputStream(buffer);
     42 		Pack200.newUnpacker().unpack(new NoCloseInput(input), jar);
     43 		jar.finish();
     44 		return new ByteArrayInputStream(buffer.toByteArray());
     45 	}
     46 
     47 	/**
     48 	 * Packs a buffer in JAR/ZIP format into a stream in Pack200 format.
     49 	 *
     50 	 * @param source
     51 	 *            source in JAR/ZIP format
     52 	 * @param output
     53 	 *            stream in Pack200 format
     54 	 * @throws IOException
     55 	 *             in case of errors with the streams
     56 	 */
     57 	public static void pack(final byte[] source, final OutputStream output)
     58 			throws IOException {
     59 		final JarInputStream jar = new JarInputStream(new ByteArrayInputStream(
     60 				source));
     61 		Pack200.newPacker().pack(jar, output);
     62 	}
     63 
     64 	private static class NoCloseInput extends FilterInputStream {
     65 		protected NoCloseInput(final InputStream in) {
     66 			super(in);
     67 		}
     68 
     69 		@Override
     70 		public void close() throws IOException {
     71 			// do not close the underlying stream
     72 		}
     73 	}
     74 
     75 	private Pack200Streams() {
     76 	}
     77 
     78 }
     79