Search in sources :

Example 1 with StreamNotFoundException

use of com.amaze.filemanager.file_operations.exceptions.StreamNotFoundException in project AmazeFileManager by TeamAmaze.

the class WriteFileAbstraction method doInBackground.

@Override
protected Integer doInBackground(Void... voids) {
    try {
        OutputStream outputStream;
        File destFile = null;
        switch(fileAbstraction.scheme) {
            case CONTENT:
                if (fileAbstraction.uri == null)
                    throw new NullPointerException("Something went really wrong!");
                try {
                    if (fileAbstraction.uri.getAuthority().equals(context.get().getPackageName())) {
                        DocumentFile documentFile = DocumentFile.fromSingleUri(AppConfig.getInstance(), fileAbstraction.uri);
                        if (documentFile != null && documentFile.exists() && documentFile.canWrite())
                            outputStream = contentResolver.openOutputStream(fileAbstraction.uri);
                        else {
                            destFile = FileUtils.fromContentUri(fileAbstraction.uri);
                            outputStream = openFile(destFile, context.get());
                        }
                    } else {
                        outputStream = contentResolver.openOutputStream(fileAbstraction.uri);
                    }
                } catch (RuntimeException e) {
                    throw new StreamNotFoundException(e);
                }
                break;
            case FILE:
                final HybridFileParcelable hybridFileParcelable = fileAbstraction.hybridFileParcelable;
                if (hybridFileParcelable == null)
                    throw new NullPointerException("Something went really wrong!");
                Context context = this.context.get();
                if (context == null) {
                    cancel(true);
                    return null;
                }
                outputStream = openFile(hybridFileParcelable.getFile(), context);
                destFile = fileAbstraction.hybridFileParcelable.getFile();
                break;
            default:
                throw new IllegalArgumentException("The scheme for '" + fileAbstraction.scheme + "' cannot be processed!");
        }
        if (outputStream == null)
            throw new StreamNotFoundException();
        outputStream.write(dataToSave.getBytes());
        outputStream.close();
        if (cachedFile != null && cachedFile.exists() && destFile != null) {
            // cat cache content to original file and delete cache file
            ConcatenateFileCommand.INSTANCE.concatenateFile(cachedFile.getPath(), destFile.getPath());
            cachedFile.delete();
        }
    } catch (IOException e) {
        e.printStackTrace();
        return EXCEPTION_IO;
    } catch (StreamNotFoundException e) {
        e.printStackTrace();
        return EXCEPTION_STREAM_NOT_FOUND;
    } catch (ShellNotRunningException e) {
        e.printStackTrace();
        return EXCEPTION_SHELL_NOT_RUNNING;
    }
    return NORMAL;
}
Also used : HybridFileParcelable(com.amaze.filemanager.filesystem.HybridFileParcelable) Context(android.content.Context) DocumentFile(androidx.documentfile.provider.DocumentFile) ShellNotRunningException(com.amaze.filemanager.file_operations.exceptions.ShellNotRunningException) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) StreamNotFoundException(com.amaze.filemanager.file_operations.exceptions.StreamNotFoundException) IOException(java.io.IOException) File(java.io.File) DocumentFile(androidx.documentfile.provider.DocumentFile)

Example 2 with StreamNotFoundException

use of com.amaze.filemanager.file_operations.exceptions.StreamNotFoundException in project AmazeFileManager by TeamAmaze.

the class ReadFileTask method doInBackground.

@Override
protected ReturnedValues doInBackground(Void... params) {
    StringBuilder stringBuilder = new StringBuilder();
    try {
        InputStream inputStream = null;
        switch(fileAbstraction.scheme) {
            case CONTENT:
                if (fileAbstraction.uri == null)
                    throw new NullPointerException("Something went really wrong!");
                if (fileAbstraction.uri.getAuthority().equals(AppConfig.getInstance().getPackageName())) {
                    DocumentFile documentFile = DocumentFile.fromSingleUri(AppConfig.getInstance(), fileAbstraction.uri);
                    if (documentFile != null && documentFile.exists() && documentFile.canWrite())
                        inputStream = contentResolver.openInputStream(documentFile.getUri());
                    else
                        inputStream = loadFile(FileUtils.fromContentUri(fileAbstraction.uri));
                } else {
                    inputStream = contentResolver.openInputStream(fileAbstraction.uri);
                }
                break;
            case FILE:
                final HybridFileParcelable hybridFileParcelable = fileAbstraction.hybridFileParcelable;
                if (hybridFileParcelable == null)
                    throw new NullPointerException("Something went really wrong!");
                File file = hybridFileParcelable.getFile();
                inputStream = loadFile(file);
                break;
            default:
                throw new IllegalArgumentException("The scheme for '" + fileAbstraction.scheme + "' cannot be processed!");
        }
        if (inputStream == null)
            throw new StreamNotFoundException();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String buffer;
        while ((buffer = bufferedReader.readLine()) != null) {
            stringBuilder.append(buffer).append("\n");
        }
        inputStream.close();
        bufferedReader.close();
    } catch (StreamNotFoundException e) {
        e.printStackTrace();
        return new ReturnedValues(EXCEPTION_STREAM_NOT_FOUND);
    } catch (IOException e) {
        e.printStackTrace();
        return new ReturnedValues(EXCEPTION_IO);
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
        return new ReturnedValues(EXCEPTION_OOM);
    }
    return new ReturnedValues(stringBuilder.toString(), cachedFile);
}
Also used : DocumentFile(androidx.documentfile.provider.DocumentFile) InputStreamReader(java.io.InputStreamReader) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) HybridFileParcelable(com.amaze.filemanager.filesystem.HybridFileParcelable) BufferedReader(java.io.BufferedReader) StreamNotFoundException(com.amaze.filemanager.file_operations.exceptions.StreamNotFoundException) File(java.io.File) DocumentFile(androidx.documentfile.provider.DocumentFile)

Aggregations

DocumentFile (androidx.documentfile.provider.DocumentFile)2 StreamNotFoundException (com.amaze.filemanager.file_operations.exceptions.StreamNotFoundException)2 HybridFileParcelable (com.amaze.filemanager.filesystem.HybridFileParcelable)2 File (java.io.File)2 IOException (java.io.IOException)2 Context (android.content.Context)1 ShellNotRunningException (com.amaze.filemanager.file_operations.exceptions.ShellNotRunningException)1 BufferedReader (java.io.BufferedReader)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 OutputStream (java.io.OutputStream)1