Search in sources :

Example 66 with FileNotFoundException

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

the class ShortcutService method loadBaseStateLocked.

private void loadBaseStateLocked() {
    mRawLastResetTime = 0;
    final AtomicFile file = getBaseStateFile();
    if (DEBUG) {
        Slog.d(TAG, "Loading from " + file.getBaseFile());
    }
    try (FileInputStream in = file.openRead()) {
        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(in, StandardCharsets.UTF_8.name());
        int type;
        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
            if (type != XmlPullParser.START_TAG) {
                continue;
            }
            final int depth = parser.getDepth();
            // Check the root tag
            final String tag = parser.getName();
            if (depth == 1) {
                if (!TAG_ROOT.equals(tag)) {
                    Slog.e(TAG, "Invalid root tag: " + tag);
                    return;
                }
                continue;
            }
            // Assume depth == 2
            switch(tag) {
                case TAG_LAST_RESET_TIME:
                    mRawLastResetTime = parseLongAttribute(parser, ATTR_VALUE);
                    break;
                default:
                    Slog.e(TAG, "Invalid tag: " + tag);
                    break;
            }
        }
    } catch (FileNotFoundException e) {
    // Use the default
    } catch (IOException | XmlPullParserException e) {
        Slog.e(TAG, "Failed to read file " + file.getBaseFile(), e);
        mRawLastResetTime = 0;
    }
    // Adjust the last reset time.
    getLastResetTimeLocked();
}
Also used : AtomicFile(android.util.AtomicFile) XmlPullParser(org.xmlpull.v1.XmlPullParser) FileNotFoundException(java.io.FileNotFoundException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 67 with FileNotFoundException

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

the class ShortcutService method loadUserLocked.

@Nullable
private ShortcutUser loadUserLocked(@UserIdInt int userId) {
    final File path = getUserFile(userId);
    if (DEBUG) {
        Slog.d(TAG, "Loading from " + path);
    }
    final AtomicFile file = new AtomicFile(path);
    final FileInputStream in;
    try {
        in = file.openRead();
    } catch (FileNotFoundException e) {
        if (DEBUG) {
            Slog.d(TAG, "Not found " + path);
        }
        return null;
    }
    try {
        final ShortcutUser ret = loadUserInternal(userId, in, /* forBackup= */
        false);
        return ret;
    } catch (IOException | XmlPullParserException | InvalidFileFormatException e) {
        Slog.e(TAG, "Failed to read file " + file.getBaseFile(), e);
        return null;
    } finally {
        IoUtils.closeQuietly(in);
    }
}
Also used : AtomicFile(android.util.AtomicFile) FileNotFoundException(java.io.FileNotFoundException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException) File(java.io.File) AtomicFile(android.util.AtomicFile) FileInputStream(java.io.FileInputStream) Nullable(android.annotation.Nullable)

Example 68 with FileNotFoundException

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

the class CopyJob method byteCopyDocument.

void byteCopyDocument(DocumentInfo src, DocumentInfo dest) throws ResourceException {
    final String dstMimeType;
    final String dstDisplayName;
    if (DEBUG)
        Log.d(TAG, "Doing byte copy of document: " + src);
    // as such format. Also, append an extension for the target mime type (if known).
    if (src.isVirtualDocument()) {
        String[] streamTypes = null;
        try {
            streamTypes = getContentResolver().getStreamTypes(src.derivedUri, "*/*");
        } catch (RuntimeException e) {
            throw new ResourceException("Failed to obtain streamable types for %s due to an exception.", src.derivedUri, e);
        }
        if (streamTypes != null && streamTypes.length > 0) {
            dstMimeType = streamTypes[0];
            final String extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(dstMimeType);
            dstDisplayName = src.displayName + (extension != null ? "." + extension : src.displayName);
        } else {
            throw new ResourceException("Cannot copy virtual file %s. No streamable formats " + "available.", src.derivedUri);
        }
    } else {
        dstMimeType = src.mimeType;
        dstDisplayName = src.displayName;
    }
    // Create the target document (either a file or a directory), then copy recursively the
    // contents (bytes or children).
    Uri dstUri = null;
    try {
        dstUri = DocumentsContract.createDocument(getClient(dest), dest.derivedUri, dstMimeType, dstDisplayName);
    } catch (RemoteException | RuntimeException e) {
        throw new ResourceException("Couldn't create destination document " + dstDisplayName + " in directory %s " + "due to an exception.", dest.derivedUri, e);
    }
    if (dstUri == null) {
        // If this is a directory, the entire subdir will not be copied over.
        throw new ResourceException("Couldn't create destination document " + dstDisplayName + " in directory %s.", dest.derivedUri);
    }
    DocumentInfo dstInfo = null;
    try {
        dstInfo = DocumentInfo.fromUri(getContentResolver(), dstUri);
    } catch (FileNotFoundException | RuntimeException e) {
        throw new ResourceException("Could not load DocumentInfo for newly created file %s.", dstUri);
    }
    if (Document.MIME_TYPE_DIR.equals(src.mimeType)) {
        copyDirectoryHelper(src, dstInfo);
    } else {
        copyFileHelper(src, dstInfo, dest, dstMimeType);
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) DocumentInfo.getCursorString(com.android.documentsui.model.DocumentInfo.getCursorString) RemoteException(android.os.RemoteException) Uri(android.net.Uri) DocumentsContract.buildDocumentUri(android.provider.DocumentsContract.buildDocumentUri) DocumentsContract.buildChildDocumentsUri(android.provider.DocumentsContract.buildChildDocumentsUri) DocumentInfo(com.android.documentsui.model.DocumentInfo)

Example 69 with FileNotFoundException

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

the class CopyJob method copyFileHelper.

/**
     * Handles copying a single file.
     *
     * @param src Info of the file to copy from.
     * @param dest Info of the *file* to copy to. Must be created beforehand.
     * @param destParent Info of the parent of the destination.
     * @param mimeType Mime type for the target. Can be different than source for virtual files.
     * @throws ResourceException
     */
private void copyFileHelper(DocumentInfo src, DocumentInfo dest, DocumentInfo destParent, String mimeType) throws ResourceException {
    CancellationSignal canceller = new CancellationSignal();
    AssetFileDescriptor srcFileAsAsset = null;
    ParcelFileDescriptor srcFile = null;
    ParcelFileDescriptor dstFile = null;
    InputStream in = null;
    ParcelFileDescriptor.AutoCloseOutputStream out = null;
    boolean success = false;
    try {
        // as such format.
        if (src.isVirtualDocument()) {
            try {
                srcFileAsAsset = getClient(src).openTypedAssetFileDescriptor(src.derivedUri, mimeType, null, canceller);
            } catch (FileNotFoundException | RemoteException | RuntimeException e) {
                throw new ResourceException("Failed to open a file as asset for %s due to an " + "exception.", src.derivedUri, e);
            }
            srcFile = srcFileAsAsset.getParcelFileDescriptor();
            try {
                in = new AssetFileDescriptor.AutoCloseInputStream(srcFileAsAsset);
            } catch (IOException e) {
                throw new ResourceException("Failed to open a file input stream for %s due " + "an exception.", src.derivedUri, e);
            }
        } else {
            try {
                srcFile = getClient(src).openFile(src.derivedUri, "r", canceller);
            } catch (FileNotFoundException | RemoteException | RuntimeException e) {
                throw new ResourceException("Failed to open a file for %s due to an exception.", src.derivedUri, e);
            }
            in = new ParcelFileDescriptor.AutoCloseInputStream(srcFile);
        }
        try {
            dstFile = getClient(dest).openFile(dest.derivedUri, "w", canceller);
        } catch (FileNotFoundException | RemoteException | RuntimeException e) {
            throw new ResourceException("Failed to open the destination file %s for writing " + "due to an exception.", dest.derivedUri, e);
        }
        out = new ParcelFileDescriptor.AutoCloseOutputStream(dstFile);
        byte[] buffer = new byte[32 * 1024];
        int len;
        try {
            while ((len = in.read(buffer)) != -1) {
                if (isCanceled()) {
                    if (DEBUG)
                        Log.d(TAG, "Canceled copy mid-copy of: " + src.derivedUri);
                    return;
                }
                out.write(buffer, 0, len);
                makeCopyProgress(len);
            }
            // Need to invoke IoUtils.close explicitly to avoid from ignoring errors at flush.
            IoUtils.close(dstFile.getFileDescriptor());
            srcFile.checkError();
        } catch (IOException e) {
            throw new ResourceException("Failed to copy bytes from %s to %s due to an IO exception.", src.derivedUri, dest.derivedUri, e);
        }
        if (src.isVirtualDocument()) {
            convertedFiles.add(src);
        }
        success = true;
    } finally {
        if (!success) {
            if (dstFile != null) {
                try {
                    dstFile.closeWithError("Error copying bytes.");
                } catch (IOException closeError) {
                    Log.w(TAG, "Error closing destination.", closeError);
                }
            }
            if (DEBUG)
                Log.d(TAG, "Cleaning up failed operation leftovers.");
            canceller.cancel();
            try {
                deleteDocument(dest, destParent);
            } catch (ResourceException e) {
                Log.w(TAG, "Failed to cleanup after copy error: " + src.derivedUri, e);
            }
        }
        // This also ensures the file descriptors are closed.
        IoUtils.closeQuietly(in);
        IoUtils.closeQuietly(out);
    }
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) ParcelFileDescriptor(android.os.ParcelFileDescriptor) RemoteException(android.os.RemoteException) CancellationSignal(android.os.CancellationSignal)

Example 70 with FileNotFoundException

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

the class StubProvider method createRegularFile.

@VisibleForTesting
public Uri createRegularFile(String rootId, String path, String mimeType, byte[] content) throws FileNotFoundException, IOException {
    final File file = createFile(rootId, path, mimeType, content);
    final StubDocument parent = mStorage.get(getDocumentIdForFile(file.getParentFile()));
    if (parent == null) {
        throw new FileNotFoundException("Parent not found.");
    }
    final StubDocument document = StubDocument.createRegularDocument(file, mimeType, parent);
    mStorage.put(document.documentId, document);
    return DocumentsContract.buildDocumentUri(mAuthority, document.documentId);
}
Also used : FileNotFoundException(java.io.FileNotFoundException) File(java.io.File) VisibleForTesting(android.support.annotation.VisibleForTesting)

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