You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2005/12/15 20:06:17 UTC

svn commit: r357058 - /jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/entity/FileEntity.java

Author: olegk
Date: Thu Dec 15 11:06:10 2005
New Revision: 357058

URL: http://svn.apache.org/viewcvs?rev=357058&view=rev
Log:
Added an HTTP entity class representing a File

Added:
    jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/entity/FileEntity.java   (with props)

Added: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/entity/FileEntity.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/entity/FileEntity.java?rev=357058&view=auto
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/entity/FileEntity.java (added)
+++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/entity/FileEntity.java Thu Dec 15 11:06:10 2005
@@ -0,0 +1,115 @@
+/*
+ * $HeadURL: $
+ * $Revision: $
+ * $Date: $
+ *
+ * ====================================================================
+ *
+ *  Copyright 1999-2004 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.entity;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import org.apache.http.HttpEntity;
+
+/**
+ * <p>
+ * </p>
+ * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
+ *
+ * @version $Revision: $
+ * 
+ * @since 4.0
+ */
+public class FileEntity implements HttpEntity {
+
+    private final File file; 
+    private String contentType = null;
+    private String contentEncoding = null;
+    private boolean chunked = false;
+
+    public FileEntity(final File file, final String contentType) {
+        super();
+        if (file == null) {
+            throw new IllegalArgumentException("File may not be null");
+        }
+        this.file = file;
+        this.contentType = contentType;
+    }
+
+    public boolean isRepeatable() {
+        return true;
+    }
+
+    public boolean isChunked() {
+        return this.chunked;
+    }
+
+    public void setChunked(boolean b) {
+        this.chunked = b;
+    }
+
+    public long getContentLength() {
+        return this.file.length();
+    }
+    
+    public String getContentType() {
+        return this.contentType;
+    }
+
+    public void setContentType(final String contentType) {
+        this.contentType = contentType;
+    }
+
+    public String getContentEncoding() {
+        return this.contentEncoding;
+    }
+
+    public void setContentEncoding(final String contentEncoding) {
+        this.contentEncoding = contentEncoding;
+    }
+    
+    public InputStream getContent() throws IOException {
+        return new FileInputStream(this.file);
+    }
+    
+    public boolean writeTo(final OutputStream outstream) throws IOException {
+        if (outstream == null) {
+            throw new IllegalArgumentException("Output stream may not be null");
+        }
+        InputStream instream = new FileInputStream(this.file);
+        byte[] tmp = new byte[4096];
+        int l;
+        while ((l = instream.read(tmp)) != -1) {
+            outstream.write(tmp, 0, l);
+        }
+        outstream.flush();
+        return true;
+    }
+
+}

Propchange: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/entity/FileEntity.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/entity/FileEntity.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/entity/FileEntity.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain