package javax.emb;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
public class MediaBean implements Media
{
private String name;
private String mimeType;
private transient File tempFile;
public MediaBean(InputStream contentStream, String mimeType, String name)
throws MediaException
{
if (contentStream == null || name == null)
{
throw new NullPointerException();
}
this.name = name;
if (mimeType != null)
{
this.mimeType = mimeType;
}
else
{
try
{
MediaFormatRegistry.SINGLETON.lookup(getFileExtension(name));
}
catch (FormatNotFoundException e)
{
this.mimeType = Media.MIME_TYPE_UNKNOWN;
}
}
String tempFilePrefix = getFileName(name);
String tempFileSuffix = getFileExtension(name);
try
{
tempFile = File.createTempFile(tempFilePrefix, tempFileSuffix);
tempFile.deleteOnExit();
OutputStream tempFileStream = new FileOutputStream(tempFile);
try
{
int DEFAULT_BUFFER_SIZE = 65536;
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
int bytesRead;
while ((bytesRead = contentStream.read(buffer)) != -1)
{
tempFileStream.write(buffer, 0, bytesRead);
}
}
catch (IOException e)
{
throw new ContentAccessException(e.getMessage());
}
finally
{
try
{
tempFileStream.close();
}
catch (IOException ignore)
{
}
}
}
catch (IOException e)
{
throw new ContentAccessException(e.getMessage());
}
finally
{
try
{
contentStream.close();
}
catch (IOException ignore)
{
}
}
}
public MediaBean(File mediaFile, String mimeType) throws MediaException
{
if (mediaFile == null)
{
throw new NullPointerException();
}
if (!mediaFile.exists())
{
throw new ContentAccessException("The given file is not present or cannot be accessed.");
}
this.tempFile = mediaFile;
this.name = mediaFile.getName();
if (mimeType != null)
{
this.mimeType = mimeType;
}
else
{
try
{
MediaFormatRegistry.SINGLETON.lookup(getFileExtension(name));
}
catch (FormatNotFoundException e)
{
this.mimeType = Media.MIME_TYPE_UNKNOWN;
}
}
}
public byte[] getContent() throws MediaException
{
long size = getSize();
if (size > Integer.MAX_VALUE)
{
throw new ContentTooLargeException("Content exceeds maximum Java array size.");
}
return getContent(0, (int) size);
}
public MediaFormat getFormat() throws MediaException
{
String fileExtension = getFileExtension(name);
return MediaFormatRegistry.SINGLETON.lookup(fileExtension);
}
public MediaHeader getHeader() throws MediaException
{
return getFormat().extractHeader(getContentStream());
}
public String getMimeType() throws MediaException
{
return mimeType;
}
public String getName() throws MediaException
{
return name;
}
public Media getProxy() throws MediaException
{
return getFormat().extractProxy(getContentStream());
}
public long getSize() throws MediaException
{
return tempFile.length();
}
public int readContent(long position, byte[] buffer) throws MediaException
{
return this.readContent(position, buffer, 0, buffer.length);
}
public int readContent(long position, byte[] buffer, int offset, int length)
throws MediaException
{
if (position < 0 || position > getSize())
{
throw new IndexOutOfBoundsException();
}
if (position < 0)
{
throw new NegativeArraySizeException();
}
if (buffer == null)
{
throw new NullPointerException();
}
int bytesRead = 0;
try
{
InputStream contentStream = new FileInputStream(tempFile);
long contentPosition = contentStream.skip(position);
bytesRead = contentStream.read(buffer, offset, length);
}
catch (IOException e)
{
throw new ContentAccessException(e.getMessage());
}
return bytesRead;
}
private byte[] getContent(long position, int length) throws MediaException
{
if (position < 0 || position > getSize())
{
throw new IndexOutOfBoundsException();
}
int contentLength;
if ((getSize() - position) < (long) length)
{
contentLength = (int) (getSize() - position);
}
else
{
contentLength = length;
}
RandomAccessFile randomAccessFile = null;
try
{
randomAccessFile = new RandomAccessFile(tempFile, "r");
byte[] content = new byte[contentLength];
randomAccessFile.seek(position);
randomAccessFile.read(content);
return content;
}
catch (FileNotFoundException e)
{
throw new ContentAccessException(e.getMessage());
}
catch (IOException e)
{
throw new ContentAccessException(e.getMessage());
}
finally
{
if (randomAccessFile != null)
{
try
{
randomAccessFile.close();
}
catch (IOException ignore)
{
}
}
}
}
private InputStream getContentStream() throws MediaException
{
try
{
return new FileInputStream(tempFile);
}
catch (FileNotFoundException e)
{
throw new ContentAccessException(e.getMessage());
}
}
private static String getFileName(String name)
{
int lastDotPosition = name.lastIndexOf('.');
if (lastDotPosition == -1)
{
return name;
}
else
{
return name.substring(0, lastDotPosition);
}
}
private static String getFileExtension(String name)
{
int lastDotPosition = name.lastIndexOf('.');
if (lastDotPosition == -1)
{
return null;
}
else
{
return name.substring(lastDotPosition + 1);
}
}
}