001/*
002 * ====================================================================
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *   http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied.  See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 * ====================================================================
020 *
021 * This software consists of voluntary contributions made by many
022 * individuals on behalf of the Apache Software Foundation.  For more
023 * information on the Apache Software Foundation, please see
024 * <http://www.apache.org/>.
025 *
026 */
027
028package org.apache.http.entity;
029
030import java.io.File;
031import java.io.FileInputStream;
032import java.io.IOException;
033import java.io.InputStream;
034import java.io.OutputStream;
035
036import org.apache.http.util.Args;
037
038/**
039 * A self contained, repeatable entity that obtains its content from a file.
040 *
041 * @since 4.0
042 */
043public class FileEntity extends AbstractHttpEntity implements Cloneable {
044
045    protected final File file;
046
047    /**
048     * @deprecated (4.1.3) {@link #FileEntity(File, ContentType)}
049     */
050    @Deprecated
051    public FileEntity(final File file, final String contentType) {
052        super();
053        this.file = Args.notNull(file, "File");
054        setContentType(contentType);
055    }
056
057    /**
058     * @since 4.2
059     */
060    public FileEntity(final File file, final ContentType contentType) {
061        super();
062        this.file = Args.notNull(file, "File");
063        if (contentType != null) {
064            setContentType(contentType.toString());
065        }
066    }
067
068    /**
069     * @since 4.2
070     */
071    public FileEntity(final File file) {
072        super();
073        this.file = Args.notNull(file, "File");
074    }
075
076    @Override
077    public boolean isRepeatable() {
078        return true;
079    }
080
081    @Override
082    public long getContentLength() {
083        return this.file.length();
084    }
085
086    @Override
087    public InputStream getContent() throws IOException {
088        return new FileInputStream(this.file);
089    }
090
091    @Override
092    public void writeTo(final OutputStream outstream) throws IOException {
093        Args.notNull(outstream, "Output stream");
094        final InputStream instream = new FileInputStream(this.file);
095        try {
096            final byte[] tmp = new byte[OUTPUT_BUFFER_SIZE];
097            int l;
098            while ((l = instream.read(tmp)) != -1) {
099                outstream.write(tmp, 0, l);
100            }
101            outstream.flush();
102        } finally {
103            instream.close();
104        }
105    }
106
107    /**
108     * Tells that this entity is not streaming.
109     *
110     * @return {@code false}
111     */
112    @Override
113    public boolean isStreaming() {
114        return false;
115    }
116
117    @Override
118    public Object clone() throws CloneNotSupportedException {
119        // File instance is considered immutable
120        // No need to make a copy of it
121        return super.clone();
122    }
123
124} // class FileEntity