1 package tim.prune.load;
4 import java.io.IOException;
5 import java.io.InputStream;
7 import java.util.zip.ZipEntry;
8 import java.util.zip.ZipFile;
10 import tim.prune.data.AudioClip;
11 import tim.prune.data.MediaObject;
12 import tim.prune.data.Photo;
15 * Class to provide helper functions for loading media
17 public abstract class MediaHelper
20 private static GenericFileFilter _jpegFilter = null, _audioFilter = null;
24 * Construct a MediaObject for the given path
25 * @param inZipFile path to archive file (if any)
26 * @param inPath path to media file
27 * @param inSourceFile file from which data was loaded
28 * @return either Photo or AudioClip object as appropriate, or null
30 public static MediaObject createMediaObject(File inZipFile, String inPath, File inSourceFile)
32 if (inPath == null || inPath.length() < 5) return null;
33 InputStream is = null;
39 // Check if path is a URL, in which case get an input stream from it
40 if (inPath.substring(0, 5).toLowerCase().equals("http:"))
43 is = new URL(inPath).openStream();
44 data = ByteScooper.scoop(is);
47 catch (IOException ioe) {
48 System.err.println("Got ioe from url: " + ioe.getMessage());
51 // Now see if file is in the zip file
52 if (is == null && inZipFile != null && inZipFile.exists() && inZipFile.canRead())
56 zf = new ZipFile(inZipFile);
57 ZipEntry entry = zf.getEntry(inPath);
58 if (entry != null && entry.getSize() > 0)
60 data = ByteScooper.scoop(zf.getInputStream(entry));
61 // System.out.println("Size of data " + (data.length == entry.getSize()?"matches":"DOESN'T match"));
64 catch (IOException ioe) {
65 System.err.println("Got ioe from zip file: " + ioe.getMessage());
68 // Clean up input streams
71 } catch (IOException ioe) {}
74 } catch (IOException ioe) {}
78 // Create Photo or AudioClip using this entry
79 String filename = new File(inPath).getName();
81 if (_jpegFilter.acceptFilename(inPath)) {
82 return new Photo(data, filename, url);
84 else if (_audioFilter.acceptFilename(inPath)) {
85 return new AudioClip(data, filename, url);
90 // If we haven't got a result by now, try to load plain file
91 File file = (inSourceFile == null ? new File(inPath) : new File(inSourceFile.getParent(), inPath));
92 return createMediaObject(file);
96 * Construct a MediaObject for the given file
97 * @param inFile file to load
98 * @return either Photo or AudioClip object as appropriate, or null
100 private static MediaObject createMediaObject(File inFile)
102 if (inFile == null) {return null;}
103 if (!inFile.exists() || !inFile.canRead() || !inFile.isFile()) {return null;}
105 // Check if filename looks like a jpeg
106 if (_jpegFilter.acceptFilename(inFile.getName())) {
107 return JpegLoader.createPhoto(inFile);
109 // Check if filename looks like an audio clip
110 if (_audioFilter.acceptFilename(inFile.getName())) {
111 return new AudioClip(inFile);
113 // Neither photo nor audio
118 * Initialise filters if necessary
120 private static void initFilters()
122 if (_jpegFilter == null) {
123 _jpegFilter = new JpegFileFilter();
124 _audioFilter = new AudioFileFilter();