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