1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.upload;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  
28  import java.io.IOException;
29  import java.io.InputStream;
30  import java.io.OutputStream;
31  
32  import javax.portlet.ActionRequest;
33  import javax.portlet.PortletSession;
34  
35  /**
36   * <a href="ProgressInputStream.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Jorge Ferrer
39   */
40  public class ProgressInputStream extends InputStream {
41  
42      public ProgressInputStream(
43          ActionRequest actionRequest, InputStream is, long totalSize,
44          String progressId) {
45  
46          _portletSession = actionRequest.getPortletSession();
47          _is = is;
48          _totalSize = totalSize;
49          _progressId = progressId;
50  
51          initProgress();
52      }
53  
54      public int available() throws IOException {
55          return _is.available();
56      }
57  
58      public void clearProgress() {
59          _portletSession.removeAttribute(_getPercentAttributeName());
60      }
61  
62      public void close() throws IOException {
63          _is.close();
64      }
65  
66      public long getTotalRead() {
67          return _totalRead;
68      }
69  
70      public void initProgress() {
71          _portletSession.setAttribute(
72              _getPercentAttributeName(), new Integer(0),
73              PortletSession.APPLICATION_SCOPE);
74      }
75  
76      public void mark(int readlimit) {
77          _is.mark(readlimit);
78      }
79  
80      public boolean markSupported() {
81          return _is.markSupported();
82      }
83  
84      public int read() throws IOException {
85          return _is.read();
86      }
87  
88      public int read(byte[] b) throws IOException {
89          return read(b, 0, b.length);
90      }
91  
92      public int read(byte[] b, int off, int len) throws IOException {
93          int bytesRead = super.read(b, off, len);
94  
95          _updateProgress(bytesRead);
96  
97          return bytesRead;
98      }
99  
100     public void readAll(OutputStream os) throws IOException {
101         byte[] buffer = new byte[_DEFAULT_INITIAL_BUFFER_SIZE];
102 
103         int len = 0;
104 
105         while ((len = read(buffer)) > 0) {
106             os.write(buffer, 0, len);
107         }
108 
109         os.close();
110     }
111 
112     public void reset() throws IOException {
113         _is.reset();
114     }
115 
116     public long skip(long n) throws IOException {
117         long result = _is.skip(n);
118 
119         _updateProgress(result);
120 
121         return result;
122     }
123 
124     private String _getPercentAttributeName() {
125         return LiferayFileUpload.PERCENT + _progressId;
126     }
127 
128     private void _updateProgress(long bytesRead) {
129         if (bytesRead > 0) {
130             _totalRead += bytesRead;
131         }
132         else {
133             _totalRead = _totalSize;
134         }
135 
136         int percent = (int) ((_totalRead * 100) / _totalSize);
137 
138         if (_log.isDebugEnabled()) {
139             _log.debug(bytesRead + "/" + _totalRead + "=" + percent);
140         }
141 
142         Integer curPercent = (Integer)_portletSession.getAttribute(
143             _getPercentAttributeName(), PortletSession.APPLICATION_SCOPE);
144 
145         if ((curPercent == null) || (percent - curPercent.intValue() >= 1)) {
146             _portletSession.setAttribute(
147                 _getPercentAttributeName(), new Integer(percent),
148                 PortletSession.APPLICATION_SCOPE);
149         }
150     }
151 
152     private static final int _DEFAULT_INITIAL_BUFFER_SIZE = 4 * 1024;
153 
154     private static Log _log = LogFactoryUtil.getLog(ProgressInputStream.class);
155 
156     private PortletSession _portletSession;
157     private InputStream _is;
158     private long _totalRead;
159     private long _totalSize;
160     private String _progressId;
161 
162 }