/* * ==================================================================== * 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 * . * */ 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.annotation.NotThreadSafe; import org.apache.http.entity.AbstractHttpEntity; import org.apache.http.entity.ContentType; import org.apache.http.nio.ContentEncoder; import org.apache.http.nio.ContentEncoderChannel; import org.apache.http.nio.FileContentEncoder; import org.apache.http.nio.IOControl; /** * A self contained, repeatable non-blocking entity that retrieves its content * from a file. This class is mostly used to stream large files of different * types, so one needs to supply the content type of the file to make sure * the content can be correctly recognized and processed by the recipient. * * @since 4.0 */ @SuppressWarnings("deprecation") @NotThreadSafe public class NFileEntity extends AbstractHttpEntity implements HttpAsyncContentProducer, ProducingNHttpEntity { private final File file; private FileChannel fileChannel; private long idx = -1; private boolean useFileChannels; /** * Creates new instance of NFileEntity from the given source {@link File} * with the given content type. If useFileChannels is set to * true, the entity will try to use {@link FileContentEncoder} * interface to stream file content directly from the file channel. * * @param file the source file. * @param contentType the content type of the file. * @param useFileChannels flag whether the direct transfer from the file * channel should be attempted. * * @since 4.2 */ public NFileEntity(final File file, final ContentType contentType, boolean useFileChannels) { if (file == null) { throw new IllegalArgumentException("File may not be null"); } this.file = file; this.useFileChannels = useFileChannels; if (contentType != null) { setContentType(contentType.toString()); } } /** * @since 4.2 */ public NFileEntity(final File file) { if (file == null) { throw new IllegalArgumentException("File may not be null"); } this.file = file; } /** * Creates new instance of NFileEntity from the given source {@link File} * with the given content type. * * @param file the source file. * @param contentType the content type of the file. * * @since 4.2 */ public NFileEntity(final File file, final ContentType contentType) { this(file, contentType, true); } /** * @deprecated use {@link #NFileEntity(File, ContentType, boolean)} */ @Deprecated 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); } /** * @deprecated use {@link #NFileEntity(File, ContentType)} */ @Deprecated public NFileEntity(final File file, final String contentType) { this(file, contentType, true); } /** * {@inheritDoc} * * @since 4.2 */ public void close() throws IOException { FileChannel local = fileChannel; fileChannel = null; if (local != null) { local.close(); } } /** * {@inheritDoc} * * @deprecated use {@link #close()} */ public void finish() throws IOException { close(); } 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(); close(); } } 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(); } } }