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 * @return either Photo or AudioClip object as appropriate, or null
29 public static MediaObject createMediaObject(File inZipFile, String inPath)
31 if (inPath == null || inPath.length() < 5) return null;
32 InputStream is = null;
38 // Check if path is a URL, in which case get an input stream from it
39 if (inPath.substring(0, 5).toLowerCase().equals("http:"))
42 is = new URL(inPath).openStream();
43 data = ByteScooper.scoop(is);
46 catch (IOException ioe) {
47 System.err.println("Got ioe from url: " + ioe.getMessage());
50 // Now see if file is in the zip file
51 if (is == null && inZipFile != null && inZipFile.exists() && inZipFile.canRead())
55 zf = new ZipFile(inZipFile);
56 ZipEntry entry = zf.getEntry(inPath);
57 if (entry != null && entry.getSize() > 0)
59 data = ByteScooper.scoop(zf.getInputStream(entry));
60 // System.out.println("Size of data " + (data.length == entry.getSize()?"matches":"DOESN'T match"));
63 catch (IOException ioe) {
64 System.err.println("Got ioe from zip file: " + ioe.getMessage());
67 // Clean up input streams
70 } catch (IOException ioe) {}
73 } catch (IOException ioe) {}
77 // Create Photo or AudioClip using this entry
78 String filename = new File(inPath).getName();
80 if (_jpegFilter.acceptFilename(inPath)) {
81 return new Photo(data, filename, url);
83 else if (_audioFilter.acceptFilename(inPath)) {
84 return new AudioClip(data, filename, url);
89 // If we haven't got a result by now, try to just load plain file
90 return createMediaObject(inPath);
94 * Construct a MediaObject for the given path
95 * @param inPath path to file
96 * @return either Photo or AudioClip object as appropriate, or null
98 private static MediaObject createMediaObject(String inPath)
100 if (inPath == null) {return null;}
101 File file = new File(inPath);
102 if (!file.exists() || !file.canRead() || !file.isFile()) {return null;}
104 // Check if filename looks like a jpeg
105 if (_jpegFilter.acceptFilename(file.getName())) {
106 return JpegLoader.createPhoto(file);
108 // Check if filename looks like an audio clip
109 if (_audioFilter.acceptFilename(file.getName())) {
110 return new AudioClip(file);
112 // Neither photo nor audio
117 * Initialise filters if necessary
119 private static void initFilters()
121 if (_jpegFilter == null) {
122 _jpegFilter = new JpegFileFilter();
123 _audioFilter = new AudioFileFilter();