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;
022:
023: import com.liferay.util.servlet.fileupload.LiferayFileUpload;
024:
025: import java.io.IOException;
026: import java.io.InputStream;
027: import java.io.OutputStream;
028:
029: import javax.portlet.ActionRequest;
030: import javax.portlet.PortletSession;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034:
035: /**
036: * <a href="ProgressInputStream.java.html"><b><i>View Source</i></b></a>
037: *
038: * @author Jorge Ferrer
039: *
040: */
041: public class ProgressInputStream extends InputStream {
042:
043: public ProgressInputStream(ActionRequest req, InputStream is,
044: long totalSize, String progressId) throws IOException {
045:
046: _ses = req.getPortletSession();
047: _is = is;
048: _totalSize = totalSize;
049: _progressId = progressId;
050:
051: initProgress();
052: }
053:
054: public int available() throws IOException {
055: return _is.available();
056: }
057:
058: public void clearProgress() {
059: _ses.removeAttribute(_getPercentAttributeName());
060: }
061:
062: public void close() throws IOException {
063: _is.close();
064: }
065:
066: public long getTotalRead() {
067: return _totalRead;
068: }
069:
070: public void initProgress() {
071: _ses.setAttribute(_getPercentAttributeName(), new Integer(0),
072: PortletSession.APPLICATION_SCOPE);
073: }
074:
075: public void mark(int readlimit) {
076: _is.mark(readlimit);
077: }
078:
079: public boolean markSupported() {
080: return _is.markSupported();
081: }
082:
083: public int read() throws IOException {
084: return _is.read();
085: }
086:
087: public int read(byte[] b) throws IOException {
088: return read(b, 0, b.length);
089: }
090:
091: public int read(byte[] b, int off, int len) throws IOException {
092: int bytesRead = super .read(b, off, len);
093:
094: _updateProgress(bytesRead);
095:
096: return bytesRead;
097: }
098:
099: public void readAll(OutputStream os) throws IOException {
100: byte[] buffer = new byte[_DEFAULT_INITIAL_BUFFER_SIZE];
101:
102: int len = 0;
103:
104: while ((len = read(buffer)) > 0) {
105: os.write(buffer, 0, len);
106: }
107:
108: os.close();
109: }
110:
111: public void reset() throws IOException {
112: _is.reset();
113: }
114:
115: public long skip(long n) throws IOException {
116: long result = _is.skip(n);
117:
118: _updateProgress(result);
119:
120: return result;
121: }
122:
123: private String _getPercentAttributeName() {
124: return LiferayFileUpload.PERCENT + _progressId;
125: }
126:
127: private void _updateProgress(long bytesRead) {
128: if (bytesRead > 0) {
129: _totalRead += bytesRead;
130: } else {
131: _totalRead = _totalSize;
132: }
133:
134: int percent = (int) ((_totalRead * 100) / _totalSize);
135:
136: if (_log.isDebugEnabled()) {
137: _log.debug(bytesRead + "/" + _totalRead + "=" + percent);
138: }
139:
140: Integer curPercent = (Integer) _ses.getAttribute(
141: _getPercentAttributeName(),
142: PortletSession.APPLICATION_SCOPE);
143:
144: if ((curPercent == null)
145: || (percent - curPercent.intValue() >= 1)) {
146: _ses.setAttribute(_getPercentAttributeName(), new Integer(
147: percent), PortletSession.APPLICATION_SCOPE);
148: }
149: }
150:
151: private static final int _DEFAULT_INITIAL_BUFFER_SIZE = 4 * 1024;
152:
153: private static Log _log = LogFactory
154: .getLog(ProgressInputStream.class);
155:
156: private PortletSession _ses;
157: private InputStream _is;
158: private long _totalRead;
159: private long _totalSize;
160: private String _progressId;
161:
162: }
|