Use try-with-resources.
This commit is contained in:
parent
8bcf0c6498
commit
95333d37c8
8 changed files with 84 additions and 137 deletions
|
|
@ -34,7 +34,6 @@ import org.schabi.newpipe.MainActivity;
|
|||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.util.LinkedList;
|
||||
|
|
@ -106,7 +105,6 @@ public final class StateSaver {
|
|||
+ "writeRead = [" + writeRead + "]");
|
||||
}
|
||||
|
||||
FileInputStream fileInputStream = null;
|
||||
try {
|
||||
Queue<Object> savedObjects
|
||||
= STATE_OBJECTS_HOLDER.remove(savedState.getPrefixFileSaved());
|
||||
|
|
@ -127,10 +125,12 @@ public final class StateSaver {
|
|||
return null;
|
||||
}
|
||||
|
||||
fileInputStream = new FileInputStream(file);
|
||||
final ObjectInputStream inputStream = new ObjectInputStream(fileInputStream);
|
||||
//noinspection unchecked
|
||||
savedObjects = (Queue<Object>) inputStream.readObject();
|
||||
try (FileInputStream fileInputStream = new FileInputStream(file);
|
||||
ObjectInputStream inputStream = new ObjectInputStream(fileInputStream)) {
|
||||
//noinspection unchecked
|
||||
savedObjects = (Queue<Object>) inputStream.readObject();
|
||||
}
|
||||
|
||||
if (savedObjects != null) {
|
||||
writeRead.readFrom(savedObjects);
|
||||
}
|
||||
|
|
@ -138,13 +138,6 @@ public final class StateSaver {
|
|||
return savedState;
|
||||
} catch (final Exception e) {
|
||||
Log.e(TAG, "Failed to restore state", e);
|
||||
} finally {
|
||||
if (fileInputStream != null) {
|
||||
try {
|
||||
fileInputStream.close();
|
||||
} catch (final IOException ignored) {
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
|
@ -227,7 +220,6 @@ public final class StateSaver {
|
|||
}
|
||||
}
|
||||
|
||||
FileOutputStream fileOutputStream = null;
|
||||
try {
|
||||
File cacheDir = new File(cacheDirPath);
|
||||
if (!cacheDir.exists()) {
|
||||
|
|
@ -258,19 +250,14 @@ public final class StateSaver {
|
|||
}
|
||||
}
|
||||
|
||||
fileOutputStream = new FileOutputStream(file);
|
||||
final ObjectOutputStream outputStream = new ObjectOutputStream(fileOutputStream);
|
||||
outputStream.writeObject(savedObjects);
|
||||
try (FileOutputStream fileOutputStream = new FileOutputStream(file);
|
||||
ObjectOutputStream outputStream = new ObjectOutputStream(fileOutputStream)) {
|
||||
outputStream.writeObject(savedObjects);
|
||||
}
|
||||
|
||||
return new SavedState(prefixFileName, file.getAbsolutePath());
|
||||
} catch (final Exception e) {
|
||||
Log.e(TAG, "Failed to save state", e);
|
||||
} finally {
|
||||
if (fileOutputStream != null) {
|
||||
try {
|
||||
fileOutputStream.close();
|
||||
} catch (final IOException ignored) { }
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -45,15 +45,15 @@ public final class ZipHelper {
|
|||
public static void addFileToZip(final ZipOutputStream outZip, final String file,
|
||||
final String name) throws Exception {
|
||||
final byte[] data = new byte[BUFFER_SIZE];
|
||||
final FileInputStream fi = new FileInputStream(file);
|
||||
final BufferedInputStream inputStream = new BufferedInputStream(fi, BUFFER_SIZE);
|
||||
final ZipEntry entry = new ZipEntry(name);
|
||||
outZip.putNextEntry(entry);
|
||||
int count;
|
||||
while ((count = inputStream.read(data, 0, BUFFER_SIZE)) != -1) {
|
||||
outZip.write(data, 0, count);
|
||||
try (FileInputStream fi = new FileInputStream(file);
|
||||
BufferedInputStream inputStream = new BufferedInputStream(fi, BUFFER_SIZE)) {
|
||||
final ZipEntry entry = new ZipEntry(name);
|
||||
outZip.putNextEntry(entry);
|
||||
int count;
|
||||
while ((count = inputStream.read(data, 0, BUFFER_SIZE)) != -1) {
|
||||
outZip.write(data, 0, count);
|
||||
}
|
||||
}
|
||||
inputStream.close();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -68,37 +68,35 @@ public final class ZipHelper {
|
|||
*/
|
||||
public static boolean extractFileFromZip(final String filePath, final String file,
|
||||
final String name) throws Exception {
|
||||
try (ZipInputStream inZip = new ZipInputStream(new BufferedInputStream(
|
||||
new FileInputStream(filePath)))) {
|
||||
final byte[] data = new byte[BUFFER_SIZE];
|
||||
|
||||
final ZipInputStream inZip = new ZipInputStream(
|
||||
new BufferedInputStream(
|
||||
new FileInputStream(filePath)));
|
||||
boolean found = false;
|
||||
|
||||
final byte[] data = new byte[BUFFER_SIZE];
|
||||
|
||||
boolean found = false;
|
||||
|
||||
ZipEntry ze;
|
||||
while ((ze = inZip.getNextEntry()) != null) {
|
||||
if (ze.getName().equals(name)) {
|
||||
found = true;
|
||||
// delete old file first
|
||||
final File oldFile = new File(file);
|
||||
if (oldFile.exists()) {
|
||||
if (!oldFile.delete()) {
|
||||
throw new Exception("Could not delete " + file);
|
||||
ZipEntry ze;
|
||||
while ((ze = inZip.getNextEntry()) != null) {
|
||||
if (ze.getName().equals(name)) {
|
||||
found = true;
|
||||
// delete old file first
|
||||
final File oldFile = new File(file);
|
||||
if (oldFile.exists()) {
|
||||
if (!oldFile.delete()) {
|
||||
throw new Exception("Could not delete " + file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final FileOutputStream outFile = new FileOutputStream(file);
|
||||
int count = 0;
|
||||
while ((count = inZip.read(data)) != -1) {
|
||||
outFile.write(data, 0, count);
|
||||
}
|
||||
try (FileOutputStream outFile = new FileOutputStream(file)) {
|
||||
int count = 0;
|
||||
while ((count = inZip.read(data)) != -1) {
|
||||
outFile.write(data, 0, count);
|
||||
}
|
||||
}
|
||||
|
||||
outFile.close();
|
||||
inZip.closeEntry();
|
||||
inZip.closeEntry();
|
||||
}
|
||||
}
|
||||
return found;
|
||||
}
|
||||
return found;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue