private File unpack200(String pluginKey, File compressedFile) { LOGGER.debug("Unpacking plugin {}", pluginKey); File jar = newTempFile(); try (InputStream input = new GZIPInputStream(new BufferedInputStream(FileUtils.openInputStream(compressedFile))); JarOutputStream output = new JarOutputStream(new BufferedOutputStream(FileUtils.openOutputStream(jar)))) { Pack200.newUnpacker().unpack(input, output); } catch (IOException e) { throw new IllegalStateException(format("Fail to download plugin [%s]. Pack200 error.", pluginKey), e); } return jar; }
try (FileOutputStream fos = new FileOutputStream(tempFile); JarFile jarFile = new JarFile(from)) { final Pack200.Packer packer = Pack200.newPacker(); packer.properties().putAll(props); packer.pack(jarFile, fos); final Pack200.Unpacker unpacker = Pack200.newUnpacker(); try (JarOutputStream jos = new JarOutputStream(new FileOutputStream(to))) { unpacker.unpack(tempFile, jos);
private static void pack200(Path jarPath, Path toPack200Path, String pluginKey) { Profiler profiler = Profiler.create(LOG); profiler.startInfo("Compressing plugin " + pluginKey + " [pack200]"); try (JarInputStream in = new JarInputStream(new BufferedInputStream(Files.newInputStream(jarPath))); OutputStream out = new GZIPOutputStream(new BufferedOutputStream(Files.newOutputStream(toPack200Path)))) { Pack200.newPacker().pack(in, out); } catch (IOException e) { throw new IllegalStateException(String.format("Fail to pack200 plugin [%s] '%s' to '%s'", pluginKey, jarPath, toPack200Path), e); } profiler.stopInfo(); }
public void finish() throws IOException { if (!finished) { finished = true; final Pack200.Packer p = Pack200.newPacker(); if (properties != null) { p.properties().putAll(properties); } try (JarInputStream ji = new JarInputStream(streamBridge.getInput())) { p.pack(ji, originalOutput); } } } }
private Pack200CompressorInputStream(final InputStream in, final File f, final Pack200Strategy mode, final Map<String, String> props) throws IOException { originalInput = in; streamBridge = mode.newStreamBridge(); try (final JarOutputStream jarOut = new JarOutputStream(streamBridge)) { final Pack200.Unpacker u = Pack200.newUnpacker(); if (props != null) { u.properties().putAll(props); } if (f == null) { u.unpack(new FilterInputStream(in) { @Override public void close() { // unpack would close this stream but we // want to give the user code more control } }, jarOut); } else { u.unpack(f, jarOut); } } }
private File packAndUnpackJar(File source) throws IOException { File packed = temp.newFile(); try (JarInputStream in = new JarInputStream(new BufferedInputStream(Files.newInputStream(source.toPath()))); OutputStream out = new GZIPOutputStream(new BufferedOutputStream(Files.newOutputStream(packed.toPath())))) { Pack200.newPacker().pack(in, out); } File to = temp.newFile(); try (InputStream input = new GZIPInputStream(new BufferedInputStream(Files.newInputStream(packed.toPath()))); JarOutputStream output = new JarOutputStream(new BufferedOutputStream(Files.newOutputStream(to.toPath())))) { Pack200.newUnpacker().unpack(input, output); } catch (IOException e) { throw new IllegalStateException(e); } return to; }
/** * Enqueue download of file with a MD5 that may not be returned (null) or not valid */ private void enqueueCompressedDownload(FileAndMd5 jar, boolean validMd5) throws IOException { Buffer body = new Buffer(); ByteArrayOutputStream bytes = new ByteArrayOutputStream(); try (JarInputStream in = new JarInputStream(new BufferedInputStream(Files.newInputStream(jar.file.toPath()))); OutputStream output = new GZIPOutputStream(new BufferedOutputStream(bytes))) { Pack200.newPacker().pack(in, output); } body.write(bytes.toByteArray()); MockResponse response = new MockResponse().setBody(body); response.setHeader("Sonar-MD5", validMd5 ? DigestUtils.md5Hex(bytes.toByteArray()) : "invalid_hash"); response.setHeader("Sonar-UncompressedMD5", jar.md5); response.setHeader("Sonar-Compression", "pack200"); server.enqueue(response); }
private void unpack200(Path compressedFile, Path jarFile) { logger.debug("Unpacking plugin " + compressedFile); Pack200.Unpacker unpacker = Pack200.newUnpacker(); try { try (JarOutputStream jarStream = new JarOutputStream(new BufferedOutputStream(Files.newOutputStream(jarFile))); InputStream in = new GZIPInputStream(new BufferedInputStream(Files.newInputStream(compressedFile)))) { unpacker.unpack(in, jarStream); } } catch (IOException e) { throw new IllegalStateException(e); } }
try (FileOutputStream fos = new FileOutputStream(tempFile); JarFile jarFile = new JarFile(from)) { final Pack200.Packer packer = Pack200.newPacker(); packer.properties().putAll(props); packer.pack(jarFile, fos); final Pack200.Unpacker unpacker = Pack200.newUnpacker(); try (JarOutputStream jos = new JarOutputStream(new FileOutputStream(to))) { unpacker.unpack(tempFile, jos);
private Packer newPacker() { Packer packer = Pack200.newPacker(); // From Pack200.Packer javadoc: // ... the segment limit may also need to be set to "-1", // to prevent accidental variation of segment boundaries as class file sizes change slightly packer.properties().put(Packer.SEGMENT_LIMIT, "-1"); return packer; }
/** * Unpacks a pack200 packed jar file from {@code packedJar} into {@code target}. If {@code * packedJar} has a {@code .gz} extension, it will be gunzipped first. */ public static void unpackPacked200Jar (File packedJar, File target) throws IOException { try (InputStream packJarIn = new FileInputStream(packedJar); JarOutputStream jarOut = new JarOutputStream(new FileOutputStream(target))) { boolean gz = (packedJar.getName().endsWith(".gz") || packedJar.getName().endsWith(".gz_new")); try (InputStream packJarIn2 = (gz ? new GZIPInputStream(packJarIn) : packJarIn)) { Pack200.Unpacker unpacker = Pack200.newUnpacker(); unpacker.unpack(packJarIn2, jarOut); } } }
try (FileOutputStream fos = new FileOutputStream(tempFile); JarFile jarFile = new JarFile(from)) { final Pack200.Packer packer = Pack200.newPacker(); packer.properties().putAll(props); packer.pack(jarFile, fos); final Pack200.Unpacker unpacker = Pack200.newUnpacker(); try (JarOutputStream jos = new JarOutputStream(new FileOutputStream(to))) { unpacker.unpack(tempFile, jos);
/** * Packs a buffer in JAR/ZIP format into a stream in Pack200 format. * * @param source * source in JAR/ZIP format * @param output * stream in Pack200 format * @throws IOException * in case of errors with the streams */ public static void pack(final byte[] source, final OutputStream output) throws IOException { final JarInputStream jar = new JarInputStream(new ByteArrayInputStream( source)); Pack200.newPacker().pack(jar, output); }
protected void unpack(File packFile, File jarFile) throws IOException, FileNotFoundException { InputStream is = new GZIPInputStream(new BufferedInputStream(new FileInputStream(packFile))); try { JarOutputStream os = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(jarFile))); try { Unpacker unpacker = Pack200.newUnpacker(); unpacker.unpack(is, os); } finally { close(os); } } finally { close(is); } }
private static void pack200(Path jarPath, Path toPack200Path, String pluginKey) { Profiler profiler = Profiler.create(LOG); profiler.startInfo("Compressing plugin " + pluginKey + " [pack200]"); try (JarInputStream in = new JarInputStream(new BufferedInputStream(Files.newInputStream(jarPath))); OutputStream out = new GZIPOutputStream(new BufferedOutputStream(Files.newOutputStream(toPack200Path)))) { Pack200.newPacker().pack(in, out); } catch (IOException e) { throw new IllegalStateException(String.format("Fail to pack200 plugin [%s] '%s' to '%s'", pluginKey, jarPath, toPack200Path), e); } profiler.stopInfo(); }
private File unpack200(String pluginKey, File compressedFile) { LOGGER.debug("Unpacking plugin {}", pluginKey); File jar = newTempFile(); try (InputStream input = new GZIPInputStream(new BufferedInputStream(FileUtils.openInputStream(compressedFile))); JarOutputStream output = new JarOutputStream(new BufferedOutputStream(FileUtils.openOutputStream(jar)))) { Pack200.newUnpacker().unpack(input, output); } catch (IOException e) { throw new IllegalStateException(format("Fail to download plugin [%s]. Pack200 error.", pluginKey), e); } return jar; }
private byte[] pack200(byte[] data) throws Exception { JarInputStream in = new JarInputStream(new ByteArrayInputStream(data)); ByteArrayOutputStream out = new ByteArrayOutputStream(); Packer packer = Pack200.newPacker(); SortedMap<String, String> props = packer.properties(); props.put(Packer.EFFORT, "9"); props.put(Packer.KEEP_FILE_ORDER, Packer.TRUE); props.put(Packer.UNKNOWN_ATTRIBUTE, Packer.PASS); final PrintStream err = new PrintStream(System.err); System.setErr(new PrintStream(ByteStreams.nullOutputStream())); packer.pack(in, out); System.setErr(err); in.close(); out.close(); return out.toByteArray(); }
public static InputStream unwrapPack200(InputStream packed) throws IOException { BufferedInputStream buffered = new BufferedInputStream(packed); buffered.mark(4); byte[] magic = new byte[4]; buffered.read(magic, 0, 4); buffered.reset(); InputStream in = buffered; if (magic[0] == (byte) 0x1F && magic[1] == (byte) 0x8B && magic[2] == (byte) 0x08) { // this is a gziped pack200 in = new GZIPInputStream(in); } Unpacker unpacker = Pack200.newUnpacker(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); JarOutputStream jar = new JarOutputStream(baos); unpacker.unpack(new UncloseInputStream(in), jar); jar.close(); return new ByteArrayInputStream(baos.toByteArray()); }
private Pack200.Packer createPack200Packer(PackFile packFile) { Pack200.Packer packer = Pack200.newPacker(); Map<String, String> defaultPackerProperties = packer.properties(); Map<String,String> localPackerProperties = packFile.getPack200Properties(); if (localPackerProperties != null) { defaultPackerProperties.putAll(localPackerProperties); } return packer; }
private static byte[] unpack(byte[] data, File path) throws IOException { File output = new File(path.getAbsolutePath() + ".unpacked"); if (output.exists()) { output.delete(); } FileOutputStream jar = new FileOutputStream(output); JarOutputStream jos = new JarOutputStream(jar); Pack200.newUnpacker().unpack(new ByteArrayInputStream(data), jos); jos.close(); jar.close(); byte[] unpacked = Files.toByteArray(output); if (!DEBUG_SAVE_STAGES) { output.delete(); } return unpacked; }