1
22
23 package com.liferay.util.servlet;
24
25 import com.liferay.util.servlet.fileupload.LiferayFileUpload;
26
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.io.OutputStream;
30
31 import javax.portlet.ActionRequest;
32 import javax.portlet.PortletSession;
33
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36
37
43 public class ProgressInputStream extends InputStream {
44
45 public ProgressInputStream(ActionRequest req, InputStream is,
46 long totalSize, String progressId)
47 throws IOException {
48
49 _ses = req.getPortletSession();
50 _is = is;
51 _totalSize = totalSize;
52 _progressId = progressId;
53
54 initProgress();
55 }
56
57 public int available() throws IOException {
58 return _is.available();
59 }
60
61 public void clearProgress() {
62 _ses.removeAttribute(_getPercentAttributeName());
63 }
64
65 public void close() throws IOException {
66 _is.close();
67 }
68
69 public long getTotalRead() {
70 return _totalRead;
71 }
72
73 public void initProgress() {
74 _ses.setAttribute(
75 _getPercentAttributeName(), new Integer(0),
76 PortletSession.APPLICATION_SCOPE);
77 }
78
79 public void mark(int readlimit) {
80 _is.mark(readlimit);
81 }
82
83 public boolean markSupported() {
84 return _is.markSupported();
85 }
86
87 public int read() throws IOException {
88 return _is.read();
89 }
90
91 public int read(byte[] b) throws IOException {
92 return read(b, 0, b.length);
93 }
94
95 public int read(byte[] b, int off, int len) throws IOException {
96 int bytesRead = super.read(b, off, len);
97
98 _updateProgress(bytesRead);
99
100 return bytesRead;
101 }
102
103 public void readAll(OutputStream os) throws IOException {
104 byte[] buffer = new byte[_DEFAULT_INITIAL_BUFFER_SIZE];
105
106 int len = 0;
107
108 while ((len = read(buffer)) > 0) {
109 os.write(buffer, 0, len);
110 }
111
112 os.close();
113 }
114
115 public void reset() throws IOException {
116 _is.reset();
117 }
118
119 public long skip(long n) throws IOException {
120 long result = _is.skip(n);
121
122 _updateProgress(result);
123
124 return result;
125 }
126
127 private String _getPercentAttributeName() {
128 return LiferayFileUpload.PERCENT + _progressId;
129 }
130
131 private void _updateProgress(long bytesRead) {
132 if (bytesRead > 0) {
133 _totalRead += bytesRead;
134 }
135 else {
136 _totalRead = _totalSize;
137 }
138
139 int percent = (int) ((_totalRead * 100) / _totalSize);
140
141 if (_log.isDebugEnabled()) {
142 _log.debug(bytesRead + "/" + _totalRead + "=" + percent);
143 }
144
145 Integer curPercent = (Integer)_ses.getAttribute(
146 _getPercentAttributeName(), PortletSession.APPLICATION_SCOPE);
147
148 if ((curPercent == null) || (percent - curPercent.intValue() >= 1)) {
149 _ses.setAttribute(
150 _getPercentAttributeName(), new Integer(percent),
151 PortletSession.APPLICATION_SCOPE);
152 }
153 }
154
155 private static final int _DEFAULT_INITIAL_BUFFER_SIZE = 4 * 1024;
156
157 private static Log _log = LogFactory.getLog(ProgressInputStream.class);
158
159 private PortletSession _ses;
160 private InputStream _is;
161 private long _totalRead;
162 private long _totalSize;
163 private String _progressId;
164
165 }