package org.apache.commons.compress.compressors.pack200;
import java.io.File;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.jar.JarOutputStream;
import java.util.jar.Pack200;
import org.apache.commons.compress.compressors.CompressorInputStream;
private final InputStream originalInput;
private final StreamBridge streamBridge;
throws IOException {
this(in, Pack200Strategy.IN_MEMORY);
}
final Pack200Strategy mode)
throws IOException {
this(in, null, mode, null);
}
final Map<String, String> props)
throws IOException {
this(in, Pack200Strategy.IN_MEMORY, props);
}
final Pack200Strategy mode,
final Map<String, String> props)
throws IOException {
this(in, null, mode, props);
}
this(f, Pack200Strategy.IN_MEMORY);
}
throws IOException {
this(null, f, mode, null);
}
final Map<String, String> props)
throws IOException {
this(f, Pack200Strategy.IN_MEMORY, props);
}
final Map<String, String> props)
throws IOException {
this(null, f, mode, props);
}
final Pack200Strategy mode,
final Map<String, String> props)
throws IOException {
originalInput = in;
streamBridge = mode.newStreamBridge();
JarOutputStream jarOut = new JarOutputStream(streamBridge);
Pack200.Unpacker u = Pack200.newUnpacker();
if (props != null) {
u.properties().putAll(props);
}
if (f == null) {
u.unpack(new FilterInputStream(in) {
@Override
}
},
jarOut);
} else {
u.unpack(f, jarOut);
}
jarOut.close();
}
@Override
public int read()
throws IOException {
return streamBridge.getInput().read();
}
@Override
public int read(
byte[] b)
throws IOException {
return streamBridge.getInput().read(b);
}
@Override
public int read(
byte[] b,
int off,
int count)
throws IOException {
return streamBridge.getInput().read(b, off, count);
}
@Override
return streamBridge.getInput().available();
}
@Override
try {
return streamBridge.getInput().markSupported();
} catch (IOException ex) {
return false;
}
}
@Override
public void mark(
int limit) {
try {
streamBridge.getInput().mark(limit);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
@Override
public void reset()
throws IOException {
streamBridge.getInput().reset();
}
@Override
public long skip(
long count)
throws IOException {
return streamBridge.getInput().skip(count);
}
@Override
public void close()
throws IOException {
try {
streamBridge.stop();
} finally {
if (originalInput != null) {
originalInput.close();
}
}
}
private static final byte[] CAFE_DOOD = new byte[] {
(byte) 0xCA, (byte) 0xFE, (byte) 0xD0, (byte) 0x0D
};
private static final int SIG_LENGTH = CAFE_DOOD.length;
public static boolean matches(
byte[] signature,
int length) {
if (length < SIG_LENGTH) {
return false;
}
for (int i = 0; i < SIG_LENGTH; i++) {
if (signature[i] != CAFE_DOOD[i]) {
return false;
}
}
return true;
}
}