Search in sources :

Example 51 with FileNotFoundException

use of java.io.FileNotFoundException in project platform_frameworks_base by android.

the class AtomicFile method truncate.

public void truncate() throws IOException {
    try {
        FileOutputStream fos = new FileOutputStream(mBaseName);
        FileUtils.sync(fos);
        fos.close();
    } catch (FileNotFoundException e) {
        throw new IOException("Couldn't append " + mBaseName);
    } catch (IOException e) {
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException)

Example 52 with FileNotFoundException

use of java.io.FileNotFoundException in project platform_frameworks_base by android.

the class ZygoteInit method preloadClasses.

/**
     * Performs Zygote process initialization. Loads and initializes
     * commonly used classes.
     *
     * Most classes only cause a few hundred bytes to be allocated, but
     * a few will allocate a dozen Kbytes (in one case, 500+K).
     */
private static void preloadClasses() {
    final VMRuntime runtime = VMRuntime.getRuntime();
    InputStream is;
    try {
        is = new FileInputStream(PRELOADED_CLASSES);
    } catch (FileNotFoundException e) {
        Log.e(TAG, "Couldn't find " + PRELOADED_CLASSES + ".");
        return;
    }
    Log.i(TAG, "Preloading classes...");
    long startTime = SystemClock.uptimeMillis();
    // Drop root perms while running static initializers.
    final int reuid = Os.getuid();
    final int regid = Os.getgid();
    // We need to drop root perms only if we're already root. In the case of "wrapped"
    // processes (see WrapperInit), this function is called from an unprivileged uid
    // and gid.
    boolean droppedPriviliges = false;
    if (reuid == ROOT_UID && regid == ROOT_GID) {
        try {
            Os.setregid(ROOT_GID, UNPRIVILEGED_GID);
            Os.setreuid(ROOT_UID, UNPRIVILEGED_UID);
        } catch (ErrnoException ex) {
            throw new RuntimeException("Failed to drop root", ex);
        }
        droppedPriviliges = true;
    }
    // Alter the target heap utilization.  With explicit GCs this
    // is not likely to have any effect.
    float defaultUtilization = runtime.getTargetHeapUtilization();
    runtime.setTargetHeapUtilization(0.8f);
    try {
        BufferedReader br = new BufferedReader(new InputStreamReader(is), 256);
        int count = 0;
        String line;
        while ((line = br.readLine()) != null) {
            // Skip comments and blank lines.
            line = line.trim();
            if (line.startsWith("#") || line.equals("")) {
                continue;
            }
            Trace.traceBegin(Trace.TRACE_TAG_DALVIK, line);
            try {
                if (false) {
                    Log.v(TAG, "Preloading " + line + "...");
                }
                // Load and explicitly initialize the given class. Use
                // Class.forName(String, boolean, ClassLoader) to avoid repeated stack lookups
                // (to derive the caller's class-loader). Use true to force initialization, and
                // null for the boot classpath class-loader (could as well cache the
                // class-loader of this class in a variable).
                Class.forName(line, true, null);
                count++;
            } catch (ClassNotFoundException e) {
                Log.w(TAG, "Class not found for preloading: " + line);
            } catch (UnsatisfiedLinkError e) {
                Log.w(TAG, "Problem preloading " + line + ": " + e);
            } catch (Throwable t) {
                Log.e(TAG, "Error preloading " + line + ".", t);
                if (t instanceof Error) {
                    throw (Error) t;
                }
                if (t instanceof RuntimeException) {
                    throw (RuntimeException) t;
                }
                throw new RuntimeException(t);
            }
            Trace.traceEnd(Trace.TRACE_TAG_DALVIK);
        }
        Log.i(TAG, "...preloaded " + count + " classes in " + (SystemClock.uptimeMillis() - startTime) + "ms.");
    } catch (IOException e) {
        Log.e(TAG, "Error reading " + PRELOADED_CLASSES + ".", e);
    } finally {
        IoUtils.closeQuietly(is);
        // Restore default.
        runtime.setTargetHeapUtilization(defaultUtilization);
        // Fill in dex caches with classes, fields, and methods brought in by preloading.
        Trace.traceBegin(Trace.TRACE_TAG_DALVIK, "PreloadDexCaches");
        runtime.preloadDexCaches();
        Trace.traceEnd(Trace.TRACE_TAG_DALVIK);
        // Bring back root. We'll need it later if we're in the zygote.
        if (droppedPriviliges) {
            try {
                Os.setreuid(ROOT_UID, ROOT_UID);
                Os.setregid(ROOT_GID, ROOT_GID);
            } catch (ErrnoException ex) {
                throw new RuntimeException("Failed to restore root", ex);
            }
        }
    }
}
Also used : VMRuntime(dalvik.system.VMRuntime) InputStreamReader(java.io.InputStreamReader) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) ErrnoException(android.system.ErrnoException) BufferedReader(java.io.BufferedReader)

Example 53 with FileNotFoundException

use of java.io.FileNotFoundException in project platform_frameworks_base by android.

the class AccountSyncSettingsBackupHelper method accountAddedInternal.

/**
     * Restore SyncSettings for all existing accounts from a stashed backup-set
     */
private void accountAddedInternal() {
    String jsonString;
    try (FileInputStream fIn = new FileInputStream(new File(STASH_FILE))) {
        DataInputStream in = new DataInputStream(fIn);
        jsonString = in.readUTF();
    } catch (FileNotFoundException fnfe) {
        // This is expected to happen when there is no accounts info stashed
        if (DEBUG)
            Log.d(TAG, "unable to find the stash file", fnfe);
        return;
    } catch (IOException ioe) {
        if (DEBUG)
            Log.d(TAG, "could not read sync settings from stash file", ioe);
        return;
    }
    try {
        JSONArray unaddedAccountsJSONArray = new JSONArray(jsonString);
        restoreFromJsonArray(unaddedAccountsJSONArray);
    } catch (JSONException jse) {
        // Malformed jsonString
        Log.e(TAG, "there was an error with the stashed sync settings", jse);
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) BackupDataInputStream(android.app.backup.BackupDataInputStream) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 54 with FileNotFoundException

use of java.io.FileNotFoundException in project platform_frameworks_base by android.

the class Icon method loadDrawableInner.

/**
     * Do the heavy lifting of loading the drawable, but stop short of applying any tint.
     */
private Drawable loadDrawableInner(Context context) {
    switch(mType) {
        case TYPE_BITMAP:
            return new BitmapDrawable(context.getResources(), getBitmap());
        case TYPE_RESOURCE:
            if (getResources() == null) {
                // figure out where to load resources from
                String resPackage = getResPackage();
                if (TextUtils.isEmpty(resPackage)) {
                    // if none is specified, try the given context
                    resPackage = context.getPackageName();
                }
                if ("android".equals(resPackage)) {
                    mObj1 = Resources.getSystem();
                } else {
                    final PackageManager pm = context.getPackageManager();
                    try {
                        ApplicationInfo ai = pm.getApplicationInfo(resPackage, PackageManager.GET_UNINSTALLED_PACKAGES);
                        if (ai != null) {
                            mObj1 = pm.getResourcesForApplication(ai);
                        } else {
                            break;
                        }
                    } catch (PackageManager.NameNotFoundException e) {
                        Log.e(TAG, String.format("Unable to find pkg=%s for icon %s", resPackage, this), e);
                        break;
                    }
                }
            }
            try {
                return getResources().getDrawable(getResId(), context.getTheme());
            } catch (RuntimeException e) {
                Log.e(TAG, String.format("Unable to load resource 0x%08x from pkg=%s", getResId(), getResPackage()), e);
            }
            break;
        case TYPE_DATA:
            return new BitmapDrawable(context.getResources(), BitmapFactory.decodeByteArray(getDataBytes(), getDataOffset(), getDataLength()));
        case TYPE_URI:
            final Uri uri = getUri();
            final String scheme = uri.getScheme();
            InputStream is = null;
            if (ContentResolver.SCHEME_CONTENT.equals(scheme) || ContentResolver.SCHEME_FILE.equals(scheme)) {
                try {
                    is = context.getContentResolver().openInputStream(uri);
                } catch (Exception e) {
                    Log.w(TAG, "Unable to load image from URI: " + uri, e);
                }
            } else {
                try {
                    is = new FileInputStream(new File(mString1));
                } catch (FileNotFoundException e) {
                    Log.w(TAG, "Unable to load image from path: " + uri, e);
                }
            }
            if (is != null) {
                return new BitmapDrawable(context.getResources(), BitmapFactory.decodeStream(is));
            }
            break;
    }
    return null;
}
Also used : PackageManager(android.content.pm.PackageManager) DataInputStream(java.io.DataInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ApplicationInfo(android.content.pm.ApplicationInfo) FileNotFoundException(java.io.FileNotFoundException) Uri(android.net.Uri) File(java.io.File) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) FileInputStream(java.io.FileInputStream)

Example 55 with FileNotFoundException

use of java.io.FileNotFoundException in project platform_frameworks_base by android.

the class ExternalStorageProvider method getDocIdForFileMaybeCreate.

private String getDocIdForFileMaybeCreate(File file, boolean createNewDir) throws FileNotFoundException {
    String path = file.getAbsolutePath();
    // Find the most-specific root path
    String mostSpecificId = null;
    String mostSpecificPath = null;
    synchronized (mRootsLock) {
        for (int i = 0; i < mRoots.size(); i++) {
            final String rootId = mRoots.keyAt(i);
            final String rootPath = mRoots.valueAt(i).path.getAbsolutePath();
            if (path.startsWith(rootPath) && (mostSpecificPath == null || rootPath.length() > mostSpecificPath.length())) {
                mostSpecificId = rootId;
                mostSpecificPath = rootPath;
            }
        }
    }
    if (mostSpecificPath == null) {
        throw new FileNotFoundException("Failed to find root that contains " + path);
    }
    // Start at first char of path under root
    final String rootPath = mostSpecificPath;
    if (rootPath.equals(path)) {
        path = "";
    } else if (rootPath.endsWith("/")) {
        path = path.substring(rootPath.length());
    } else {
        path = path.substring(rootPath.length() + 1);
    }
    if (!file.exists() && createNewDir) {
        Log.i(TAG, "Creating new directory " + file);
        if (!file.mkdir()) {
            Log.e(TAG, "Could not create directory " + file);
        }
    }
    return mostSpecificId + ':' + path;
}
Also used : FileNotFoundException(java.io.FileNotFoundException) Point(android.graphics.Point)

Aggregations

FileNotFoundException (java.io.FileNotFoundException)3572 IOException (java.io.IOException)2027 File (java.io.File)1415 FileInputStream (java.io.FileInputStream)906 InputStream (java.io.InputStream)535 FileOutputStream (java.io.FileOutputStream)522 BufferedReader (java.io.BufferedReader)301 FileReader (java.io.FileReader)267 ArrayList (java.util.ArrayList)232 Path (org.apache.hadoop.fs.Path)224 Test (org.junit.Test)212 InputStreamReader (java.io.InputStreamReader)193 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)189 XmlPullParser (org.xmlpull.v1.XmlPullParser)166 BufferedInputStream (java.io.BufferedInputStream)154 URL (java.net.URL)139 ParcelFileDescriptor (android.os.ParcelFileDescriptor)131 FileStatus (org.apache.hadoop.fs.FileStatus)131 Properties (java.util.Properties)129 HashMap (java.util.HashMap)120