01: /*
02: * $HeadURL: https://svn.apache.org/repos/asf/jakarta/httpcomponents/oac.hc3x/tags/HTTPCLIENT_3_1/src/java/org/apache/commons/httpclient/methods/FileRequestEntity.java $
03: * $Revision: 486665 $
04: * $Date: 2006-12-13 15:19:07 +0100 (Wed, 13 Dec 2006) $
05: *
06: * ====================================================================
07: *
08: * Licensed to the Apache Software Foundation (ASF) under one or more
09: * 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:
59: public long getContentLength() {
60: return this .file.length();
61: }
62:
63: public String getContentType() {
64: return this .contentType;
65: }
66:
67: public boolean isRepeatable() {
68: return true;
69: }
70:
71: public void writeRequest(final OutputStream out) throws IOException {
72: byte[] tmp = new byte[4096];
73: int i = 0;
74: InputStream instream = new FileInputStream(this .file);
75: try {
76: while ((i = instream.read(tmp)) >= 0) {
77: out.write(tmp, 0, i);
78: }
79: } finally {
80: instream.close();
81: }
82: }
83:
84: }
|