1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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   */
41  public class ProgressInputStream extends InputStream {
42  
43      public ProgressInputStream(
44          ActionRequest actionRequest, InputStream is, long totalSize,
45          String progressId) {
46  
47          _portletSession = actionRequest.getPortletSession();
48          _is = is;
49          _totalSize = totalSize;
50          _progressId = progressId;
51  
52          initProgress();
53      }
54  
55      public int available() throws IOException {
56          return _is.available();
57      }
58  
59      public void clearProgress() {
60          _portletSession.removeAttribute(_getPercentAttributeName());
61      }
62  
63      public void close() throws IOException {
64          _is.close();
65      }
66  
67      public long getTotalRead() {
68          return _totalRead;
69      }
70  
71      public void initProgress() {
72          _portletSession.setAttribute(
73              _getPercentAttributeName(), new Integer(0),
74              PortletSession.APPLICATION_SCOPE);
75      }
76  
77      public void mark(int readlimit) {
78          _is.mark(readlimit);
79      }
80  
81      public boolean markSupported() {
82          return _is.markSupported();
83      }
84  
85      public int read() throws IOException {
86          return _is.read();
87      }
88  
89      public int read(byte[] b) throws IOException {
90          return read(b, 0, b.length);
91      }
92  
93      public int read(byte[] b, int off, int len) throws IOException {
94          int bytesRead = super.read(b, off, len);
95  
96          _updateProgress(bytesRead);
97  
98          return bytesRead;
99      }
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     public void reset() throws IOException {
114         _is.reset();
115     }
116 
117     public long skip(long n) throws IOException {
118         long result = _is.skip(n);
119 
120         _updateProgress(result);
121 
122         return result;
123     }
124 
125     private String _getPercentAttributeName() {
126         return LiferayFileUpload.PERCENT + _progressId;
127     }
128 
129     private void _updateProgress(long bytesRead) {
130         if (bytesRead > 0) {
131             _totalRead += bytesRead;
132         }
133         else {
134             _totalRead = _totalSize;
135         }
136 
137         int percent = (int) ((_totalRead * 100) / _totalSize);
138 
139         if (_log.isDebugEnabled()) {
140             _log.debug(bytesRead + "/" + _totalRead + "=" + percent);
141         }
142 
143         Integer curPercent = (Integer)_portletSession.getAttribute(
144             _getPercentAttributeName(), PortletSession.APPLICATION_SCOPE);
145 
146         if ((curPercent == null) || (percent - curPercent.intValue() >= 1)) {
147             _portletSession.setAttribute(
148                 _getPercentAttributeName(), new Integer(percent),
149                 PortletSession.APPLICATION_SCOPE);
150         }
151     }
152 
153     private static final int _DEFAULT_INITIAL_BUFFER_SIZE = 4 * 1024;
154 
155     private static Log _log = LogFactoryUtil.getLog(ProgressInputStream.class);
156 
157     private PortletSession _portletSession;
158     private InputStream _is;
159     private long _totalRead;
160     private long _totalSize;
161     private String _progressId;
162 
163 }