1 /***
2 * Ambient - A music player for the Android platform
3 Copyright (C) 2007 Martin Vysny
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 package sk.baka.ambient.collection.file;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.net.MalformedURLException;
24 import java.net.URL;
25 import java.net.URLConnection;
26
27 import sk.baka.ambient.library.LibraryUtils;
28 import android.util.Log;
29
30 /***
31 * An URL audio.
32 *
33 * @author Martin Vysny
34 */
35 public final class URLAudio extends AbstractAudio {
36 private final URL url;
37 private URLConnection connection = null;
38
39 private URLConnection getConn() {
40 if (connection == null) {
41 try {
42 connection = url.openConnection();
43 } catch (IOException e) {
44 throw new RuntimeException(e);
45 }
46 }
47 return connection;
48 }
49
50 /***
51 * Creates new object instance.
52 *
53 * @param uri
54 * the file location.
55 */
56 URLAudio(final String uri) {
57 super(uri);
58 try {
59 url = new URL(uri);
60 } catch (MalformedURLException e) {
61 throw new RuntimeException(e);
62 }
63 }
64
65 @Override
66 public boolean exists() {
67 try {
68 getSize();
69 return true;
70 } catch (Exception ex) {
71 Log.e(URLAudio.class.getName(), "URL " + url + " does not exist",
72 ex);
73 return false;
74 }
75 }
76
77 @Override
78 public String getMimeType() {
79 return LibraryUtils.getMime(url.getPath());
80 }
81
82 @Override
83 public boolean isReadable() {
84 return LibraryUtils.TRACK_NAME_FILTER.accept(null, url.getPath());
85 }
86
87 @Override
88 public InputStream openInputStream() throws IOException {
89 return getConn().getInputStream();
90 }
91
92 @Override
93 public long getSize() {
94 return getConn().getContentLength();
95 }
96 }