com.kaltura.client.FileHolder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kalturaApiClient Show documentation
Show all versions of kalturaApiClient Show documentation
KalturaClient is a library of Java classes that can be used to interact
with the Kaltura REST API. More information about the REST API can be
found at http://corp.kaltura.com/Products/Kaltura-API Many of the Java classes
in this library are auto-generated from a schema that defines the objects that
are used to interect with the API. The current schema can be found at
http://www.kaltura.com/api_v3/api_schema.php
/**
* Copyright 2011 Unicon (R) Licensed under the
* Educational Community 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.osedu.org/licenses/ECL-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.
*/
package com.kaltura.client;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
/**
* This is an abstraction of a file which allows this construct to hold a File OR a Stream
*
* @author Aaron Zeckoski (azeckoski @ vt.edu)
*/
@SuppressWarnings("serial")
public class FileHolder implements Serializable {
private String name;
private long size;
private File file;
private InputStream inputStream;
private String mimeType;
/**
* Create a KF from a File object
* @param file the file (must not be null)
*/
public FileHolder(File file) {
if (file == null) {
throw new IllegalArgumentException("file must be set");
}
if (! file.canRead() || ! file.isFile()) {
throw new IllegalArgumentException("file ("+file.getAbsolutePath()+") is not readable or not a file");
}
this.file = file;
this.name = this.file.getName();
this.size = this.file.length();
try {
this.mimeType = this.file.toURI().toURL().openConnection().getContentType();
} catch (IOException e) {
this.mimeType = "application/octet-stream";
}
}
/**
* Create a KF from a FileInputStream object
* @param fileInputStream the file stream (must not be null)
* @param mimeType mime type
* @param name the file name
*/
public FileHolder(FileInputStream fileInputStream, String mimeType, String name) {
if (fileInputStream == null) {
throw new IllegalArgumentException("fileInputStream must be set");
}
if (name == null || "".equals(name)) {
throw new IllegalArgumentException("name must be set");
}
this.inputStream = fileInputStream;
this.name = name;
this.mimeType = mimeType;
try {
this.size = fileInputStream.getChannel().size();
} catch (IOException e) {
// should not happen
throw new RuntimeException("Failure trying to read info from inptustream: "+e, e);
}
}
/**
* Create a KF from a normal input stream and some params
* @param inputStream the file content stream (must not be null)
* @param mimeType mime type
* @param name the file name
* @param size the file size
*/
public FileHolder(InputStream inputStream, String mimeType, String name, long size) {
if (inputStream == null) {
throw new IllegalArgumentException("fileInputStream must be set");
}
if (name == null || "".equals(name)) {
throw new IllegalArgumentException("name must be set");
}
if (size <= 0) {
throw new IllegalArgumentException("size must be set");
}
this.inputStream = inputStream;
this.name = name;
this.mimeType = mimeType;
this.size = size;
}
/**
* @return the name for the file (is NEVER null)
*/
public String getName() {
return name;
}
/**
* @return the File object if one is set (this can be null)
*/
public File getFile() {
return file;
}
/**
* @return the size of this file
*/
public long getSize() {
return size;
}
/**
* @return the mime-type this file
*/
public String getMimeType() {
return mimeType;
}
/**
* @return the input stream for this File (this is NEVER null)
*/
public InputStream getInputStream() {
InputStream fis = inputStream;
if (inputStream == null && file != null) {
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException e) {
// should not be possible for this to happen
throw new IllegalArgumentException("file ("+file.getAbsolutePath()+") is not readable or not a file");
}
}
return fis;
}
}