Search in sources :

Example 1 with StreamNotFoundException

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

the class WriteFileAbstraction method doInBackground.

@Override
protected Integer doInBackground(Void... voids) {
    try {
        OutputStream outputStream;
        switch(fileAbstraction.scheme) {
            case EditableFileAbstraction.SCHEME_CONTENT:
                if (fileAbstraction.uri == null)
                    throw new NullPointerException("Something went really wrong!");
                try {
                    outputStream = contentResolver.openOutputStream(fileAbstraction.uri);
                } catch (RuntimeException e) {
                    throw new StreamNotFoundException(e);
                }
                break;
            case EditableFileAbstraction.SCHEME_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 = FileUtil.getOutputStream(hybridFileParcelable.getFile(), context);
                if (isRootExplorer && outputStream == null) {
                    // try loading stream associated using root
                    try {
                        if (cachedFile != null && cachedFile.exists()) {
                            outputStream = new FileOutputStream(cachedFile);
                        }
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                        outputStream = null;
                    }
                }
                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()) {
            // cat cache content to original file and delete cache file
            RootUtils.cat(cachedFile.getPath(), fileAbstraction.hybridFileParcelable.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) ShellNotRunningException(com.amaze.filemanager.exceptions.ShellNotRunningException) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) StreamNotFoundException(com.amaze.filemanager.exceptions.StreamNotFoundException) IOException(java.io.IOException)

Example 2 with StreamNotFoundException

use of com.amaze.filemanager.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 EditableFileAbstraction.SCHEME_CONTENT:
                if (fileAbstraction.uri == null)
                    throw new NullPointerException("Something went really wrong!");
                inputStream = contentResolver.openInputStream(fileAbstraction.uri);
                break;
            case EditableFileAbstraction.SCHEME_FILE:
                final HybridFileParcelable hybridFileParcelable = fileAbstraction.hybridFileParcelable;
                if (hybridFileParcelable == null)
                    throw new NullPointerException("Something went really wrong!");
                File file = hybridFileParcelable.getFile();
                if (!file.canWrite() && isRootExplorer) {
                    // try loading stream associated using root
                    try {
                        cachedFile = new File(externalCacheDir, hybridFileParcelable.getName());
                        // creating a cache file
                        RootUtils.copy(hybridFileParcelable.getPath(), cachedFile.getPath());
                        inputStream = new FileInputStream(cachedFile);
                    } catch (ShellNotRunningException e) {
                        e.printStackTrace();
                        inputStream = null;
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                        inputStream = null;
                    }
                } else if (file.canRead()) {
                    // readable file in filesystem
                    try {
                        inputStream = new FileInputStream(hybridFileParcelable.getPath());
                    } catch (FileNotFoundException e) {
                        inputStream = null;
                    }
                }
                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);
    }
    return new ReturnedValues(stringBuilder.toString(), cachedFile);
}
Also used : InputStreamReader(java.io.InputStreamReader) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) HybridFileParcelable(com.amaze.filemanager.filesystem.HybridFileParcelable) ShellNotRunningException(com.amaze.filemanager.exceptions.ShellNotRunningException) BufferedReader(java.io.BufferedReader) StreamNotFoundException(com.amaze.filemanager.exceptions.StreamNotFoundException) File(java.io.File)

Aggregations

ShellNotRunningException (com.amaze.filemanager.exceptions.ShellNotRunningException)2 StreamNotFoundException (com.amaze.filemanager.exceptions.StreamNotFoundException)2 HybridFileParcelable (com.amaze.filemanager.filesystem.HybridFileParcelable)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 Context (android.content.Context)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)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