Package org.apache.http.nio.entity

Source Code of org.apache.http.nio.entity.NFileEntity

/*
* $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta3/module-nio/src/main/java/org/apache/http/nio/entity/NFileEntity.java $
* $Revision: 632746 $
* $Date: 2008-03-02 13:23:03 +0100 (Sun, 02 Mar 2008) $
*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation.  For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/

package org.apache.http.nio.entity;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.channels.FileChannel;

import org.apache.http.entity.AbstractHttpEntity;
import org.apache.http.nio.ContentEncoder;
import org.apache.http.nio.ContentEncoderChannel;
import org.apache.http.nio.FileContentEncoder;
import org.apache.http.nio.IOControl;

/**
* An entity whose content is retrieved from from a file.
*
* @author <a href="mailto:sberlin at gmail.com">Sam Berlin</a>
*
* @version $Revision: 632746 $
*
* @since 4.0
*/
public class NFileEntity extends AbstractHttpEntity implements ProducingNHttpEntity {

    private final File file;
    private FileChannel fileChannel;
    private long idx = -1;
    private boolean useFileChannels;

    public NFileEntity(final File file, final String contentType, boolean useFileChannels) {
        if (file == null) {
            throw new IllegalArgumentException("File may not be null");
        }
        this.file = file;
        this.useFileChannels = useFileChannels;
        setContentType(contentType);
    }

    public NFileEntity(final File file, final String contentType) {
        this(file, contentType, true);
    }

    public void finish() {
        try {
            if(fileChannel != null)
                fileChannel.close();
        } catch(IOException ignored) {}
        fileChannel = null;
    }

    public long getContentLength() {
        return file.length();
    }

    public boolean isRepeatable() {
        return true;
    }

    public void produceContent(ContentEncoder encoder, IOControl ioctrl)
            throws IOException {
        if(fileChannel == null) {
            FileInputStream in = new FileInputStream(file);
            fileChannel = in.getChannel();
            idx = 0;
        }

        long transferred;
        if(useFileChannels && encoder instanceof FileContentEncoder) {
            transferred = ((FileContentEncoder)encoder)
                .transfer(fileChannel, idx, Long.MAX_VALUE);
        } else {
            transferred = fileChannel.
                transferTo(idx, Long.MAX_VALUE, new ContentEncoderChannel(encoder));
        }

        if(transferred > 0)
            idx += transferred;

        if(idx >= fileChannel.size())
            encoder.complete();
    }

    public boolean isStreaming() {
        return false;
    }

    public InputStream getContent() throws IOException {
        return new FileInputStream(this.file);
    }

    public void writeTo(final OutputStream outstream) throws IOException {
        if (outstream == null) {
            throw new IllegalArgumentException("Output stream may not be null");
        }
        InputStream instream = new FileInputStream(this.file);
        try {
            byte[] tmp = new byte[4096];
            int l;
            while ((l = instream.read(tmp)) != -1) {
                outstream.write(tmp, 0, l);
            }
            outstream.flush();
        } finally {
            instream.close();
        }
    }

}
TOP

Related Classes of org.apache.http.nio.entity.NFileEntity

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.