Skip to content

Commit

Permalink
[Refactor] Move Path classes to libcore:io module
Browse files Browse the repository at this point in the history
Signed-off-by: Muntashir Al-Islam <[email protected]>
  • Loading branch information
MuntashirAkon committed Nov 12, 2023
1 parent 5a78375 commit 702ade2
Show file tree
Hide file tree
Showing 45 changed files with 1,036 additions and 806 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ public void extractObb(Path writableObbDir) throws IOException {
// Extract obb file to the destination directory
try (InputStream zipInputStream = mZipFile.getInputStream(obbEntry);
OutputStream outputStream = obbDir.openOutputStream()) {
IoUtils.copy(zipInputStream, outputStream, -1, null);
IoUtils.copy(zipInputStream, outputStream);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public static void backupApk(@NonNull Context ctx, @NonNull String packageName,
} else {
// Regular apk
apkFile = backupPath.createNewFile(outputName + EXT_APK, null);
IoUtils.copy(Paths.get(info.publicSourceDir), apkFile, null);
IoUtils.copy(Paths.get(info.publicSourceDir), apkFile);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import io.github.muntashirakon.AppManager.users.Users;
import io.github.muntashirakon.AppManager.utils.BroadcastUtils;
import io.github.muntashirakon.AppManager.utils.ContextUtils;
import io.github.muntashirakon.AppManager.utils.FileUtils;
import io.github.muntashirakon.AppManager.utils.MiuiUtils;
import io.github.muntashirakon.AppManager.utils.ThreadUtils;
import io.github.muntashirakon.AppManager.utils.UIUtils;
Expand Down Expand Up @@ -595,7 +596,7 @@ public boolean install(@NonNull ApkFile apkFile, @NonNull List<String> selectedS
long entrySize = entry.getFileSize(options.isSignApkFiles());
try (InputStream apkInputStream = entry.getInputStream(options.isSignApkFiles());
OutputStream apkOutputStream = mSession.openWrite(entry.getFileName(), 0, entrySize)) {
IoUtils.copy(apkInputStream, apkOutputStream, totalSize, progressHandler);
FileUtils.copy(apkInputStream, apkOutputStream, totalSize, progressHandler);
mSession.fsync(apkOutputStream);
Log.d(TAG, "Install: copied entry %s", entry.name);
} catch (IOException e) {
Expand Down Expand Up @@ -655,7 +656,7 @@ public boolean install(@NonNull Path[] apkFiles, @NonNull String packageName, @N
for (Path apkFile : apkFiles) {
try (InputStream apkInputStream = apkFile.openInputStream();
OutputStream apkOutputStream = mSession.openWrite(apkFile.getName(), 0, apkFile.length())) {
IoUtils.copy(apkInputStream, apkOutputStream, totalSize, progressHandler);
FileUtils.copy(apkInputStream, apkOutputStream, totalSize, progressHandler);
mSession.fsync(apkOutputStream);
} catch (IOException e) {
callFinish(STATUS_FAILURE_SESSION_WRITE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ static void addFile(@NonNull ZipOutputStream zipOutputStream, @NonNull Path file
zipEntry.setTime(timestamp);
zipOutputStream.putNextEntry(zipEntry);
try (InputStream apkInputStream = filePath.openInputStream()) {
IoUtils.copy(apkInputStream, zipOutputStream, -1, null);
IoUtils.copy(apkInputStream, zipOutputStream);
}
zipOutputStream.closeEntry();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ private void backupKeyStore() throws BackupException { // Called only when the
try {
String newFileName = Utils.replaceOnce(keyStoreFileName, String.valueOf(mApplicationInfo.uid),
String.valueOf(KEYSTORE_PLACEHOLDER));
IoUtils.copy(keyStorePath.findFile(keyStoreFileName), cachePath.findOrCreateFile(newFileName, null), null);
IoUtils.copy(keyStorePath.findFile(keyStoreFileName), cachePath.findOrCreateFile(newFileName, null));
cachedKeyStoreFileNames.add(newFileName);
keyStoreFilters.add(Pattern.quote(newFileName));
} catch (Throwable e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static void fromTar(@NonNull Path tarSource, @NonNull Path abDest, @Nulla
OutputStream os = abDest.openOutputStream();
try (InputStream is = tarSource.openInputStream();
OutputStream realOs = header.write(os)) {
IoUtils.copy(is, realOs, -1, null);
IoUtils.copy(is, realOs);
} catch (IOException e) {
throw e;
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static void toTar(@NonNull Path abSource, @NonNull Path tarDest, @Nullabl
InputStream is = abSource.openInputStream();
try (OutputStream os = tarDest.openOutputStream();
InputStream realIs = header.read(is)) {
IoUtils.copy(realIs, os, -1, null);
IoUtils.copy(realIs, os);
} catch (Exception e) {
ExUtils.rethrowAsIOException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ private void backupData() throws BackupException {
// We need to use a temporary file
tmpFile = FileCache.getGlobalFileCache().createCachedFile(files[0].getExtension());
try (OutputStream fos = new FileOutputStream(tmpFile)) {
IoUtils.copy(zis, fos, -1, null);
IoUtils.copy(zis, fos);
}
}
String fileName = zipEntry.getName().replaceFirst(Pattern.quote(mPackageName + "/"), "");
Expand All @@ -389,7 +389,7 @@ private void backupData() throws BackupException {
if (tmpFile != null) {
// Copy from the temporary file
try (FileInputStream fis = new FileInputStream(tmpFile)) {
IoUtils.copy(fis, tos, -1, null);
IoUtils.copy(fis, tos);
} finally {
FileCache.getGlobalFileCache().delete(tmpFile);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ private void backupData() throws BackupException {
// We need to use a temporary file
tmpFile = FileCache.getGlobalFileCache().createCachedFile(dataFile.getExtension());
try (OutputStream fos = new FileOutputStream(tmpFile)) {
IoUtils.copy(zis, fos, -1, null);
IoUtils.copy(zis, fos);
}
}
String fileName = zipEntry.getName().replaceFirst(Pattern.quote(mPackageName + "/"), "");
Expand All @@ -283,7 +283,7 @@ private void backupData() throws BackupException {
if (tmpFile != null) {
// Copy from the temporary file
try (FileInputStream fis = new FileInputStream(tmpFile)) {
IoUtils.copy(fis, tos, -1, null);
IoUtils.copy(fis, tos);
} finally {
FileCache.getGlobalFileCache().delete(tmpFile);
}
Expand All @@ -308,7 +308,7 @@ private void generateMetadata() throws BackupException {
mCachedApk = FileUtils.getTempPath(mPackageName, "base.apk");
try (InputStream pis = getApkFile().openInputStream()) {
try (OutputStream fos = mCachedApk.openOutputStream()) {
IoUtils.copy(pis, fos, -1, null);
IoUtils.copy(pis, fos);
}
mFilesToBeDeleted.add(getApkFile());
} catch (IOException e) {
Expand Down Expand Up @@ -417,7 +417,7 @@ private String[] cacheAndGetSplitConfigs() throws IOException, RemoteException {
splits.add(splitName);
Path file = mCachedApk.requireParent().findOrCreateFile(splitName, null);
try (OutputStream fos = file.openOutputStream()) {
IoUtils.copy(zis, fos, -1, null);
IoUtils.copy(zis, fos);
} catch (IOException e) {
file.delete();
throw e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ private void backupApkFile() throws BackupException {
}
try (OutputStream fos = baseApkFile.openOutputStream()) {
// The whole file is the APK
IoUtils.copy(is, fos, -1, null);
IoUtils.copy(is, fos);
} finally {
is.close();
}
Expand Down Expand Up @@ -356,11 +356,11 @@ private void backupData() throws BackupException {
if (!inTarEntry.isDirectory() && !inTarEntry.isSymbolicLink()) {
if (isExternal) {
if (extTos != null) {
IoUtils.copy(tis, extTos, -1, null);
IoUtils.copy(tis, extTos);
}
} else {
if (intTos != null) {
IoUtils.copy(tis, intTos, -1, null);
IoUtils.copy(tis, intTos);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public void encrypt(@NonNull InputStream unencryptedStream, @NonNull OutputStrea
cipher.init(true, getParams());
// Convert unencrypted stream to encrypted stream
try (OutputStream cipherOS = new CipherOutputStream(encryptedStream, cipher)) {
IoUtils.copy(unencryptedStream, cipherOS, -1, null);
IoUtils.copy(unencryptedStream, cipherOS);
}
}

Expand All @@ -152,7 +152,7 @@ public void decrypt(@NonNull InputStream encryptedStream, @NonNull OutputStream
cipher.init(false, getParams());
// Convert encrypted stream to unencrypted stream
try (InputStream cipherIS = new CipherInputStream(encryptedStream, cipher)) {
IoUtils.copy(cipherIS, unencryptedStream, -1, null);
IoUtils.copy(cipherIS, unencryptedStream);
}
}

Expand Down Expand Up @@ -186,11 +186,11 @@ private void handleFiles(boolean forEncryption, @NonNull Path[] files) throws IO
OutputStream os = outputPath.openOutputStream()) {
if (forEncryption) {
try (OutputStream cipherOS = new CipherOutputStream(os, cipher)) {
IoUtils.copy(is, cipherOS, -1, null);
IoUtils.copy(is, cipherOS);
}
} else { // Cipher.DECRYPT_MODE
try (InputStream cipherIS = new CipherInputStream(is, cipher)) {
IoUtils.copy(cipherIS, os, -1, null);
IoUtils.copy(cipherIS, os);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ private void loadFiles(@NonNull Uri uri, @Nullable String scrollToFilename) {
}
Uri documentUri = DocumentsContract.buildDocumentUriUsingTree(mCurrentUri, documentId);
Path child = Paths.getTreeDocument(path, documentUri);
PathAttributes attributes = PathAttributes.fromSafTreeCursor(documentUri, c);
PathAttributes attributes = Paths.getAttributesFromSafTreeCursor(documentUri, c);
FmItem fmItem = new FmItem(child, attributes);
mFmItems.add(fmItem);
if (fmItem.isDirectory) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ public void saveLogs(@NonNull Path path, @NonNull SendLogDetails sendLogDetails)
}
try (OutputStream output = path.openOutputStream()) {
try (InputStream input = sendLogDetails.getAttachment().openInputStream()) {
IoUtils.copy(input, output, -1, null);
IoUtils.copy(input, output);
}
mLogSavedLiveData.postValue(path);
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public static void addMultipartFormData(@NonNull OutputStream os, @NonNull Strin
.getBytes(StandardCharsets.UTF_8));
os.write(("Content-Type: application/octet-stream\r\n").getBytes(StandardCharsets.UTF_8));
os.write(("Content-Transfer-Encoding: chunked\r\n\r\n").getBytes(StandardCharsets.UTF_8));
IoUtils.copy(is, os, -1, null);
IoUtils.copy(is, os);
}

@WorkerThread
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public File getCachedFile(@NonNull Path source) throws IOException {
} else if (source.lastModified() > 0 && source.lastModified() < tempFile.lastModified()) {
return tempFile;
}
IoUtils.copy(source, Paths.get(tempFile), null);
IoUtils.copy(source, Paths.get(tempFile));
return tempFile;
}

Expand All @@ -94,7 +94,7 @@ public File getCachedFile(@NonNull InputStream is, @Nullable String extension) t
File tempFile = File.createTempFile("file_", "." + (extension != null ? extension : "tmp"), mCacheDir);
mFileCache.add(tempFile);
try (OutputStream os = new FileOutputStream(tempFile)) {
IoUtils.copy(is, os, -1, null);
IoUtils.copy(is, os);
}
return tempFile;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public ImageFileCache() {
public void putImage(@NonNull String name, @NonNull InputStream inputStream) throws IOException {
File iconFile = getImageFile(name);
try (OutputStream os = new FileOutputStream(iconFile)) {
IoUtils.copy(inputStream, os, -1, null);
IoUtils.copy(inputStream, os);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class ImportExportKeyStoreDialogFragment extends DialogFragment {
try (InputStream is = new FileInputStream(AM_KEYSTORE_FILE);
OutputStream os = mActivity.getContentResolver().openOutputStream(uri)) {
if (os == null) throw new IOException("Unable to open URI");
IoUtils.copy(is, os, -1, null);
IoUtils.copy(is, os);
mActivity.runOnUiThread(() -> {
UIUtils.displayShortToast(R.string.done);
dismiss();
Expand Down Expand Up @@ -77,7 +77,7 @@ public class ImportExportKeyStoreDialogFragment extends DialogFragment {
try (InputStream is = mActivity.getContentResolver().openInputStream(uri);
OutputStream os = new FileOutputStream(AM_KEYSTORE_FILE)) {
if (is == null) throw new IOException("Unable to open URI");
IoUtils.copy(is, os, -1, null);
IoUtils.copy(is, os);
if (KeyStoreManager.hasKeyStorePassword()) {
CountDownLatch waitForKs = new CountDownLatch(1);
KeyStoreManager.inputKeyStorePassword(mActivity, waitForKs::countDown);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -877,7 +877,7 @@ private void readStateSyncLocked() throws IllegalStateException {
if (parseStateFromXmlStreamLocked(in)) {
// Parsed state from fallback file. Restore original file with fallback file
try {
IoUtils.copy(statePersistFallbackFile, mStatePersistFile, null);
IoUtils.copy(statePersistFallbackFile, mStatePersistFile);
} catch (IOException ignored) {
// Failed to copy, but it's okay because we already parsed states from fallback file
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -889,7 +889,7 @@ private void readStateSyncLocked() throws IllegalStateException {
if (parseStateFromXmlStreamLocked(in)) {
// Parsed state from fallback file. Restore original file with fallback file
try {
IoUtils.copy(statePersistFallbackFile, mStatePersistFile, null);
IoUtils.copy(statePersistFallbackFile, mStatePersistFile);
} catch (IOException ignored) {
// Failed to copy, but it's okay because we already parsed states from fallback file
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.zip.ZipEntry;

import io.github.muntashirakon.AppManager.logs.Log;
import io.github.muntashirakon.AppManager.progress.ProgressHandler;
import io.github.muntashirakon.AppManager.self.filecache.FileCache;
import io.github.muntashirakon.io.FileSystemManager;
import io.github.muntashirakon.io.IoUtils;
Expand Down Expand Up @@ -113,12 +114,37 @@ public static boolean isAssetDirectory(@NonNull Context context, @NonNull String
return files != null && files.length > 0;
}

@AnyThread
public static long copy(@NonNull Path from, @NonNull Path to, @Nullable ProgressHandler progressHandler)
throws IOException {
try (InputStream in = from.openInputStream();
OutputStream out = to.openOutputStream()) {
return copy(in, out, from.length(), progressHandler);
}
}

/**
* Copy the contents of one stream to another.
*
* @param totalSize Total size of the stream. Only used for handling progress. Set {@code -1} if unknown.
*/
@AnyThread
public static long copy(@NonNull InputStream in, @NonNull OutputStream out, long totalSize,
@Nullable ProgressHandler progressHandler) throws IOException {
float lastProgress = progressHandler != null ? progressHandler.getLastProgress() : 0;
return IoUtils.copy(in, out, ThreadUtils.getBackgroundThreadExecutor(), progress -> {
if (progressHandler != null) {
progressHandler.postUpdate(100, lastProgress + (progress * 100f / totalSize));
}
});
}

@WorkerThread
public static void copyFromAsset(@NonNull Context context, @NonNull String fileName, @NonNull Path dest)
throws IOException {
try (InputStream is = context.getAssets().open(fileName);
OutputStream os = dest.openOutputStream()) {
IoUtils.copy(is, os, -1, null);
IoUtils.copy(is, os);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public static List<Path> create(@NonNull @TarType String type, @NonNull Path sou
tos.putArchiveEntry(tarEntry);
if (!file.isDirectory()) {
try (InputStream is = file.openInputStream()) {
IoUtils.copy(is, tos, -1, null);
IoUtils.copy(is, tos);
}
}
}
Expand Down Expand Up @@ -223,7 +223,7 @@ public static void extract(@NonNull @TarType String type, @NonNull Path[] source
}
if (!entry.isDirectory()) {
try (OutputStream os = file.openOutputStream()) {
IoUtils.copy(tis, os, -1, null);
IoUtils.copy(tis, os);
}
}
}
Expand Down
Loading

0 comments on commit 702ade2

Please sign in to comment.