下面列出了java.util.jar.JarInputStream#close ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public byte[] extractEntry(String entry) throws IOException {
JarInputStream zin = new JarInputStream(new BufferedInputStream(new FileInputStream(jarFile)));
JarEntry currentEntry = null;
while ((currentEntry = zin.getNextJarEntry()) != null) {
if (currentEntry.getName().equals(entry)) {
// currentEntry.getSize() may not be accurate, so read bytes into a stream first
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[4096];
while (true) {
int n = zin.read(buf);
if (n < 0){
break;
}
baos.write(buf, 0, n);
}
zin.close();
return baos.toByteArray();
}
}
zin.close();
return null;
}
static void copyJarFile(JarInputStream in, JarOutputStream out) throws IOException {
if (in.getManifest() != null) {
ZipEntry me = new ZipEntry(JarFile.MANIFEST_NAME);
out.putNextEntry(me);
in.getManifest().write(out);
out.closeEntry();
}
byte[] buffer = new byte[1 << 14];
for (JarEntry je; (je = in.getNextJarEntry()) != null; ) {
out.putNextEntry(je);
for (int nr; 0 < (nr = in.read(buffer)); ) {
out.write(buffer, 0, nr);
}
}
in.close();
markJarFile(out); // add PACK200 comment
}
static void copyJarFile(JarInputStream in, JarOutputStream out) throws IOException {
if (in.getManifest() != null) {
ZipEntry me = new ZipEntry(JarFile.MANIFEST_NAME);
out.putNextEntry(me);
in.getManifest().write(out);
out.closeEntry();
}
byte[] buffer = new byte[1 << 14];
for (JarEntry je; (je = in.getNextJarEntry()) != null; ) {
out.putNextEntry(je);
for (int nr; 0 < (nr = in.read(buffer)); ) {
out.write(buffer, 0, nr);
}
}
in.close();
markJarFile(out); // add PACK200 comment
}
/**
* Takes a JarInputStream and converts into a pack-stream.
* <p>
* Closes its input but not its output. (Pack200 archives are appendable.)
* <p>
* The modification time and deflation hint attributes are not available,
* for the jar-manifest file and the directory containing the file.
*
* @see #MODIFICATION_TIME
* @see #DEFLATION_HINT
* @param in a JarInputStream
* @param out an OutputStream
* @exception IOException if an error is encountered.
*/
public synchronized void pack(JarInputStream in, OutputStream out) throws IOException {
assert (Utils.currentInstance.get() == null);
boolean needUTC = !props.getBoolean(Utils.PACK_DEFAULT_TIMEZONE);
try {
Utils.currentInstance.set(this);
if (needUTC) {
Utils.changeDefaultTimeZoneToUtc();
}
if ("0".equals(props.getProperty(Pack200.Packer.EFFORT))) {
Utils.copyJarFile(in, out);
} else {
(new DoPack()).run(in, out);
}
} finally {
Utils.currentInstance.set(null);
if (needUTC) {
Utils.restoreDefaultTimeZone();
}
in.close();
}
}
/**
* Takes a JarInputStream and converts into a pack-stream.
* <p>
* Closes its input but not its output. (Pack200 archives are appendable.)
* <p>
* The modification time and deflation hint attributes are not available,
* for the jar-manifest file and the directory containing the file.
*
* @see #MODIFICATION_TIME
* @see #DEFLATION_HINT
* @param in a JarInputStream
* @param out an OutputStream
* @exception IOException if an error is encountered.
*/
public synchronized void pack(JarInputStream in, OutputStream out) throws IOException {
assert (Utils.currentInstance.get() == null);
boolean needUTC = !props.getBoolean(Utils.PACK_DEFAULT_TIMEZONE);
try {
Utils.currentInstance.set(this);
if (needUTC) {
Utils.changeDefaultTimeZoneToUtc();
}
if ("0".equals(props.getProperty(Pack200.Packer.EFFORT))) {
Utils.copyJarFile(in, out);
} else {
(new DoPack()).run(in, out);
}
} finally {
Utils.currentInstance.set(null);
if (needUTC) {
Utils.restoreDefaultTimeZone();
}
in.close();
}
}
/**
* Takes a JarInputStream and converts into a pack-stream.
* <p>
* Closes its input but not its output. (Pack200 archives are appendable.)
* <p>
* The modification time and deflation hint attributes are not available,
* for the jar-manifest file and the directory containing the file.
*
* @see #MODIFICATION_TIME
* @see #DEFLATION_HINT
* @param in a JarInputStream
* @param out an OutputStream
* @exception IOException if an error is encountered.
*/
public synchronized void pack(JarInputStream in, OutputStream out) throws IOException {
assert(Utils.currentInstance.get() == null);
TimeZone tz = (props.getBoolean(Utils.PACK_DEFAULT_TIMEZONE)) ? null :
TimeZone.getDefault();
try {
Utils.currentInstance.set(this);
if (tz != null) TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
if ("0".equals(props.getProperty(Pack200.Packer.EFFORT))) {
Utils.copyJarFile(in, out);
} else {
(new DoPack()).run(in, out);
}
} finally {
Utils.currentInstance.set(null);
if (tz != null) TimeZone.setDefault(tz);
in.close();
}
}
static void copyJarFile(JarInputStream in, JarOutputStream out) throws IOException {
if (in.getManifest() != null) {
ZipEntry me = new ZipEntry(JarFile.MANIFEST_NAME);
out.putNextEntry(me);
in.getManifest().write(out);
out.closeEntry();
}
byte[] buffer = new byte[1 << 14];
for (JarEntry je; (je = in.getNextJarEntry()) != null; ) {
out.putNextEntry(je);
for (int nr; 0 < (nr = in.read(buffer)); ) {
out.write(buffer, 0, nr);
}
}
in.close();
markJarFile(out); // add PACK200 comment
}
/**
* Takes a JarInputStream and converts into a pack-stream.
* <p>
* Closes its input but not its output. (Pack200 archives are appendable.)
* <p>
* The modification time and deflation hint attributes are not available,
* for the jar-manifest file and the directory containing the file.
*
* @see #MODIFICATION_TIME
* @see #DEFLATION_HINT
* @param in a JarInputStream
* @param out an OutputStream
* @exception IOException if an error is encountered.
*/
public synchronized void pack(JarInputStream in, OutputStream out) throws IOException {
assert (Utils.currentInstance.get() == null);
boolean needUTC = !props.getBoolean(Utils.PACK_DEFAULT_TIMEZONE);
try {
Utils.currentInstance.set(this);
if (needUTC) {
Utils.changeDefaultTimeZoneToUtc();
}
if ("0".equals(props.getProperty(Pack200.Packer.EFFORT))) {
Utils.copyJarFile(in, out);
} else {
(new DoPack()).run(in, out);
}
} finally {
Utils.currentInstance.set(null);
if (needUTC) {
Utils.restoreDefaultTimeZone();
}
in.close();
}
}
static void copyJarFile(JarInputStream in, JarOutputStream out) throws IOException {
if (in.getManifest() != null) {
ZipEntry me = new ZipEntry(JarFile.MANIFEST_NAME);
out.putNextEntry(me);
in.getManifest().write(out);
out.closeEntry();
}
byte[] buffer = new byte[1 << 14];
for (JarEntry je; (je = in.getNextJarEntry()) != null; ) {
out.putNextEntry(je);
for (int nr; 0 < (nr = in.read(buffer)); ) {
out.write(buffer, 0, nr);
}
}
in.close();
markJarFile(out); // add PACK200 comment
}
public JarEntry getJarEntry(String name) {
String filename = name.substring(name.indexOf("!/") + 2);
if (filename.equals(MANIFEST_NAME)) {
// Synthesize a JarEntry.
return new JarEntry(filename) {
};
}
try {
JarInputStream is = new JarInputStream(super.getInputStream(wrappedJarFile));
try {
JarEntry entry;
while ((entry = is.getNextJarEntry()) != null) {
if (entry.getName().equals(filename)) {
return entry;
}
}
} finally {
is.close();
}
} catch (IOException e) {
throw new IllegalStateException("Undefined Error", e);
}
return null;
// throw new RuntimeException("Entry not found : " + name);
}
/**
* Takes a JarInputStream and converts into a pack-stream.
* <p>
* Closes its input but not its output. (Pack200 archives are appendable.)
* <p>
* The modification time and deflation hint attributes are not available,
* for the jar-manifest file and the directory containing the file.
*
* @see #MODIFICATION_TIME
* @see #DEFLATION_HINT
* @param in a JarInputStream
* @param out an OutputStream
* @exception IOException if an error is encountered.
*/
public synchronized void pack(JarInputStream in, OutputStream out) throws IOException {
assert(Utils.currentInstance.get() == null);
TimeZone tz = (props.getBoolean(Utils.PACK_DEFAULT_TIMEZONE)) ? null :
TimeZone.getDefault();
try {
Utils.currentInstance.set(this);
if (tz != null) TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
if ("0".equals(props.getProperty(Pack200.Packer.EFFORT))) {
Utils.copyJarFile(in, out);
} else {
(new DoPack()).run(in, out);
}
} finally {
Utils.currentInstance.set(null);
if (tz != null) TimeZone.setDefault(tz);
in.close();
}
}
private boolean unzipJarAlt(String pathJar, String pathDir) {
try {
FileInputStream fileStream = new FileInputStream(File.separator + pathJar);
JarInputStream jarStream = new JarInputStream(fileStream);
JarEntry entry = jarStream.getNextJarEntry();
while (entry != null) {
File fileEntry = new File(File.separator + pathDir + File.separator + entry.getName());
if (entry.isDirectory()) {
fileEntry.mkdirs();
}
else {
FileOutputStream os = new FileOutputStream(fileEntry);
while (jarStream.available() > 0) {
os.write(jarStream.read());
}
os.close();
}
entry = jarStream.getNextJarEntry();
}
jarStream.close();
return true;
}
catch (IOException e) {
e.printStackTrace();
}
return false;
}
/**
* Fetches the game jar and loads and patches the classes
*
* @param jarURL The URL of the jar to be loaded and patched
* @return If no exceptions occurred
*/
public boolean fetch(String jarURL) {
Logger.Info("Fetching Jar: " + jarURL);
try {
JarInputStream in = new JarInputStream(Settings.getResourceAsStream(jarURL));
Launcher.getInstance().setProgress(1, 1);
JarEntry entry;
while ((entry = in.getNextJarEntry()) != null) {
// Check if file is needed
String name = entry.getName();
// Read class to byte array
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
byte[] data = new byte[1024];
int readSize;
while ((readSize = in.read(data, 0, data.length)) != -1) bOut.write(data, 0, readSize);
byte[] classData = bOut.toByteArray();
bOut.close();
Logger.Info("Loading file: " + name);
Launcher.getInstance().setStatus("Loading " + name + "...");
if (name.endsWith(".class")) {
name = name.substring(0, name.indexOf(".class"));
classData = JClassPatcher.getInstance().patch(classData);
m_classData.put(name, classData);
}
}
in.close();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
@Test
public void transformSimpleTestBpmn() throws Exception {
File file = new File("result.jar");
file.deleteOnExit();
FileOutputStream fos = new FileOutputStream(file);
File bpmn = new File("src/test/resources/testprocess.bpmn");
transformer.transform(bpmn.toURI().toURL(), fos);
JarInputStream jis = new JarInputStream(new FileInputStream(file));
Attributes attributes = jis.getManifest().getMainAttributes();
assertThat(attributes.getValue("Manifest-Version"), is("2"));
assertThat(attributes.getValue(Constants.BUNDLE_VERSION), is("0.0.0"));
assertThat(attributes.getValue(Constants.BUNDLE_SYMBOLICNAME),
is("testprocess"));
assertThat(attributes.getValue(Constants.BUNDLE_MANIFESTVERSION),
is("2"));
assertThat(attributes.getValue(BUNDLE_PROCESS_DEFINITIONS_HEADER),
is(BUNDLE_PROCESS_DEFINTIONS_DEFAULT));
ZipEntry entry;
while ((entry = jis.getNextEntry()) != null) {
assertThat(
entry.getName(),
is(anyOf(is("OSGI-INF/"),
is(BUNDLE_PROCESS_DEFINTIONS_DEFAULT),
is(BUNDLE_PROCESS_DEFINTIONS_DEFAULT
+ "testprocess.bpmn"))));
}
jis.close();
}
/**
* Returns true if the specified classpath contains a class of the given name,
* false otherwise.
*
* @param classpath consists of jar files and folders containing classes
* @param className the name of the class
*
* @return true if the specified classpath contains a class of the given name,
* false otherwise.
*
* @throws IOException if an I/O error has occurred
*
* @since 1.15
*/
public static boolean containsClass(Collection<File> classpath, String className) throws IOException {
Parameters.notNull("classpath", classpath); // NOI18N
Parameters.notNull("driverClassName", className); // NOI18N
String classFilePath = className.replace('.', '/') + ".class"; // NOI18N
for (File file : classpath) {
if (file.isFile()) {
JarInputStream is = new JarInputStream(new BufferedInputStream(
new FileInputStream(file)), false);
try {
JarEntry entry;
while ((entry = is.getNextJarEntry()) != null) {
if (classFilePath.equals(entry.getName())) {
return true;
}
}
} finally {
is.close();
}
} else {
if (new File(file, classFilePath).exists()) {
return true;
}
}
}
return false;
}
private void replaceManifest(InputStream is, OutputStream os, Manifest manifest) throws IOException {
JarInputStream in = new JarInputStream(is);
try {
JarOutputStream out = new JarOutputStream(os, manifest);
try {
JarEntry entry = null;
byte[] temp = new byte[32768];
while ((entry = in.getNextJarEntry()) != null) {
String name = entry.getName();
if (name.equalsIgnoreCase("META-INF/MANIFEST.MF")) { // NOI18N
continue;
}
out.putNextEntry(entry);
while (in.available() != 0) {
int read = in.read(temp);
if (read != -1) {
out.write(temp, 0, read);
}
}
out.closeEntry();
}
} finally {
out.close();
}
} finally {
in.close();
}
}
private void loadSubJars(byte[] byteArray) {
try {
JarInputStream jarInputStream = new JarInputStream(new ByteArrayInputStream(byteArray));
JarEntry entry = jarInputStream.getNextJarEntry();
while (entry != null) {
addDataToMap(jarInputStream, entry);
entry = jarInputStream.getNextJarEntry();
}
jarInputStream.close();
} catch (IOException e) {
LOGGER.error("", e);
}
}
@Override
public Map<String, byte[]> getClassData() {
// For this one, we'll need the note in the database
if (classData == null) {
classData = new TreeMap<String, byte[]>();
try {
byte[] jarData = getFileDataRaw("%%object%%.jar");
InputStream objInputStream = new ByteArrayInputStream(jarData);
JarInputStream jis = new JarInputStream(objInputStream);
JarEntry entry = jis.getNextJarEntry();
while (entry != null) {
String name = entry.getName();
if (name.endsWith(".class")) { //TODO our classloader should support resources also!
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while (jis.available() > 0) {
bos.write(jis.read());
}
classData.put(DominoUtils.filePathToJavaBinaryName(name, "/"), bos.toByteArray());
}
entry = jis.getNextJarEntry();
}
jis.close();
objInputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return classData;
}
/**
* Load the class data when the installed jar is accessible
* only as an input stream (the jar is itself in a database jar).
*/
private Class loadClassData(
InputStream in, String className, String jvmClassName, boolean resolve)
throws IOException {
if (GemFireXDUtils.TraceApplicationJars) {
SanityManager.DEBUG_PRINT(GfxdConstants.TRACE_APP_JARS,
"JarLoader#loadClassData Attempting to load class " + className + " jvmClassName "
+ jvmClassName + " name " + Arrays.toString(name));
}
JarInputStream jarIn = new JarInputStream(in);
for (;;) {
JarEntry e = jarIn.getNextJarEntry();
if (e == null) {
jarIn.close();
return null;
}
if (e.getName().equals(jvmClassName)) {
if (GemFireXDUtils.TraceApplicationJars) {
SanityManager.DEBUG_PRINT(GfxdConstants.TRACE_APP_JARS,
"JarLoader#loadClassData found jar entry " + e.getName()
+ " attempting to load classData ");
}
Class c = loadClassData(e, jarIn, className, resolve);
jarIn.close();
return c;
}
}
}
private void genericTestResolve(String jarName, String conf, ModuleRevisionId[] expectedMrids,
ModuleRevisionId[] expected2Mrids) throws Exception {
JarInputStream jis = new JarInputStream(
new FileInputStream("test/test-repo/bundlerepo/" + jarName));
Manifest manifest = jis.getManifest();
jis.close();
BundleInfo bundleInfo = ManifestParser.parseManifest(manifest);
bundleInfo.addArtifact(new BundleArtifact(false, new File("test/test-repo/bundlerepo/"
+ jarName).toURI(), null));
DefaultModuleDescriptor md = BundleInfoAdapter.toModuleDescriptor(
OSGiManifestParser.getInstance(), null, bundleInfo, profileProvider);
ResolveReport resolveReport = ivy.resolve(md,
new ResolveOptions().setConfs(new String[] {conf}).setOutputReport(false));
assertFalse("resolve failed " + resolveReport.getAllProblemMessages(),
resolveReport.hasError());
Set<ModuleRevisionId> actual = new HashSet<>();
for (Artifact artifact : resolveReport.getArtifacts()) {
actual.add(artifact.getModuleRevisionId());
}
Set<ModuleRevisionId> expected = new HashSet<>(Arrays.asList(expectedMrids));
if (expected2Mrids != null) {
// in this use case, we have two choices, let's try the second one
try {
Set<ModuleRevisionId> expected2 = new HashSet<>(
Arrays.asList(expected2Mrids));
assertEquals(expected2, actual);
return; // test passed
} catch (AssertionError e) {
// too bad, let's continue
}
}
assertEquals(expected, actual);
}