Code example for Reader

Methods: close

0
    @Override 
    public MusicDirectory getMusicDirectory(String id, Context context, ProgressListener progressListener) throws Exception {
        Reader reader = getReader(context, progressListener, "getMusicDirectory", "id", id);
        addReader(reader);
        try { 
            return musicDirectoryParser.parse(reader, progressListener);
        } finally { 
            closeReader(reader);
        } 
    } 
 
    @Override 
    public MusicDirectory search(String query, Context context, ProgressListener progressListener) throws Exception {
        Reader reader = getReader(context, progressListener, "search", "any", query);
        addReader(reader);
        try { 
            return searchResultParser.parse(reader, progressListener);
        } finally { 
            closeReader(reader);
        } 
    } 
 
    @Override 
    public MusicDirectory getPlaylist(String id, Context context, ProgressListener progressListener) throws Exception {
        Reader reader = getReader(context, progressListener, "getPlaylist", "id", id);
        addReader(reader);
        try { 
            return playlistParser.parse(reader, progressListener);
        } finally { 
            closeReader(reader);
        } 
    } 
 
    @Override 
    public List<Pair<String, String>> getPlaylists(Context context, ProgressListener progressListener) throws Exception {
        Reader reader = getReader(context, progressListener, "getPlaylists");
        addReader(reader);
        try { 
            return playlistsParser.parse(reader, progressListener);
        } finally { 
            closeReader(reader);
        } 
    } 
 
    @Override 
    public Version getLocalVersion(Context context) throws Exception {
        PackageInfo packageInfo = context.getPackageManager().getPackageInfo("net.sourceforge.subsonic.androidapp", 0);
        return new Version(packageInfo.versionName);
    } 
 
    @Override 
    public Version getLatestVersion(Context context, ProgressListener progressListener) throws Exception {
        URL url = new URL(VERSION_URL);
        Reader reader = openURL(url);
        addReader(reader);
        try { 
            return versionParser.parse(reader, progressListener);
        } finally { 
            closeReader(reader);
        } 
    } 
 
    @Override 
    public Bitmap getCoverArt(Context context, String id, int size, ProgressListener progressListener) throws Exception {
        String url = Util.getRestUrl(context, "getCoverArt") + "&id=" + id + "&size=" + size;
        URLConnection connection = new URL(url).openConnection();
        connection.setConnectTimeout(Constants.SOCKET_CONNECT_TIMEOUT);
        connection.setReadTimeout(Constants.SOCKET_READ_TIMEOUT);
        connection.connect();
        InputStream in = connection.getInputStream();
 
        try { 
            // If content type is XML, an error occured.  Get it. 
            String contentType = connection.getContentType();
            if (contentType != null && contentType.startsWith("text/xml")) {
                new ErrorParser().parse(new InputStreamReader(in, Constants.UTF_8));
                return null; // Never reached. 
            } 
 
            byte[] bytes = Util.toByteArray(in);
            return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
 
        } finally { 
            Util.close(in);
        } 
    } 
 
    @Override 
    public synchronized void cancel(Context context, ProgressListener progressListener) {
        while (!readers.isEmpty()) {
            Reader reader = readers.get(readers.size() - 1);
            closeReader(reader);
        } 
    } 
 
    private synchronized void addReader(Reader reader) {
        readers.add(reader);
    } 
 
    private synchronized void closeReader(Reader reader) {
        try { 
            reader.close();
        } catch (IOException x) {
            x.printStackTrace();
        } 
        readers.remove(reader);
    } 
 
    private Reader getReader(Context context, ProgressListener progressListener, String method) throws Exception {
        return getReader(context, progressListener, method, Collections.<String>emptyList(), Collections.emptyList());
    }