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.res.Resource;
011    
012    /**
013     * A RequestEntity that represents a Resource.
014     */
015    public class ResourceHttpEntity extends AbstractHttpEntity implements Entity4 {
016    
017        final Resource res;
018            private String strContentType;
019        
020        public ResourceHttpEntity(final Resource res, final String contentType) {
021            super();
022            this.res = res;
023            setContentType(contentType);
024            strContentType = contentType;
025        }
026       
027        @Override
028        public long getContentLength() {
029            return this.res.length();
030        }
031    
032        @Override
033        public boolean isRepeatable() {
034            return true;
035        }
036        
037        @Override
038        public InputStream getContent() throws IOException {
039            return res.getInputStream();
040        }
041    
042        @Override
043        public void writeTo(final OutputStream out) throws IOException {
044           IOUtil.copy(res.getInputStream(), out,true,false);
045        }
046    
047            @Override
048            public boolean isStreaming() {
049                    return false;
050            }
051    
052            @Override
053            public long contentLength() {
054                    return getContentLength();
055            }
056    
057            @Override
058            public String contentType() {
059                    return strContentType;
060            }
061    }