001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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 ServletInputStream getCachedInputStream() {
050                    if (_totalSize < THRESHOLD_SIZE) {
051                            return this;
052                    }
053                    else {
054                            return new UnsyncByteArrayInputStreamWrapper(
055                                    new UnsyncByteArrayInputStream(
056                                            _cachedBytes.unsafeGetByteArray(), 0, _cachedBytes.size()));
057                    }
058            }
059    
060            @Override
061            public int read(byte[] b, int off, int len) throws IOException {
062                    int bytesRead = super.read(b, off, len);
063    
064                    if (bytesRead > 0) {
065                            _totalRead += bytesRead;
066                    }
067                    else {
068                            return bytesRead;
069                    }
070    
071                    int percent = (int)((_totalRead * 100L) / _totalSize);
072    
073                    if (_log.isDebugEnabled()) {
074                            _log.debug(bytesRead + "/" + _totalRead + "=" + percent);
075                    }
076    
077                    if ((_totalSize > 0) && (_totalSize < THRESHOLD_SIZE)) {
078                            _cachedBytes.write(b, off, bytesRead);
079                    }
080    
081                    Integer curPercent = (Integer)_session.getAttribute(
082                            LiferayFileUpload.PERCENT);
083    
084                    if ((curPercent == null) || (percent - curPercent.intValue() >= 1)) {
085                            _session.setAttribute(
086                                    LiferayFileUpload.PERCENT, new Integer(percent));
087                    }
088    
089                    return bytesRead;
090            }
091    
092            private static Log _log = LogFactoryUtil.getLog(LiferayInputStream.class);
093    
094            private UnsyncByteArrayOutputStream _cachedBytes =
095                    new UnsyncByteArrayOutputStream();
096            private HttpSession _session;
097            private int _totalRead;
098            private int _totalSize;
099    
100    }