FELIX-2485 Improve the performance of the manipulator on large files
Now we use a buffered copy instead of byte-based copy. The difference is notable moslty on huge file, like native libraries.
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@979566 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/ipojo/manipulator/src/main/java/org/apache/felix/ipojo/manipulator/Pojoization.java b/ipojo/manipulator/src/main/java/org/apache/felix/ipojo/manipulator/Pojoization.java
index 3763477..325cc8b 100644
--- a/ipojo/manipulator/src/main/java/org/apache/felix/ipojo/manipulator/Pojoization.java
+++ b/ipojo/manipulator/src/main/java/org/apache/felix/ipojo/manipulator/Pojoization.java
@@ -326,6 +326,19 @@
}
}
}
+
+ /**
+ * Copies an input stream into an output stream but does not close the streams.
+ * @param in the input stream
+ * @param out the output stream
+ * @throws IOException if the stream cannot be copied
+ */
+ private static void copyStreamWithoutClosing(InputStream in, OutputStream out) throws IOException {
+ byte[] b = new byte[4096];
+ for (int n; (n = in.read(b)) != -1;) {
+ out.write(b, 0, n);
+ }
+ }
/**
* Manipulate the input bundle.
@@ -368,12 +381,8 @@
} else { // The class is already manipulated
jos.putNextEntry(curEntry);
InputStream currIn = m_inputJar.getInputStream(curEntry);
- int c;
- int i = 0;
- while ((c = currIn.read()) >= 0) {
- jos.write(c);
- i++;
- }
+ copyStreamWithoutClosing(currIn, jos);
+
currIn.close();
jos.closeEntry();
}
@@ -383,12 +392,7 @@
// copy the entry header to jos
jos.putNextEntry(curEntry);
InputStream currIn = m_inputJar.getInputStream(curEntry);
- int c;
- int i = 0;
- while ((c = currIn.read()) >= 0) {
- jos.write(c);
- i++;
- }
+ copyStreamWithoutClosing(currIn, jos);
currIn.close();
jos.closeEntry();
}