001    package railo.commons.net.http.httpclient4.entity;
002    
003    import java.io.IOException;
004    import java.io.InputStream;
005    import java.io.OutputStream;
006    
007    import org.apache.http.entity.AbstractHttpEntity;
008    
009    import railo.commons.io.IOUtil;
010    import railo.commons.io.TemporaryStream;
011    
012    
013    public class TemporaryStreamHttpEntity extends AbstractHttpEntity implements Entity4 {
014    
015            private final TemporaryStream ts;
016            private String ct;
017    
018            public TemporaryStreamHttpEntity(TemporaryStream ts,String contentType) {
019                    this.ts=ts;
020                    setContentType(contentType);
021                    this.ct=contentType;
022            }
023            
024            @Override
025            public long getContentLength() {
026                    return ts.length();
027            }
028    
029            @Override
030            public boolean isRepeatable() {
031                    return false;
032            }
033    
034            @Override
035            public void writeTo(OutputStream os) throws IOException {
036                    IOUtil.copy(ts.getInputStream(), os,true,false);
037            }
038    
039            @Override
040            public InputStream getContent() throws IOException, IllegalStateException {
041                    return ts.getInputStream();
042            }
043    
044            @Override
045            public boolean isStreaming() {
046                    return false;
047            }
048    
049            @Override
050            public long contentLength() {
051                    return getContentLength();
052            }
053    
054            @Override
055            public String contentType() {
056                    return ct;
057            }
058    }