001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    import com.liferay.portal.kernel.util.ProgressTracker;
020    
021    import java.io.IOException;
022    import java.io.InputStream;
023    import java.io.OutputStream;
024    
025    import javax.portlet.ActionRequest;
026    import javax.portlet.PortletSession;
027    
028    /**
029     * @author Jorge Ferrer
030     * @author Brian Wing Shun Chan
031     * @author Minhchau Dang
032     */
033    public class ProgressInputStream extends InputStream {
034    
035            public ProgressInputStream(
036                    ActionRequest actionRequest, InputStream is, long totalSize,
037                    String progressId) {
038    
039                    _portletSession = actionRequest.getPortletSession();
040                    _is = is;
041                    _totalSize = totalSize;
042                    _progressId = progressId;
043    
044                    initProgress();
045            }
046    
047            @Override
048            public int available() throws IOException {
049                    return _is.available();
050            }
051    
052            public void clearProgress() {
053                    _portletSession.removeAttribute(_getPercentAttributeName());
054            }
055    
056            @Override
057            public void close() throws IOException {
058                    _is.close();
059            }
060    
061            public long getTotalRead() {
062                    return _totalRead;
063            }
064    
065            public void initProgress() {
066                    ProgressTracker progressTracker = new ProgressTracker(
067                            _portletSession, _progressId);
068    
069                    progressTracker.initialize();
070            }
071    
072            @Override
073            public void mark(int readlimit) {
074                    _is.mark(readlimit);
075            }
076    
077            @Override
078            public boolean markSupported() {
079                    return _is.markSupported();
080            }
081    
082            @Override
083            public int read() throws IOException {
084                    return _is.read();
085            }
086    
087            @Override
088            public int read(byte[] b) throws IOException {
089                    return read(b, 0, b.length);
090            }
091    
092            @Override
093            public int read(byte[] b, int off, int len) throws IOException {
094                    int bytesRead = super.read(b, off, len);
095    
096                    _updateProgress(bytesRead);
097    
098                    return bytesRead;
099            }
100    
101            public void readAll(OutputStream os) throws IOException {
102                    byte[] buffer = new byte[_DEFAULT_INITIAL_BUFFER_SIZE];
103    
104                    int len = 0;
105    
106                    while ((len = read(buffer)) > 0) {
107                            os.write(buffer, 0, len);
108                    }
109    
110                    os.close();
111            }
112    
113            @Override
114            public void reset() throws IOException {
115                    _is.reset();
116            }
117    
118            @Override
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 if (_totalSize > 0) {
136                            _totalRead = _totalSize;
137                    }
138    
139                    int percent = 0;
140    
141                    if (_totalSize > 0) {
142                            percent = (int)((_totalRead * 100) / _totalSize);
143                    }
144    
145                    if (_log.isDebugEnabled()) {
146                            _log.debug(_totalRead + "/" + _totalSize + "=" + percent);
147                    }
148    
149                    ProgressTracker progressTracker =
150                            (ProgressTracker)_portletSession.getAttribute(
151                                    _getPercentAttributeName(), PortletSession.APPLICATION_SCOPE);
152    
153                    Integer curPercent = null;
154    
155                    if (progressTracker != null) {
156                            curPercent = progressTracker.getPercent();
157                    }
158    
159                    if ((curPercent == null) || ((percent - curPercent.intValue()) >= 1)) {
160                            if (progressTracker == null) {
161                                    progressTracker = new ProgressTracker(
162                                            _portletSession, _progressId);
163    
164                                    progressTracker.initialize();
165                            }
166    
167                            progressTracker.setPercent(percent);
168                    }
169            }
170    
171            private static final int _DEFAULT_INITIAL_BUFFER_SIZE = 4 * 1024;
172    
173            private static Log _log = LogFactoryUtil.getLog(ProgressInputStream.class);
174    
175            private InputStream _is;
176            private PortletSession _portletSession;
177            private String _progressId;
178            private long _totalRead;
179            private long _totalSize;
180    
181    }