001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.upload;
016    
017    import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
018    import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStreamWrapper;
019    import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.util.GetterUtil;
023    import com.liferay.portal.util.PropsUtil;
024    import com.liferay.util.servlet.ServletInputStreamWrapper;
025    
026    import java.io.IOException;
027    
028    import javax.servlet.ServletInputStream;
029    import javax.servlet.http.HttpServletRequest;
030    import javax.servlet.http.HttpSession;
031    
032    /**
033     * @author Brian Myunghun Kim
034     * @author Brian Wing Shun Chan
035     * @author Harry Mark
036     */
037    public class LiferayInputStream extends ServletInputStreamWrapper {
038    
039            public static final int THRESHOLD_SIZE = GetterUtil.getInteger(
040                    PropsUtil.get(LiferayInputStream.class.getName() + ".threshold.size"));
041    
042            public LiferayInputStream(HttpServletRequest request) throws IOException {
043                    super(request.getInputStream());
044    
045                    _session = request.getSession();
046                    _totalSize = request.getContentLength();
047            }
048    
049            public int read(byte[] b, int off, int len) throws IOException {
050                    int bytesRead = super.read(b, off, len);
051    
052                    if (bytesRead > 0) {
053                            _totalRead += bytesRead;
054                    }
055                    else {
056                            return bytesRead;
057                    }
058    
059                    int percent = (_totalRead * 100) / _totalSize;
060    
061                    if (_log.isDebugEnabled()) {
062                            _log.debug(bytesRead + "/" + _totalRead + "=" + percent);
063                    }
064    
065                    if (_totalSize < THRESHOLD_SIZE) {
066                            _cachedBytes.write(b, off, bytesRead);
067                    }
068    
069                    Integer curPercent = (Integer)_session.getAttribute(
070                            LiferayFileUpload.PERCENT);
071    
072                    if ((curPercent == null) || (percent - curPercent.intValue() >= 1)) {
073                            _session.setAttribute(
074                                    LiferayFileUpload.PERCENT, new Integer(percent));
075                    }
076    
077                    return bytesRead;
078            }
079    
080            public ServletInputStream getCachedInputStream() {
081                    if (_totalSize < THRESHOLD_SIZE) {
082                            return this;
083                    }
084                    else {
085                            return new UnsyncByteArrayInputStreamWrapper(
086                                    new UnsyncByteArrayInputStream(
087                                            _cachedBytes.unsafeGetByteArray(), 0, _cachedBytes.size()));
088                    }
089            }
090    
091            private static Log _log = LogFactoryUtil.getLog(LiferayInputStream.class);
092    
093            private HttpSession _session;
094            private int _totalRead;
095            private int _totalSize;
096            private UnsyncByteArrayOutputStream _cachedBytes =
097                    new UnsyncByteArrayOutputStream();
098    
099    }