com.rt.storage.api.client.http.FileContent Maven / Gradle / Ivy
package com.rt.storage.api.client.http;
import com.rt.storage.api.client.util.Preconditions;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
/**
* Concrete implementation of {@link AbstractInputStreamContent} that generates repeatable input
* streams based on the contents of a file.
*
* Sample use:
*
*
*
* private static void setRequestJpegContent(HttpRequest request, File jpegFile) {
* request.setContent(new FileContent("image/jpeg", jpegFile));
* }
*
*
*
* Implementation is not thread-safe.
*
* @since 1.4
*/
public final class FileContent extends AbstractInputStreamContent {
private final File file;
/**
* @param type Content type or {@code null} for none
* @param file file
* @since 1.5
*/
public FileContent(String type, File file) {
super(type);
this.file = Preconditions.checkNotNull(file);
}
public long getLength() {
return file.length();
}
public boolean retrySupported() {
return true;
}
@Override
public InputStream getInputStream() throws FileNotFoundException {
return new FileInputStream(file);
}
/**
* Returns the file.
*
* @since 1.5
*/
public File getFile() {
return file;
}
@Override
public FileContent setType(String type) {
return (FileContent) super.setType(type);
}
@Override
public FileContent setCloseInputStream(boolean closeInputStream) {
return (FileContent) super.setCloseInputStream(closeInputStream);
}
}