Check resolver input/output streams

This commit is contained in:
M66B 2022-03-27 13:43:45 +02:00
parent df3e0e132a
commit 1aa80a9258
16 changed files with 77 additions and 10 deletions

View File

@ -45,6 +45,7 @@ import androidx.webkit.WebViewFeature;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
@ -207,6 +208,8 @@ public class ActivityAMP extends ActivityBase {
String html;
ContentResolver resolver = context.getContentResolver();
try (InputStream is = resolver.openInputStream(uri)) {
if (is == null)
throw new FileNotFoundException(uri.toString());
html = Helper.readStream(is);
}

View File

@ -62,7 +62,9 @@ import androidx.preference.PreferenceManager;
import androidx.recyclerview.widget.RecyclerView;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
@ -472,7 +474,11 @@ abstract class ActivityBase extends AppCompatActivity implements SharedPreferenc
File file = new File(dir, fname);
Log.i("Copying shared file to " + file);
Helper.copy(getContentResolver().openInputStream(uri), new FileOutputStream(file));
InputStream is = getContentResolver().openInputStream(uri);
if (is == null)
throw new FileNotFoundException(uri.toString());
Helper.copy(is, new FileOutputStream(file));
return FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID, file);
} catch (Throwable ex) {

View File

@ -32,6 +32,7 @@ import android.widget.TextView;
import androidx.constraintlayout.widget.Group;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
@ -96,6 +97,9 @@ public class ActivityDSN extends ActivityBase {
ContentResolver resolver = context.getContentResolver();
try (InputStream is = resolver.openInputStream(uri)) {
if (is == null)
throw new FileNotFoundException(uri.toString());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = new byte[Helper.BUFFER_SIZE];
int length;

View File

@ -42,6 +42,7 @@ import androidx.constraintlayout.widget.Group;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.StringReader;
import java.net.InetAddress;
@ -120,6 +121,8 @@ public class ActivityDmarc extends ActivityBase {
String data;
ContentResolver resolver = context.getContentResolver();
try (InputStream is = resolver.openInputStream(uri)) {
if (is == null)
throw new FileNotFoundException(uri.toString());
data = Helper.readStream(is);
}

View File

@ -266,6 +266,8 @@ public class ActivityEML extends ActivityBase {
ContentResolver resolver = context.getContentResolver();
try (InputStream is = resolver.openInputStream(uri)) {
if (is == null)
throw new FileNotFoundException(uri.toString());
Properties props = MessageHelper.getSessionProperties();
Session isession = Session.getInstance(props, null);
@ -534,6 +536,9 @@ public class ActivityEML extends ActivityBase {
os = getContentResolver().openOutputStream(uri);
is = apart.part.getInputStream();
if (os == null)
throw new FileNotFoundException(uri.toString());
byte[] buffer = new byte[Helper.BUFFER_SIZE];
int read;
while ((read = is.read(buffer)) != -1)
@ -631,6 +636,8 @@ public class ActivityEML extends ActivityBase {
ContentResolver resolver = context.getContentResolver();
try (InputStream is = resolver.openInputStream(uri)) {
if (is == null)
throw new FileNotFoundException(uri.toString());
Properties props = MessageHelper.getSessionProperties();
Session isession = Session.getInstance(props, null);

View File

@ -732,6 +732,8 @@ public class ActivitySetup extends ActivityBase implements FragmentManager.OnBac
DocumentFile file = DocumentFile.fromSingleUri(context, uri);
try (OutputStream raw = resolver.openOutputStream(uri)) {
Log.i("Writing URI=" + uri + " name=" + file.getName() + " virtual=" + file.isVirtual());
if (raw == null)
throw new FileNotFoundException(uri.toString());
if (TextUtils.isEmpty(password))
raw.write(jexport.toString(2).getBytes());
@ -857,8 +859,10 @@ public class ActivitySetup extends ActivityBase implements FragmentManager.OnBac
StringBuilder data = new StringBuilder();
Log.i("Reading URI=" + uri);
ContentResolver resolver = context.getContentResolver();
try (InputStream raw = new BufferedInputStream(resolver.openInputStream(uri))) {
InputStream is = resolver.openInputStream(uri);
if (is == null)
throw new FileNotFoundException(uri.toString());
try (InputStream raw = new BufferedInputStream(is)) {
InputStream in;
if (TextUtils.isEmpty(password))
in = raw;
@ -1284,10 +1288,13 @@ public class ActivitySetup extends ActivityBase implements FragmentManager.OnBac
DB db = DB.getInstance(context);
ContentResolver resolver = context.getContentResolver();
try (InputStream is = new BufferedInputStream(resolver.openInputStream(uri))) {
InputStream is = resolver.openInputStream(uri);
if (is == null)
throw new FileNotFoundException(uri.toString());
try (InputStream bis = new BufferedInputStream(is)) {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser xml = factory.newPullParser();
xml.setInput(new InputStreamReader(is));
xml.setInput(new InputStreamReader(bis));
EntityAccount account = null;
EntityIdentity identity = null;
@ -1504,6 +1511,9 @@ public class ActivitySetup extends ActivityBase implements FragmentManager.OnBac
X509Certificate cert;
CertificateFactory fact = CertificateFactory.getInstance("X.509");
try (InputStream is = context.getContentResolver().openInputStream(uri)) {
if (is == null)
throw new FileNotFoundException(uri.toString());
if (der)
cert = (X509Certificate) fact.generateCertificate(is);
else {
@ -1560,7 +1570,10 @@ public class ActivitySetup extends ActivityBase implements FragmentManager.OnBac
Log.i("Reading URI=" + uri);
ContentResolver resolver = context.getContentResolver();
EmailProvider.importProfiles(resolver.openInputStream(uri), context);
InputStream is = resolver.openInputStream(uri);
if (is == null)
throw new FileNotFoundException(uri.toString());
EmailProvider.importProfiles(is, context);
return null;
}

View File

@ -157,6 +157,7 @@ import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
@ -3595,6 +3596,8 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
try {
is = new FileInputStream(file);
os = resolver.openOutputStream(uri);
if (os == null)
throw new FileNotFoundException(uri.toString());
Helper.copy(is, os);
} finally {
try {

View File

@ -506,6 +506,9 @@ public class FragmentBase extends Fragment {
os = context.getContentResolver().openOutputStream(uri);
is = new FileInputStream(file);
if (os == null)
throw new FileNotFoundException(uri.toString());
byte[] buffer = new byte[Helper.BUFFER_SIZE];
int read;
while ((read = is.read(buffer)) != -1)
@ -592,6 +595,9 @@ public class FragmentBase extends Fragment {
os = context.getContentResolver().openOutputStream(document.getUri());
is = new FileInputStream(file);
if (os == null)
throw new FileNotFoundException(uri.toString());
byte[] buffer = new byte[Helper.BUFFER_SIZE];
int read;
while ((read = is.read(buffer)) != -1)

View File

@ -4084,7 +4084,7 @@ public class FragmentCompose extends FragmentBase {
os = new FileOutputStream(file);
if (is == null)
throw new IOException("Content provider crashed");
throw new FileNotFoundException(uri.toString());
byte[] buffer = new byte[Helper.BUFFER_SIZE];
for (int len = is.read(buffer); len != -1; len = is.read(buffer)) {

View File

@ -415,8 +415,11 @@ public class FragmentContacts extends FragmentBase {
int count = 0;
ContentResolver resolver = context.getContentResolver();
try (InputStream is = new BufferedInputStream(resolver.openInputStream(uri))) {
VCardReader reader = new VCardReader(is);
InputStream is = resolver.openInputStream(uri);
if (is == null)
throw new FileNotFoundException(uri.toString());
try (InputStream bis = new BufferedInputStream(is)) {
VCardReader reader = new VCardReader(bis);
VCard vcard;
while ((vcard = reader.readNext()) != null) {
Integer type = null;
@ -534,6 +537,8 @@ public class FragmentContacts extends FragmentBase {
ContentResolver resolver = context.getContentResolver();
try (OutputStream os = resolver.openOutputStream(uri)) {
if (os == null)
throw new FileNotFoundException(uri.toString());
try (VCardWriter writer = new VCardWriter(os, VCardVersion.V3_0)) {
for (VCard vcard : vcards)
writer.write(vcard);

View File

@ -76,6 +76,7 @@ import org.json.JSONObject;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
@ -1191,7 +1192,10 @@ public class FragmentFolders extends FragmentBase {
// http://qmail.org./man/man5/mbox.html
long last = new Date().getTime();
ContentResolver resolver = context.getContentResolver();
try (OutputStream out = new BufferedOutputStream(resolver.openOutputStream(uri))) {
OutputStream os = resolver.openOutputStream(uri);
if (os == null)
throw new FileNotFoundException(uri.toString());
try (OutputStream out = new BufferedOutputStream(os)) {
for (int i = 0; i < ids.size(); i++)
try {
long now = new Date().getTime();

View File

@ -7288,6 +7288,9 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
os = context.getContentResolver().openOutputStream(uri);
is = new FileInputStream(file);
if (os == null)
throw new FileNotFoundException(uri.toString());
byte[] buffer = new byte[Helper.BUFFER_SIZE];
int read;
while ((read = is.read(buffer)) != -1)

View File

@ -519,6 +519,8 @@ public class FragmentOptionsEncryption extends FragmentBase implements SharedPre
throw new FileNotFoundException();
try (InputStream is = context.getContentResolver().openInputStream(uri)) {
if (is == null)
throw new FileNotFoundException(uri.toString());
return Helper.readBytes(is);
}
}

View File

@ -353,6 +353,8 @@ public class FragmentRules extends FragmentBase {
ContentResolver resolver = context.getContentResolver();
try (OutputStream os = resolver.openOutputStream(uri)) {
Log.i("Writing URI=" + uri);
if (os == null)
throw new FileNotFoundException(uri.toString());
os.write(jrules.toString(2).getBytes());
}
@ -398,6 +400,8 @@ public class FragmentRules extends FragmentBase {
Log.i("Reading URI=" + uri);
ContentResolver resolver = context.getContentResolver();
try (InputStream is = resolver.openInputStream(uri)) {
if (is == null)
throw new FileNotFoundException(uri.toString());
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = reader.readLine()) != null)

View File

@ -1917,6 +1917,8 @@ public class Helper {
static long copy(Context context, Uri uri, File file) throws IOException {
try (InputStream is = context.getContentResolver().openInputStream(uri)) {
if (is == null)
throw new FileNotFoundException(uri.toString());
try (OutputStream os = new FileOutputStream(file)) {
return copy(is, os);
}

View File

@ -407,6 +407,8 @@ class ImageHelper {
Bitmap bm;
int scaleToPixels = res.getDisplayMetrics().widthPixels;
try (InputStream is = context.getContentResolver().openInputStream(uri)) {
if (is == null)
throw new FileNotFoundException(uri.toString());
bm = getScaledBitmap(is, a.source, null, scaleToPixels);
if (bm == null)
throw new FileNotFoundException(a.source);