1 /* 2 * ==================================================================== 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * ==================================================================== 20 * 21 * This software consists of voluntary contributions made by many 22 * individuals on behalf of the Apache Software Foundation. For more 23 * information on the Apache Software Foundation, please see 24 * <http://www.apache.org/>. 25 * 26 */ 27 28 package org.apache.hc.core5.http.io.entity; 29 30 import java.io.File; 31 import java.io.FileInputStream; 32 import java.io.IOException; 33 import java.io.InputStream; 34 35 import org.apache.hc.core5.annotation.Contract; 36 import org.apache.hc.core5.annotation.ThreadingBehavior; 37 import org.apache.hc.core5.http.ContentType; 38 import org.apache.hc.core5.util.Args; 39 40 /** 41 * A self contained, repeatable entity that obtains its content from a file. 42 * <p> 43 * This class contains {@link ThreadingBehavior#IMMUTABLE_CONDITIONAL immutable attributes} but subclasses may contain 44 * additional immutable or mutable attributes. 45 * </p> 46 * @since 4.0 47 */ 48 @Contract(threading = ThreadingBehavior.IMMUTABLE_CONDITIONAL) 49 public class FileEntity extends AbstractHttpEntity { 50 51 private final File file; 52 53 /** 54 * Constructs a new instance with the given attributes kept as immutable. 55 * <p> 56 * The new instance: 57 * </p> 58 * <ul> 59 * <li>is not chunked.</li> 60 * </ul> 61 * 62 * @param file The message body contents will be set from this file. 63 * @param contentType The content-type, may be null. 64 * @param contentEncoding The content encoding string, may be null. 65 */ 66 public FileEntity(final File file, final ContentType contentType, final String contentEncoding) { 67 super(contentType, contentEncoding); 68 this.file = Args.notNull(file, "File"); 69 } 70 71 /** 72 * Constructs a new instance with the given attributes kept as immutable. 73 * <p> 74 * The new instance: 75 * </p> 76 * <ul> 77 * <li>is not chunked.</li> 78 * <li>does not define a content encoding.</li> 79 * </ul> 80 * 81 * @param file The message body contents will be set from this file. 82 * @param contentType The content-type, may be null. 83 */ 84 public FileEntity(final File file, final ContentType contentType) { 85 super(contentType, null); 86 this.file = Args.notNull(file, "File"); 87 } 88 89 /** 90 * {@inheritDoc} 91 * <p> 92 * This implementation always returns {@code true}. 93 * </p> 94 */ 95 @Override 96 public final boolean isRepeatable() { 97 return true; 98 } 99 100 /** 101 * {@inheritDoc} 102 * <p> 103 * This implementation returns the underlying file's current file length. 104 * </p> 105 */ 106 @Override 107 public final long getContentLength() { 108 return this.file.length(); 109 } 110 111 @Override 112 public final InputStream getContent() throws IOException { 113 return new FileInputStream(this.file); 114 } 115 116 /** 117 * {@inheritDoc} 118 * <p> 119 * This implementation always returns {@code false}. 120 * </p> 121 */ 122 @Override 123 public final boolean isStreaming() { 124 return false; 125 } 126 127 /** 128 * {@inheritDoc} 129 * <p> 130 * This implementation is a no-op. 131 * </p> 132 */ 133 @Override 134 public final void close() throws IOException { 135 // do nothing 136 } 137 138 }