001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/main/java/org/apache/http/entity/FileEntity.java $
003: * $Revision: 604625 $
004: * $Date: 2007-12-16 15:11:11 +0100 (Sun, 16 Dec 2007) $
005: *
006: * ====================================================================
007: * Licensed to the Apache Software Foundation (ASF) under one
008: * or more contributor license agreements. See the NOTICE file
009: * distributed with this work for additional information
010: * regarding copyright ownership. The ASF licenses this file
011: * to you under the Apache License, Version 2.0 (the
012: * "License"); you may not use this file except in compliance
013: * with the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing,
018: * software distributed under the License is distributed on an
019: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020: * KIND, either express or implied. See the License for the
021: * specific language governing permissions and limitations
022: * under the License.
023: * ====================================================================
024: *
025: * This software consists of voluntary contributions made by many
026: * individuals on behalf of the Apache Software Foundation. For more
027: * information on the Apache Software Foundation, please see
028: * <http://www.apache.org/>.
029: *
030: */
031:
032: package org.apache.http.entity;
033:
034: import java.io.File;
035: import java.io.FileInputStream;
036: import java.io.IOException;
037: import java.io.InputStream;
038: import java.io.OutputStream;
039:
040: /**
041: * An entity whose content is retrieved from a file.
042: *
043: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
044: *
045: * @version $Revision: 604625 $
046: *
047: * @since 4.0
048: */
049: public class FileEntity extends AbstractHttpEntity implements Cloneable {
050:
051: protected final File file;
052:
053: public FileEntity(final File file, final String contentType) {
054: super ();
055: if (file == null) {
056: throw new IllegalArgumentException("File may not be null");
057: }
058: this .file = file;
059: setContentType(contentType);
060: }
061:
062: public boolean isRepeatable() {
063: return true;
064: }
065:
066: public long getContentLength() {
067: return this .file.length();
068: }
069:
070: public InputStream getContent() throws IOException {
071: return new FileInputStream(this .file);
072: }
073:
074: public void writeTo(final OutputStream outstream)
075: throws IOException {
076: if (outstream == null) {
077: throw new IllegalArgumentException(
078: "Output stream may not be null");
079: }
080: InputStream instream = new FileInputStream(this .file);
081: try {
082: byte[] tmp = new byte[4096];
083: int l;
084: while ((l = instream.read(tmp)) != -1) {
085: outstream.write(tmp, 0, l);
086: }
087: outstream.flush();
088: } finally {
089: instream.close();
090: }
091: }
092:
093: /**
094: * Tells that this entity is not streaming.
095: *
096: * @return <code>false</code>
097: */
098: public boolean isStreaming() {
099: return false;
100: }
101:
102: public Object clone() throws CloneNotSupportedException {
103: // File instance is considered immutable
104: // No need to make a copy of it
105: return super .clone();
106: }
107:
108: } // class FileEntity
|