View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.entity;
29  
30  import java.io.File;
31  import java.io.FileInputStream;
32  import java.io.IOException;
33  import java.io.InputStream;
34  import java.io.OutputStream;
35  
36  import org.apache.http.util.Args;
37  
38  /**
39   * A self contained, repeatable entity that obtains its content from a file.
40   *
41   * @since 4.0
42   */
43  public class FileEntity extends AbstractHttpEntity implements Cloneable {
44  
45      protected final File file;
46  
47      /**
48       * Creates a new instance.
49       *
50       * @param file The file to serve.
51       * @param contentType  The content type for the given {@code file}.
52       *
53       * @deprecated (4.1.3) {@link #FileEntity(File, ContentType)}
54       */
55      @Deprecated
56      public FileEntity(final File file, final String contentType) {
57          super();
58          this.file = Args.notNull(file, "File");
59          setContentType(contentType);
60      }
61  
62      /**
63       * Creates a new instance.
64       *
65       * @param file The file to serve.
66       * @param contentType  The content type for the given {@code file}.
67       *
68       * @since 4.2
69       */
70      public FileEntity(final File file, final ContentType contentType) {
71          super();
72          this.file = Args.notNull(file, "File");
73          if (contentType != null) {
74              setContentType(contentType.toString());
75          }
76      }
77  
78      /**
79       * Creates a new instance.
80       *
81       * @param file The file to serve.
82       *
83       * @since 4.2
84       */
85      public FileEntity(final File file) {
86          super();
87          this.file = Args.notNull(file, "File");
88      }
89  
90      @Override
91      public boolean isRepeatable() {
92          return true;
93      }
94  
95      @Override
96      public long getContentLength() {
97          return this.file.length();
98      }
99  
100     @Override
101     public InputStream getContent() throws IOException {
102         return new FileInputStream(this.file);
103     }
104 
105     @Override
106     public void writeTo(final OutputStream outStream) throws IOException {
107         Args.notNull(outStream, "Output stream");
108         final InputStream inStream = new FileInputStream(this.file);
109         try {
110             final byte[] tmp = new byte[OUTPUT_BUFFER_SIZE];
111             int l;
112             while ((l = inStream.read(tmp)) != -1) {
113                 outStream.write(tmp, 0, l);
114             }
115             outStream.flush();
116         } finally {
117             inStream.close();
118         }
119     }
120 
121     /**
122      * Tells that this entity is not streaming.
123      *
124      * @return {@code false}
125      */
126     @Override
127     public boolean isStreaming() {
128         return false;
129     }
130 
131     @Override
132     public Object clone() throws CloneNotSupportedException {
133         // File instance is considered immutable
134         // No need to make a copy of it
135         return super.clone();
136     }
137 
138 } // class FileEntity