Share February 2009: Project of the Month

ZK - Simply Ajax and Mobile

Tracker: Bugs

5 Horrible IO-Performance with RepeatableInputStream - ID: 2872183
Last Update: Settings changed ( tomyeh )

Due to missing BufferedOutputStreams, IO-Performance is quite horrible when
using org.zkoss.io.RepeatableInputStream.

For a simple test use the following script which loads a local file
(referenced by absolute path) to a Media-Object and copies this object 10
times to different files. Although the test is a little bit "artifical",
I'm having real issues with ZKs IO-Performance developing a CMS for
uploading/downloading/copying files.

Running a test with a 30MB-Zip-File takes 400sec to procede. Applying the
patch below which introduces BufferedOutputStreams to
org.zkoss.io.RepeatableInputStream the test only takes 10 sec.

<zk>

<textbox id="filenameField" />
<button onClick="copy();" label="Copy" />

<zscript><![CDATA[
public void copy() {
String filename = filenameField.getText();
java.io.File file = new java.io.File(filename);
org.zkoss.util.media.Media media = new org.zkoss.util.media.AMedia(
null, null, "application/unknown", file, true);

long start = System.currentTimeMillis();

for (int i = 0; i < 10; i++) {
java.io.InputStream input = new java.io.BufferedInputStream(media
.getStreamData());
java.io.OutputStream output = new java.io.BufferedOutputStream(
new java.io.FileOutputStream(new java.io.File(filename
+ ".copy" + i)));
byte[] buffer = new byte[4096];
for(int n=0; -1 != (n = input.read(buffer));) {
output.write(buffer, 0, n);
}
output.close();
input.close();
}

alert(System.currentTimeMillis() - start);
}
]]>
</zscript>

</zk>


Index: zcommon/src/org/zkoss/io/RepeatableInputStream.java
===================================================================
--- zcommon/src/org/zkoss/io/RepeatableInputStream.java (revision 12635)
+++ zcommon/src/org/zkoss/io/RepeatableInputStream.java (working copy)
@@ -18,15 +18,17 @@
*/

package org.zkoss.io;



+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.FileInputStream;

+import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

-import java.io.FileNotFoundException;

import java.net.URL;



import org.zkoss.lang.Library;

@@ -185,7 +187,7 @@
f.mkdir();

_f = File.createTempFile("zk.io", ".zk.io", f);

final byte[] bs = ((ByteArrayOutputStream)_out).toByteArray();

- _out = new FileOutputStream(_f);

+ _out = new BufferedOutputStream(new FileOutputStream(_f));
_out.write(bs);

} catch (Throwable ex) {

log.warning("Ingored: failed to buffer to a file, "+_f+"\nCause:
"+ex.getMessage());

@@ -224,7 +226,7 @@
return b;

} else {

if (_in == null)

- _in = new FileInputStream(_f); //_f must be non-null

+ _in = new BufferedInputStream(new FileInputStream(_f)); //_f must be
non-null


return _in.read();

}

@@ -309,8 +311,9 @@
}



public int read() throws IOException {

- if (_in == null)

- _in = new FileInputStream(_file);

+ if (_in == null) {
+ _in = new BufferedInputStream(new FileInputStream(_file));
+ }
return _in.read();

}

/** Closes the current access, and the next call of {@link #read}

@@ -340,7 +343,7 @@


public int read() throws IOException {

if (_in == null) {

- _in = _url.openStream();

+ _in = new BufferedInputStream(_url.openStream());
if (_in == null) throw new
FileNotFoundException(_url.toExternalForm());

}

return _in.read();


Maik Jablonski ( mjablonski ) - 2009-10-03 10:58

5

Closed

None

Nobody/Anonymous

General

3.6.2

Public


Comments ( 2 )

Date: 2009-10-06 02:41
Sender: tomyehProject Admin

Fixed since 10/6


Maik, thanks for your contribution.


Date: 2009-10-03 16:31
Sender: mjablonski

The same performance-problem can be easily seen when using
org.zkoss.zul.Filedownload to download files via ZK. The proposed patch
fixes this also.


Attached File

No Files Currently Attached

Changes ( 5 )

Field Old Value Date By
status_id Open 2009-10-06 02:41 tomyeh
priority 9 2009-10-06 02:41 tomyeh
allow_comments 1 2009-10-06 02:41 tomyeh
close_date - 2009-10-06 02:41 tomyeh
priority 5 2009-10-03 10:59 mjablonski