Author: bombe
Date: 2007-10-29 10:33:28 +0000 (Mon, 29 Oct 2007)
New Revision: 15638
Modified:
trunk/freenet/src/freenet/support/JarClassLoader.java
trunk/freenet/src/freenet/support/io/FileUtil.java
Log:
move StreamCopier methods to FileUtil
adapt jar class loader to use FileUtil
Modified: trunk/freenet/src/freenet/support/JarClassLoader.java
===================================================================
--- trunk/freenet/src/freenet/support/JarClassLoader.java 2007-10-29
10:31:14 UTC (rev 15637)
+++ trunk/freenet/src/freenet/support/JarClassLoader.java 2007-10-29
10:33:28 UTC (rev 15638)
@@ -28,7 +28,7 @@
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
-import freenet.support.io.StreamCopier;
+import freenet.support.io.FileUtil;
/**
* Class loader that loads classes from a JAR file. The JAR file gets copied
@@ -100,7 +100,7 @@
private void copyFileToTemp(InputStream inputStream, long length)
throws IOException {
File tempFile = File.createTempFile("jar-", ".tmp");
FileOutputStream fileOutputStream = new
FileOutputStream(tempFile);
- StreamCopier.copy(inputStream, fileOutputStream, length);
+ FileUtil.copy(inputStream, fileOutputStream, length);
fileOutputStream.close();
tempFile.deleteOnExit();
tempJarFile = new JarFile(tempFile);
@@ -122,7 +122,7 @@
long size = jarEntry.getSize();
InputStream jarEntryInputStream =
tempJarFile.getInputStream(jarEntry);
ByteArrayOutputStream classBytesOutputStream =
new ByteArrayOutputStream((int) size);
- StreamCopier.copy(jarEntryInputStream,
classBytesOutputStream, size);
+ FileUtil.copy(jarEntryInputStream,
classBytesOutputStream, size);
classBytesOutputStream.close();
jarEntryInputStream.close();
byte[] classBytes =
classBytesOutputStream.toByteArray();
Modified: trunk/freenet/src/freenet/support/io/FileUtil.java
===================================================================
--- trunk/freenet/src/freenet/support/io/FileUtil.java 2007-10-29 10:31:14 UTC
(rev 15637)
+++ trunk/freenet/src/freenet/support/io/FileUtil.java 2007-10-29 10:33:28 UTC
(rev 15638)
@@ -6,6 +6,7 @@
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
+import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
@@ -13,11 +14,14 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
+import java.io.OutputStream;
import freenet.client.DefaultMIMETypes;
import freenet.support.Logger;
final public class FileUtil {
+
+ private static final int BUFFER_SIZE = 4096;
/** Round up a value to the next multiple of a power of 2 */
private static final long roundup_2n (long val, int blocksize) {
@@ -165,5 +169,62 @@
if(defaultExt == null) return filename;
else return filename + '.' + defaultExt;
}
-
+
+ /**
+ * Find the length of an input stream. This method will consume the
complete
+ * input stream until its {@link InputStream#read(byte[])} method
returns
+ * <code>-1</code>, thus signalling the end of the stream.
+ *
+ * @param source
+ * The input stream to find the length of
+ * @return The numbe of bytes that can be read from the stream
+ * @throws IOException
+ * if an I/O error occurs
+ */
+ public static long findLength(InputStream source) throws IOException {
+ long length = 0;
+ byte[] buffer = new byte[BUFFER_SIZE];
+ int read = 0;
+ while (read > -1) {
+ read = source.read(buffer);
+ if (read != -1) {
+ length += read;
+ }
+ }
+ return length;
+ }
+
+ /**
+ * Copies <code>length</code> bytes from the source input stream to the
+ * destination output stream. If <code>length</code> is <code>-1</code>
+ * as much bytes as possible will be copied (i.e. until
+ * {@link InputStream#read()} returns <code>-1</code> to signal the end
of
+ * the stream).
+ *
+ * @param source
+ * The input stream to read from
+ * @param destination
+ * The output stream to write to
+ * @param length
+ * The number of bytes to copy
+ * @throws IOException
+ * if an I/O error occurs
+ */
+ public static void copy(InputStream source, OutputStream destination,
long length) throws IOException {
+ long remaining = length;
+ byte[] buffer = new byte[BUFFER_SIZE];
+ int read = 0;
+ while ((remaining == -1) || (remaining > 0)) {
+ read = source.read(buffer, 0, ((remaining >
BUFFER_SIZE) || (remaining == -1)) ? BUFFER_SIZE : (int) remaining);
+ if (read == -1) {
+ if (length == -1) {
+ return;
+ }
+ throw new EOFException("stream reached eof");
+ }
+ destination.write(buffer, 0, read);
+ remaining -= read;
+ }
+ }
+
}