From 6fbfb133394f685b96813f480ecd8eec1f252b07 Mon Sep 17 00:00:00 2001 From: Mariotaku Lee Date: Fri, 25 Aug 2017 15:58:58 +0800 Subject: [PATCH] fixed temp file stream --- .../twidere/activity/ComposeActivityTest.kt | 9 +++- .../twidere/extension/FileExtensionsTest.kt | 46 +++++++++++++++++ .../twidere/util/IOFunctionsKtTest.kt | 27 ---------- .../twidere/activity/ComposeActivity.kt | 4 -- .../twidere/extension/FileExtensions.kt | 49 +++++++++++++++++++ .../twidere/extension/ReflectionExtensions.kt | 10 ++++ .../org/mariotaku/twidere/util/IOFunctions.kt | 27 ---------- 7 files changed, 112 insertions(+), 60 deletions(-) create mode 100644 twidere/src/androidTest/kotlin/org/mariotaku/twidere/extension/FileExtensionsTest.kt delete mode 100644 twidere/src/androidTest/kotlin/org/mariotaku/twidere/util/IOFunctionsKtTest.kt create mode 100644 twidere/src/main/kotlin/org/mariotaku/twidere/extension/FileExtensions.kt delete mode 100644 twidere/src/main/kotlin/org/mariotaku/twidere/util/IOFunctions.kt diff --git a/twidere/src/androidTest/kotlin/org/mariotaku/twidere/activity/ComposeActivityTest.kt b/twidere/src/androidTest/kotlin/org/mariotaku/twidere/activity/ComposeActivityTest.kt index b7bd9287b..0b08d957e 100644 --- a/twidere/src/androidTest/kotlin/org/mariotaku/twidere/activity/ComposeActivityTest.kt +++ b/twidere/src/androidTest/kotlin/org/mariotaku/twidere/activity/ComposeActivityTest.kt @@ -20,7 +20,6 @@ package org.mariotaku.twidere.activity import android.annotation.SuppressLint -import android.app.Activity import android.content.Intent import android.support.test.InstrumentationRegistry import android.support.test.runner.AndroidJUnit4 @@ -30,6 +29,7 @@ import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith import org.mariotaku.twidere.constant.IntentConstants.* +import org.mariotaku.twidere.extension.set import org.mariotaku.twidere.model.ParcelableStatus import org.mariotaku.twidere.model.ParcelableStatusUpdate import org.mariotaku.twidere.test.R @@ -167,7 +167,12 @@ class ComposeActivityTest { activity.finish() } - private fun Activity.getStatusUpdateTest(checkLength: Boolean): ParcelableStatusUpdate { + private fun ComposeActivity.requestSkipDraft() { + val shouldSkipDraft = javaClass.getDeclaredField("shouldSkipDraft") + this[shouldSkipDraft] = true + } + + private fun ComposeActivity.getStatusUpdateTest(checkLength: Boolean): ParcelableStatusUpdate { val getStatusUpdate = javaClass.getDeclaredMethod("getStatusUpdate", kotlin.Boolean::class.java).apply { isAccessible = true diff --git a/twidere/src/androidTest/kotlin/org/mariotaku/twidere/extension/FileExtensionsTest.kt b/twidere/src/androidTest/kotlin/org/mariotaku/twidere/extension/FileExtensionsTest.kt new file mode 100644 index 000000000..740d5d97f --- /dev/null +++ b/twidere/src/androidTest/kotlin/org/mariotaku/twidere/extension/FileExtensionsTest.kt @@ -0,0 +1,46 @@ +/* + * Twidere - Twitter client for Android + * + * Copyright (C) 2012-2017 Mariotaku Lee + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.mariotaku.twidere.extension + +import android.support.test.InstrumentationRegistry +import android.support.test.runner.AndroidJUnit4 +import org.junit.Assert +import org.junit.Test +import org.junit.runner.RunWith +import java.util.* + +/** + * Created by mariotaku on 2017/1/24. + */ +@RunWith(AndroidJUnit4::class) +class FileExtensionsTest { + @Test + fun testTempFileInputStream() { + val context = InstrumentationRegistry.getTargetContext() + val random = Random() + val testData = ByteArray(1024) + random.nextBytes(testData) + val compareData = context.cacheDir.tempInputStream { os -> + os.write(testData) + }.use { it.readBytes(1024) } + Assert.assertArrayEquals(testData, compareData) + } + +} \ No newline at end of file diff --git a/twidere/src/androidTest/kotlin/org/mariotaku/twidere/util/IOFunctionsKtTest.kt b/twidere/src/androidTest/kotlin/org/mariotaku/twidere/util/IOFunctionsKtTest.kt deleted file mode 100644 index 9a555440d..000000000 --- a/twidere/src/androidTest/kotlin/org/mariotaku/twidere/util/IOFunctionsKtTest.kt +++ /dev/null @@ -1,27 +0,0 @@ -package org.mariotaku.twidere.util - -import android.support.test.InstrumentationRegistry -import android.support.test.runner.AndroidJUnit4 -import org.junit.Assert -import org.junit.Test -import org.junit.runner.RunWith -import java.util.* - -/** - * Created by mariotaku on 2017/1/24. - */ -@RunWith(AndroidJUnit4::class) -class IOFunctionsKtTest { - @Test - fun testTempFileInputStream() { - val context = InstrumentationRegistry.getTargetContext() - val random = Random() - val testData = ByteArray(1024) - random.nextBytes(testData) - val compareData = tempFileInputStream(context) { os -> - os.write(testData) - }.readBytes(1024) - Assert.assertArrayEquals(testData, compareData) - } - -} \ No newline at end of file diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/activity/ComposeActivity.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/activity/ComposeActivity.kt index f35aaf7b7..239374610 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/activity/ComposeActivity.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/activity/ComposeActivity.kt @@ -756,10 +756,6 @@ class ComposeActivity : BaseActivity(), OnMenuItemClickListener, OnClickListener mediaPreviewAdapter.setAltText(position, altText) } - internal fun requestSkipDraft() { - shouldSkipDraft = true - } - private fun locationMenuItemSelected(item: MenuItem) { item.isChecked = true var attachLocationChecked = false diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/extension/FileExtensions.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/extension/FileExtensions.kt new file mode 100644 index 000000000..3ad20d534 --- /dev/null +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/extension/FileExtensions.kt @@ -0,0 +1,49 @@ +/* + * Twidere - Twitter client for Android + * + * Copyright (C) 2012-2017 Mariotaku Lee + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.mariotaku.twidere.extension + +import org.mariotaku.twidere.util.DebugLog +import java.io.File +import java.io.FileInputStream +import java.io.InputStream +import java.io.OutputStream + +fun File.tempInputStream(write: (OutputStream) -> Unit): InputStream { + val file = File.createTempFile("twidere__temp_is_file", ".tmp", this) + file.outputStream().use { write(it) } + return TempFileInputStream(file) +} + +internal class TempFileInputStream(val file: File) : FileInputStream(file) { + override fun close() { + try { + super.close() + } finally { + file.delete() + } + } + + override fun finalize() { + if (file.exists()) { + DebugLog.w(msg = "Stream not properly closed, ${file.absolutePath} not deleted") + } + super.finalize() + } +} \ No newline at end of file diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/extension/ReflectionExtensions.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/extension/ReflectionExtensions.kt index d31a264a2..2bd369650 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/extension/ReflectionExtensions.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/extension/ReflectionExtensions.kt @@ -33,4 +33,14 @@ operator fun Any.get(field: Field): Any? { } finally { field.isAccessible = accessible } +} + +operator fun Any.set(field: Field, any: Any?) { + val accessible = field.isAccessible + try { + field.isAccessible = true + field[this] = any + } finally { + field.isAccessible = accessible + } } \ No newline at end of file diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/util/IOFunctions.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/util/IOFunctions.kt deleted file mode 100644 index 50113281e..000000000 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/util/IOFunctions.kt +++ /dev/null @@ -1,27 +0,0 @@ -package org.mariotaku.twidere.util - -import android.content.Context -import java.io.File -import java.io.FileInputStream -import java.io.InputStream -import java.io.OutputStream - -/** - * Created by mariotaku on 2017/1/24. - */ - -fun tempFileInputStream(context: Context, write: (OutputStream) -> Unit): InputStream { - val file = File.createTempFile("twidere__temp_is_file", ".tmp", context.cacheDir) - file.outputStream().use { write(it) } - return TempFileInputStream(file) -} - -internal class TempFileInputStream(val file: File) : FileInputStream(file) { - override fun close() { - try { - super.close() - } finally { - file.delete() - } - } -} \ No newline at end of file