Code example for URI

Methods: getAuthority, getPath, getScheme

0
 
    private BufferedReader openResourceFile(URI filename) {
        InputStream stream;
        try { 
            stream = getContext().getResources().openRawResource(
                    Integer.valueOf(filename.getAuthority()));
        } catch(Resources.NotFoundException e) {
            Log.w(TAG, "Unable to find a trace resource with URI " + filename
                    + " -- returning an empty buffer"); 
            stream = new ByteArrayInputStream(new byte[0]);
        } 
        return readerForStream(stream);
    } 
 
    private BufferedReader openRegularFile(URI filename)
            throws DataSourceException { 
        FileInputStream stream;
        try { 
            stream = new FileInputStream(filename.getPath());
        } catch(FileNotFoundException e) {
            throw new DataSourceException( 
                "Couldn't open the trace file " + filename, e);
        } 
 
        return readerForStream(stream);
    } 
 
    private BufferedReader readerForStream(InputStream stream) {
        DataInputStream dataStream = new DataInputStream(stream);
        return new BufferedReader(new InputStreamReader(dataStream));
    } 
 
    private BufferedReader openFile(URI filename)
            throws DataSourceException { 
        String scheme = filename.getScheme();
        if(scheme != null && scheme.equals("resource")) {
            return openResourceFile(filename);
        } else { 
            return openRegularFile(filename);
        }