001    /**
002     * Copyright (c) 2000-2013 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.servlet.HttpHeaders;
023    import com.liferay.portal.kernel.util.GetterUtil;
024    import com.liferay.portal.util.PropsUtil;
025    import com.liferay.util.servlet.ServletInputStreamWrapper;
026    
027    import java.io.IOException;
028    
029    import javax.servlet.ServletInputStream;
030    import javax.servlet.http.HttpServletRequest;
031    import javax.servlet.http.HttpSession;
032    
033    /**
034     * @author Brian Myunghun Kim
035     * @author Brian Wing Shun Chan
036     * @author Harry Mark
037     */
038    public class LiferayInputStream extends ServletInputStreamWrapper {
039    
040            public static final long THRESHOLD_SIZE = GetterUtil.getLong(
041                    PropsUtil.get(LiferayInputStream.class.getName() + ".threshold.size"));
042    
043            public LiferayInputStream(HttpServletRequest request) throws IOException {
044                    super(request.getInputStream());
045    
046                    _session = request.getSession();
047                    _totalSize = request.getContentLength();
048    
049                    if (_totalSize < 0) {
050                            _totalSize = GetterUtil.getLong(
051                                    request.getHeader(HttpHeaders.CONTENT_LENGTH), _totalSize);
052                    }
053            }
054    
055            public ServletInputStream getCachedInputStream() {
056                    if (_totalSize < THRESHOLD_SIZE) {
057                            return this;
058                    }
059                    else {
060                            return new UnsyncByteArrayInputStreamWrapper(
061                                    new UnsyncByteArrayInputStream(
062                                            _cachedBytes.unsafeGetByteArray(), 0, _cachedBytes.size()));
063                    }
064            }
065    
066            @Override
067            public int read(byte[] b, int off, int len) throws IOException {
068                    int bytesRead = super.read(b, off, len);
069    
070                    if (bytesRead > 0) {
071                            _totalRead += bytesRead;
072                    }
073                    else {
074                            return bytesRead;
075                    }
076    
077                    int percent = (int)((_totalRead * 100L) / _totalSize);
078    
079                    if (_log.isDebugEnabled()) {
080                            _log.debug(bytesRead + "/" + _totalRead + "=" + percent);
081                    }
082    
083                    if ((_totalSize > 0) && (_totalSize < THRESHOLD_SIZE)) {
084                            _cachedBytes.write(b, off, bytesRead);
085                    }
086    
087                    Integer curPercent = (Integer)_session.getAttribute(
088                            LiferayFileUpload.PERCENT);
089    
090                    if ((curPercent == null) || ((percent - curPercent.intValue()) >= 1)) {
091                            _session.setAttribute(
092                                    LiferayFileUpload.PERCENT, new Integer(percent));
093                    }
094    
095                    return bytesRead;
096            }
097    
098            private static Log _log = LogFactoryUtil.getLog(LiferayInputStream.class);
099    
100            private UnsyncByteArrayOutputStream _cachedBytes =
101                    new UnsyncByteArrayOutputStream();
102            private HttpSession _session;
103            private long _totalRead;
104            private long _totalSize;
105    
106    }