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