001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.commons.net.http.httpclient4.entity;
020
021import java.io.IOException;
022import java.io.InputStream;
023import java.io.OutputStream;
024
025import lucee.commons.io.IOUtil;
026import lucee.commons.io.TemporaryStream;
027
028import org.apache.http.entity.AbstractHttpEntity;
029
030
031public class TemporaryStreamHttpEntity extends AbstractHttpEntity implements Entity4 {
032
033        private final TemporaryStream ts;
034        private String ct;
035
036        public TemporaryStreamHttpEntity(TemporaryStream ts,String contentType) {
037                this.ts=ts;
038                setContentType(contentType);
039                this.ct=contentType;
040        }
041        
042        @Override
043        public long getContentLength() {
044                return ts.length();
045        }
046
047        @Override
048        public boolean isRepeatable() {
049                return false;
050        }
051
052        @Override
053        public void writeTo(OutputStream os) throws IOException {
054                IOUtil.copy(ts.getInputStream(), os,true,false);
055        }
056
057        @Override
058        public InputStream getContent() throws IOException, IllegalStateException {
059                return ts.getInputStream();
060        }
061
062        @Override
063        public boolean isStreaming() {
064                return false;
065        }
066
067        @Override
068        public long contentLength() {
069                return getContentLength();
070        }
071
072        @Override
073        public String contentType() {
074                return ct;
075        }
076}