1   /**
2    * Copyright (c) 2000-2008 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.util.servlet;
24  
25  import com.liferay.util.servlet.fileupload.LiferayFileUpload;
26  
27  import java.io.IOException;
28  import java.io.InputStream;
29  import java.io.OutputStream;
30  
31  import javax.portlet.ActionRequest;
32  import javax.portlet.PortletSession;
33  
34  import org.apache.commons.logging.Log;
35  import org.apache.commons.logging.LogFactory;
36  
37  /**
38   * <a href="ProgressInputStream.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Jorge Ferrer
41   *
42   */
43  public class ProgressInputStream extends InputStream {
44  
45      public ProgressInputStream(ActionRequest req, InputStream is,
46                                 long totalSize, String progressId)
47          throws IOException {
48  
49          _ses = req.getPortletSession();
50          _is = is;
51          _totalSize = totalSize;
52          _progressId = progressId;
53  
54          initProgress();
55      }
56  
57      public int available() throws IOException {
58          return _is.available();
59      }
60  
61      public void clearProgress() {
62          _ses.removeAttribute(_getPercentAttributeName());
63      }
64  
65      public void close() throws IOException {
66          _is.close();
67      }
68  
69      public long getTotalRead() {
70          return _totalRead;
71      }
72  
73      public void initProgress() {
74          _ses.setAttribute(
75              _getPercentAttributeName(), new Integer(0),
76              PortletSession.APPLICATION_SCOPE);
77      }
78  
79      public void mark(int readlimit) {
80          _is.mark(readlimit);
81      }
82  
83      public boolean markSupported() {
84          return _is.markSupported();
85      }
86  
87      public int read() throws IOException {
88          return _is.read();
89      }
90  
91      public int read(byte[] b) throws IOException {
92          return read(b, 0, b.length);
93      }
94  
95      public int read(byte[] b, int off, int len) throws IOException {
96          int bytesRead = super.read(b, off, len);
97  
98          _updateProgress(bytesRead);
99  
100         return bytesRead;
101     }
102 
103     public void readAll(OutputStream os) throws IOException {
104         byte[] buffer = new byte[_DEFAULT_INITIAL_BUFFER_SIZE];
105 
106         int len = 0;
107 
108         while ((len = read(buffer)) > 0) {
109             os.write(buffer, 0, len);
110         }
111 
112         os.close();
113     }
114 
115     public void reset() throws IOException {
116         _is.reset();
117     }
118 
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 {
136             _totalRead = _totalSize;
137         }
138 
139         int percent = (int) ((_totalRead * 100) / _totalSize);
140 
141         if (_log.isDebugEnabled()) {
142             _log.debug(bytesRead + "/" + _totalRead + "=" + percent);
143         }
144 
145         Integer curPercent = (Integer)_ses.getAttribute(
146             _getPercentAttributeName(), PortletSession.APPLICATION_SCOPE);
147 
148         if ((curPercent == null) || (percent - curPercent.intValue() >= 1)) {
149             _ses.setAttribute(
150                 _getPercentAttributeName(), new Integer(percent),
151                 PortletSession.APPLICATION_SCOPE);
152         }
153     }
154 
155     private static final int _DEFAULT_INITIAL_BUFFER_SIZE = 4 * 1024;
156 
157     private static Log _log = LogFactory.getLog(ProgressInputStream.class);
158 
159     private PortletSession _ses;
160     private InputStream _is;
161     private long _totalRead;
162     private long _totalSize;
163     private String _progressId;
164 
165 }