1
22
23 package com.liferay.util.servlet.fileupload;
24
25 import com.liferay.portal.kernel.util.ByteArrayMaker;
26 import com.liferay.util.servlet.ByteArrayInputStreamWrapper;
27 import com.liferay.util.servlet.ServletInputStreamWrapper;
28
29 import java.io.ByteArrayInputStream;
30 import java.io.IOException;
31
32 import javax.servlet.ServletInputStream;
33 import javax.servlet.http.HttpServletRequest;
34 import javax.servlet.http.HttpSession;
35
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38
39
47 public class LiferayInputStream extends ServletInputStreamWrapper {
48
49 public LiferayInputStream(HttpServletRequest req) throws IOException {
50 super(req.getInputStream());
51
52 _ses = req.getSession();
53 _totalSize = req.getContentLength();
54 }
55
56 public int read(byte[] b, int off, int len) throws IOException {
57 int bytesRead = super.read(b, off, len);
58
59 if (bytesRead > 0) {
60 _totalRead += bytesRead;
61 }
62 else {
63 _totalRead = _totalSize;
64 }
65
66 int percent = (_totalRead * 100) / _totalSize;
67
68 if (_log.isDebugEnabled()) {
69 _log.debug(bytesRead + "/" + _totalRead + "=" + percent);
70 }
71
72 _cachedBytes.write(b, off, bytesRead);
73
74 Integer curPercent = (Integer)_ses.getAttribute(
75 LiferayFileUpload.PERCENT);
76
77 if ((curPercent == null) || (percent - curPercent.intValue() >= 1)) {
78 _ses.setAttribute(LiferayFileUpload.PERCENT, new Integer(percent));
79 }
80
81 return bytesRead;
82 }
83
84 public ServletInputStream getCachedInputStream() throws IOException {
85 return new ByteArrayInputStreamWrapper(
86 new ByteArrayInputStream(_cachedBytes.toByteArray()));
87 }
88
89 private static Log _log = LogFactory.getLog(LiferayInputStream.class);
90
91 private HttpSession _ses;
92 private int _totalRead;
93 private int _totalSize;
94 private ByteArrayMaker _cachedBytes = new ByteArrayMaker();
95
96 }