001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019 020package org.apache.commons.compress.compressors.pack200; 021 022import java.io.IOException; 023import java.io.OutputStream; 024import java.util.Map; 025import java.util.jar.JarInputStream; 026import java.util.jar.Pack200; 027 028import org.apache.commons.compress.compressors.CompressorOutputStream; 029 030/** 031 * An output stream that compresses using the Pack200 format. 032 * 033 * @NotThreadSafe 034 * @since 1.3 035 */ 036public class Pack200CompressorOutputStream extends CompressorOutputStream { 037 private boolean finished = false; 038 private final OutputStream originalOutput; 039 private final StreamBridge streamBridge; 040 private final Map<String, String> properties; 041 042 /** 043 * Compresses the given stream, caching the compressed data in 044 * memory. 045 * 046 * @param out the stream to write to 047 * @throws IOException if writing fails 048 */ 049 public Pack200CompressorOutputStream(final OutputStream out) 050 throws IOException { 051 this(out, Pack200Strategy.IN_MEMORY); 052 } 053 054 /** 055 * Compresses the given stream using the given strategy to cache 056 * the results. 057 * 058 * @param out the stream to write to 059 * @param mode the strategy to use 060 * @throws IOException if writing fails 061 */ 062 public Pack200CompressorOutputStream(final OutputStream out, 063 final Pack200Strategy mode) 064 throws IOException { 065 this(out, mode, null); 066 } 067 068 /** 069 * Compresses the given stream, caching the compressed data in 070 * memory and using the given properties. 071 * 072 * @param out the stream to write to 073 * @param props Pack200 properties to use 074 * @throws IOException if writing fails 075 */ 076 public Pack200CompressorOutputStream(final OutputStream out, 077 final Map<String, String> props) 078 throws IOException { 079 this(out, Pack200Strategy.IN_MEMORY, props); 080 } 081 082 /** 083 * Compresses the given stream using the given strategy to cache 084 * the results and the given properties. 085 * 086 * @param out the stream to write to 087 * @param mode the strategy to use 088 * @param props Pack200 properties to use 089 * @throws IOException if writing fails 090 */ 091 public Pack200CompressorOutputStream(final OutputStream out, 092 final Pack200Strategy mode, 093 final Map<String, String> props) 094 throws IOException { 095 originalOutput = out; 096 streamBridge = mode.newStreamBridge(); 097 properties = props; 098 } 099 100 @Override 101 public void write(final int b) throws IOException { 102 streamBridge.write(b); 103 } 104 105 @Override 106 public void write(final byte[] b) throws IOException { 107 streamBridge.write(b); 108 } 109 110 @Override 111 public void write(final byte[] b, final int from, final int length) throws IOException { 112 streamBridge.write(b, from, length); 113 } 114 115 @Override 116 public void close() throws IOException { 117 try { 118 finish(); 119 } finally { 120 try { 121 streamBridge.stop(); 122 } finally { 123 originalOutput.close(); 124 } 125 } 126 } 127 128 public void finish() throws IOException { 129 if (!finished) { 130 finished = true; 131 final Pack200.Packer p = Pack200.newPacker(); 132 if (properties != null) { 133 p.properties().putAll(properties); 134 } 135 try (JarInputStream ji = new JarInputStream(streamBridge.getInput())) { 136 p.pack(ji, originalOutput); 137 } 138 } 139 } 140}