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