001
014
015 package com.liferay.portal.upload;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019
020 import java.io.IOException;
021 import java.io.InputStream;
022 import java.io.OutputStream;
023
024 import javax.portlet.ActionRequest;
025 import javax.portlet.PortletSession;
026
027
030 public class ProgressInputStream extends InputStream {
031
032 public ProgressInputStream(
033 ActionRequest actionRequest, InputStream is, long totalSize,
034 String progressId) {
035
036 _portletSession = actionRequest.getPortletSession();
037 _is = is;
038 _totalSize = totalSize;
039 _progressId = progressId;
040
041 initProgress();
042 }
043
044 public int available() throws IOException {
045 return _is.available();
046 }
047
048 public void clearProgress() {
049 _portletSession.removeAttribute(_getPercentAttributeName());
050 }
051
052 public void close() throws IOException {
053 _is.close();
054 }
055
056 public long getTotalRead() {
057 return _totalRead;
058 }
059
060 public void initProgress() {
061 _portletSession.setAttribute(
062 _getPercentAttributeName(), new Integer(0),
063 PortletSession.APPLICATION_SCOPE);
064 }
065
066 public void mark(int readlimit) {
067 _is.mark(readlimit);
068 }
069
070 public boolean markSupported() {
071 return _is.markSupported();
072 }
073
074 public int read() throws IOException {
075 return _is.read();
076 }
077
078 public int read(byte[] b) throws IOException {
079 return read(b, 0, b.length);
080 }
081
082 public int read(byte[] b, int off, int len) throws IOException {
083 int bytesRead = super.read(b, off, len);
084
085 _updateProgress(bytesRead);
086
087 return bytesRead;
088 }
089
090 public void readAll(OutputStream os) throws IOException {
091 byte[] buffer = new byte[_DEFAULT_INITIAL_BUFFER_SIZE];
092
093 int len = 0;
094
095 while ((len = read(buffer)) > 0) {
096 os.write(buffer, 0, len);
097 }
098
099 os.close();
100 }
101
102 public void reset() throws IOException {
103 _is.reset();
104 }
105
106 public long skip(long n) throws IOException {
107 long result = _is.skip(n);
108
109 _updateProgress(result);
110
111 return result;
112 }
113
114 private String _getPercentAttributeName() {
115 return LiferayFileUpload.PERCENT + _progressId;
116 }
117
118 private void _updateProgress(long bytesRead) {
119 if (bytesRead > 0) {
120 _totalRead += bytesRead;
121 }
122 else {
123 _totalRead = _totalSize;
124 }
125
126 int percent = (int) ((_totalRead * 100) / _totalSize);
127
128 if (_log.isDebugEnabled()) {
129 _log.debug(bytesRead + "/" + _totalRead + "=" + percent);
130 }
131
132 Integer curPercent = (Integer)_portletSession.getAttribute(
133 _getPercentAttributeName(), PortletSession.APPLICATION_SCOPE);
134
135 if ((curPercent == null) || (percent - curPercent.intValue() >= 1)) {
136 _portletSession.setAttribute(
137 _getPercentAttributeName(), new Integer(percent),
138 PortletSession.APPLICATION_SCOPE);
139 }
140 }
141
142 private static final int _DEFAULT_INITIAL_BUFFER_SIZE = 4 * 1024;
143
144 private static Log _log = LogFactoryUtil.getLog(ProgressInputStream.class);
145
146 private PortletSession _portletSession;
147 private InputStream _is;
148 private long _totalRead;
149 private long _totalSize;
150 private String _progressId;
151
152 }