View Javadoc

    1   /*
    2    * $HeadURL: https://svn.apache.org/repos/asf/jakarta/httpcomponents/oac.hc3x/tags/HTTPCLIENT_3_1/src/java/org/apache/commons/httpclient/methods/FileRequestEntity.java $
    3    * $Revision: 486665 $
    4    * $Date: 2006-12-13 15:19:07 +0100 (Wed, 13 Dec 2006) $
    5    *
    6    * ====================================================================
    7    *
    8    *  Licensed to the Apache Software Foundation (ASF) under one or more
    9    *  contributor license agreements.  See the NOTICE file distributed with
    10   *  this work for additional information regarding copyright ownership.
    11   *  The ASF licenses this file to You under the Apache License, Version 2.0
    12   *  (the "License"); you may not use this file except in compliance with
    13   *  the License.  You may obtain a copy of the License at
    14   *
    15   *      http://www.apache.org/licenses/LICENSE-2.0
    16   *
    17   *  Unless required by applicable law or agreed to in writing, software
    18   *  distributed under the License is distributed on an "AS IS" BASIS,
    19   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    20   *  See the License for the specific language governing permissions and
    21   *  limitations under the License.
    22   * ====================================================================
    23   *
    24   * This software consists of voluntary contributions made by many
    25   * individuals on behalf of the Apache Software Foundation.  For more
    26   * information on the Apache Software Foundation, please see
    27   * <http://www.apache.org/>.
    28   *
    29   */
    30  package org.apache.commons.httpclient.methods;
    31
    32  import java.io.File;
    33  import java.io.FileInputStream;
    34  import java.io.IOException;
    35  import java.io.InputStream;
    36  import java.io.OutputStream;
    37
    38  import org.apache.commons.httpclient.methods.RequestEntity;
    39
    40  /**
    41   * A RequestEntity that represents a File.
    42   *
    43   * @since 3.1
    44   */
    45  public class FileRequestEntity implements RequestEntity {
    46
    47      final File file;
    48      final String contentType;
    49
    50      public FileRequestEntity(final File file, final String contentType) {
    51          super();
    52          if (file == null) {
    53              throw new IllegalArgumentException("File may not be null");
    54          }
    55          this.file = file;
    56          this.contentType = contentType;
    57      }
    58      public long getContentLength() {
    59          return this.file.length();
    60      }
    61
    62      public String getContentType() {
    63          return this.contentType;
    64      }
    65
    66      public boolean isRepeatable() {
    67          return true;
    68      }
    69
    70      public void writeRequest(final OutputStream out) throws IOException {
    71          byte[] tmp = new byte[4096];
    72          int i = 0;
    73          InputStream instream = new FileInputStream(this.file);
    74          try {
    75              while ((i = instream.read(tmp)) >= 0) {
    76                  out.write(tmp, 0, i);
    77              }
    78          } finally {
    79              instream.close();
    80          }
    81      }
    82
    83  }