001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.util.servlet.fileupload;
022:
023: import com.liferay.portal.kernel.util.ByteArrayMaker;
024: import com.liferay.portal.kernel.util.GetterUtil;
025: import com.liferay.util.SystemProperties;
026: import com.liferay.util.servlet.ByteArrayInputStreamWrapper;
027: import com.liferay.util.servlet.ServletInputStreamWrapper;
028:
029: import java.io.ByteArrayInputStream;
030: import java.io.IOException;
031:
032: import javax.servlet.ServletInputStream;
033: import javax.servlet.http.HttpServletRequest;
034: import javax.servlet.http.HttpSession;
035:
036: import org.apache.commons.logging.Log;
037: import org.apache.commons.logging.LogFactory;
038:
039: /**
040: * <a href="LiferayInputStream.java.html"><b><i>View Source</i></b></a>
041: *
042: * @author Brian Myunghun Kim
043: * @author Brian Wing Shun Chan
044: * @author Harry Mark
045: *
046: */
047: public class LiferayInputStream extends ServletInputStreamWrapper {
048:
049: public static final int THRESHOLD_SIZE = GetterUtil
050: .getInteger(SystemProperties.get(LiferayInputStream.class
051: .getName()
052: + ".threshold.size"));
053:
054: public LiferayInputStream(HttpServletRequest req)
055: throws IOException {
056: super (req.getInputStream());
057:
058: _ses = req.getSession();
059: _totalSize = req.getContentLength();
060: }
061:
062: public int read(byte[] b, int off, int len) throws IOException {
063: int bytesRead = super .read(b, off, len);
064:
065: if (bytesRead > 0) {
066: _totalRead += bytesRead;
067: } else {
068: return bytesRead;
069: }
070:
071: int percent = (_totalRead * 100) / _totalSize;
072:
073: if (_log.isDebugEnabled()) {
074: _log.debug(bytesRead + "/" + _totalRead + "=" + percent);
075: }
076:
077: if (_totalSize < THRESHOLD_SIZE) {
078: _cachedBytes.write(b, off, bytesRead);
079: }
080:
081: Integer curPercent = (Integer) _ses
082: .getAttribute(LiferayFileUpload.PERCENT);
083:
084: if ((curPercent == null)
085: || (percent - curPercent.intValue() >= 1)) {
086: _ses.setAttribute(LiferayFileUpload.PERCENT, new Integer(
087: percent));
088: }
089:
090: return bytesRead;
091: }
092:
093: public ServletInputStream getCachedInputStream() throws IOException {
094: if (_totalSize < THRESHOLD_SIZE) {
095: return this ;
096: } else {
097: return new ByteArrayInputStreamWrapper(
098: new ByteArrayInputStream(_cachedBytes.toByteArray()));
099: }
100: }
101:
102: private static Log _log = LogFactory
103: .getLog(LiferayInputStream.class);
104:
105: private HttpSession _ses;
106: private int _totalRead;
107: private int _totalSize;
108: private ByteArrayMaker _cachedBytes = new ByteArrayMaker();
109:
110: }
|