1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2024-12-23 08:30:44 +00:00

Merge pull request #8648 from Isira-Seneviratne/Use_IO_extensions

Use IO extensions.
This commit is contained in:
Stypox 2022-07-22 18:30:51 +02:00 committed by GitHub
commit 3281ed2ef1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 39 deletions

View File

@ -12,10 +12,7 @@ import org.schabi.newpipe.R
import org.schabi.newpipe.util.Localization import org.schabi.newpipe.util.Localization
import org.schabi.newpipe.util.ThemeHelper import org.schabi.newpipe.util.ThemeHelper
import org.schabi.newpipe.util.external_communication.ShareUtils import org.schabi.newpipe.util.external_communication.ShareUtils
import java.io.BufferedReader
import java.io.IOException import java.io.IOException
import java.io.InputStreamReader
import java.nio.charset.StandardCharsets
object LicenseFragmentHelper { object LicenseFragmentHelper {
/** /**
@ -25,32 +22,13 @@ object LicenseFragmentHelper {
* styled according to the context's theme * styled according to the context's theme
*/ */
private fun getFormattedLicense(context: Context, license: License): String { private fun getFormattedLicense(context: Context, license: License): String {
val licenseContent = StringBuilder()
val webViewData: String
try { try {
BufferedReader( return context.assets.open(license.filename).bufferedReader().use { it.readText() }
InputStreamReader(
context.assets.open(license.filename),
StandardCharsets.UTF_8
)
).use { `in` ->
var str: String?
while (`in`.readLine().also { str = it } != null) {
licenseContent.append(str)
}
// split the HTML file and insert the stylesheet into the HEAD of the file // split the HTML file and insert the stylesheet into the HEAD of the file
webViewData = "$licenseContent".replace( .replace("</head>", "<style>${getLicenseStylesheet(context)}</style></head>")
"</head>",
"<style>" + getLicenseStylesheet(context) + "</style></head>"
)
}
} catch (e: IOException) { } catch (e: IOException) {
throw IllegalArgumentException( throw IllegalArgumentException("Could not get license file: ${license.filename}", e)
"Could not get license file: " + license.filename, e
)
} }
return webViewData
} }
/** /**
@ -118,9 +96,7 @@ object LicenseFragmentHelper {
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe { formattedLicense -> .subscribe { formattedLicense ->
val webViewData = Base64.encodeToString( val webViewData = Base64.encodeToString(formattedLicense.toByteArray(), Base64.NO_PADDING)
formattedLicense.toByteArray(StandardCharsets.UTF_8), Base64.NO_PADDING
)
val webView = WebView(context) val webView = WebView(context)
webView.loadData(webViewData, "text/html; charset=UTF-8", "base64") webView.loadData(webViewData, "text/html; charset=UTF-8", "base64")

View File

@ -5,9 +5,6 @@ import android.util.Log
import org.schabi.newpipe.streams.io.SharpOutputStream import org.schabi.newpipe.streams.io.SharpOutputStream
import org.schabi.newpipe.streams.io.StoredFileHelper import org.schabi.newpipe.streams.io.StoredFileHelper
import org.schabi.newpipe.util.ZipHelper import org.schabi.newpipe.util.ZipHelper
import java.io.BufferedOutputStream
import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.IOException import java.io.IOException
import java.io.ObjectInputStream import java.io.ObjectInputStream
import java.io.ObjectOutputStream import java.io.ObjectOutputStream
@ -25,12 +22,12 @@ class ContentSettingsManager(private val fileLocator: NewPipeFileLocator) {
@Throws(Exception::class) @Throws(Exception::class)
fun exportDatabase(preferences: SharedPreferences, file: StoredFileHelper) { fun exportDatabase(preferences: SharedPreferences, file: StoredFileHelper) {
file.create() file.create()
ZipOutputStream(BufferedOutputStream(SharpOutputStream(file.stream))) ZipOutputStream(SharpOutputStream(file.stream).buffered())
.use { outZip -> .use { outZip ->
ZipHelper.addFileToZip(outZip, fileLocator.db.path, "newpipe.db") ZipHelper.addFileToZip(outZip, fileLocator.db.path, "newpipe.db")
try { try {
ObjectOutputStream(FileOutputStream(fileLocator.settings)).use { output -> ObjectOutputStream(fileLocator.settings.outputStream()).use { output ->
output.writeObject(preferences.all) output.writeObject(preferences.all)
output.flush() output.flush()
} }
@ -74,7 +71,7 @@ class ContentSettingsManager(private val fileLocator: NewPipeFileLocator) {
try { try {
val preferenceEditor = preferences.edit() val preferenceEditor = preferences.edit()
ObjectInputStream(FileInputStream(fileLocator.settings)).use { input -> ObjectInputStream(fileLocator.settings.inputStream()).use { input ->
preferenceEditor.clear() preferenceEditor.clear()
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
val entries = input.readObject() as Map<String, *> val entries = input.readObject() as Map<String, *>

View File

@ -7,8 +7,6 @@ import org.schabi.newpipe.App
import org.schabi.newpipe.error.ErrorInfo import org.schabi.newpipe.error.ErrorInfo
import org.schabi.newpipe.error.ErrorUtil.Companion.createNotification import org.schabi.newpipe.error.ErrorUtil.Companion.createNotification
import org.schabi.newpipe.error.UserAction import org.schabi.newpipe.error.UserAction
import java.io.ByteArrayInputStream
import java.io.InputStream
import java.security.MessageDigest import java.security.MessageDigest
import java.security.NoSuchAlgorithmException import java.security.NoSuchAlgorithmException
import java.security.cert.CertificateEncodingException import java.security.cert.CertificateEncodingException
@ -47,10 +45,8 @@ object ReleaseVersionUtil {
return "" return ""
} }
val x509cert = try { val x509cert = try {
val cert = signatures[0].toByteArray()
val input: InputStream = ByteArrayInputStream(cert)
val cf = CertificateFactory.getInstance("X509") val cf = CertificateFactory.getInstance("X509")
cf.generateCertificate(input) as X509Certificate cf.generateCertificate(signatures[0].toByteArray().inputStream()) as X509Certificate
} catch (e: CertificateException) { } catch (e: CertificateException) {
showRequestError(app, e, "Certificate error") showRequestError(app, e, "Certificate error")
return "" return ""