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;
}
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);
}
Aggregations