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  import com.liferay.portal.kernel.util.ByteArrayMaker;
25  import com.liferay.portal.kernel.util.GetterUtil;
26  import com.liferay.portal.util.PropsUtil;
27  import com.liferay.util.servlet.ByteArrayInputStreamWrapper;
28  import com.liferay.util.servlet.ServletInputStreamWrapper;
29  
30  import java.io.ByteArrayInputStream;
31  import java.io.IOException;
32  
33  import javax.servlet.ServletInputStream;
34  import javax.servlet.http.HttpServletRequest;
35  import javax.servlet.http.HttpSession;
36  
37  /**
38   * <a href="LiferayInputStream.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Myunghun Kim
41   * @author Brian Wing Shun Chan
42   * @author Harry Mark
43   *
44   */
45  public class LiferayInputStream extends ServletInputStreamWrapper {
46  
47      public static final int THRESHOLD_SIZE = GetterUtil.getInteger(
48          PropsUtil.get(LiferayInputStream.class.getName() + ".threshold.size"));
49  
50      public LiferayInputStream(HttpServletRequest request) throws IOException {
51          super(request.getInputStream());
52  
53          _session = request.getSession();
54          _totalSize = request.getContentLength();
55      }
56  
57      public int read(byte[] b, int off, int len) throws IOException {
58          int bytesRead = super.read(b, off, len);
59  
60          if (bytesRead > 0) {
61              _totalRead += bytesRead;
62          }
63          else {
64              return bytesRead;
65          }
66  
67          int percent = (_totalRead * 100) / _totalSize;
68  
69          if (_log.isDebugEnabled()) {
70              _log.debug(bytesRead + "/" + _totalRead + "=" + percent);
71          }
72  
73          if (_totalSize < THRESHOLD_SIZE) {
74              _cachedBytes.write(b, off, bytesRead);
75          }
76  
77          Integer curPercent = (Integer)_session.getAttribute(
78              LiferayFileUpload.PERCENT);
79  
80          if ((curPercent == null) || (percent - curPercent.intValue() >= 1)) {
81              _session.setAttribute(
82                  LiferayFileUpload.PERCENT, new Integer(percent));
83          }
84  
85          return bytesRead;
86      }
87  
88      public ServletInputStream getCachedInputStream() {
89          if (_totalSize < THRESHOLD_SIZE) {
90              return this;
91          }
92          else {
93              return new ByteArrayInputStreamWrapper(
94                  new ByteArrayInputStream(_cachedBytes.toByteArray()));
95          }
96      }
97  
98      private static Log _log = LogFactoryUtil.getLog(LiferayInputStream.class);
99  
100     private HttpSession _session;
101     private int _totalRead;
102     private int _totalSize;
103     private ByteArrayMaker _cachedBytes = new ByteArrayMaker();
104 
105 }