Attempt to fix selector memeory leak. Add update checker
This commit is contained in:
@@ -2,7 +2,6 @@ package com.stardust.pio;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.AssetManager;
|
||||
import android.util.Log;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
@@ -11,6 +10,7 @@ import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/4/1.
|
||||
@@ -20,18 +20,34 @@ public class PFile {
|
||||
|
||||
private static final String TAG = "PFile";
|
||||
|
||||
private static final int BUFFER_SIZE = 8192;
|
||||
static final int DEFAULT_BUFFER_SIZE = 8192;
|
||||
static final String DEFAULT_ENCODING = Charset.defaultCharset().name();
|
||||
|
||||
public static PFile open(String path, String mode) {
|
||||
public static PFile open(String path, String mode, String encoding, int bufferSize) {
|
||||
switch (mode) {
|
||||
case "r":
|
||||
return new PReadableFile(path);
|
||||
return new PReadableTextFile(path, encoding, bufferSize);
|
||||
case "w":
|
||||
return new PWritableFile();
|
||||
return new PWritableTextFile(path, encoding, bufferSize, false);
|
||||
case "a":
|
||||
return new PWritableTextFile(path, encoding, bufferSize, true);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static PFile open(String path, String mode, String encoding) {
|
||||
return open(path, mode, encoding, DEFAULT_BUFFER_SIZE);
|
||||
}
|
||||
|
||||
public static PFile open(String path, String mode) {
|
||||
return open(path, mode, DEFAULT_ENCODING, DEFAULT_BUFFER_SIZE);
|
||||
}
|
||||
|
||||
public static PFile open(String path) {
|
||||
return open(path, "r", DEFAULT_ENCODING, DEFAULT_BUFFER_SIZE);
|
||||
}
|
||||
|
||||
|
||||
public static boolean createIfNotExists(String path) {
|
||||
ensureDirectory(path);
|
||||
File file = new File(path);
|
||||
@@ -119,7 +135,7 @@ public class PFile {
|
||||
}
|
||||
|
||||
public static boolean write(InputStream is, OutputStream os) {
|
||||
byte[] buffer = new byte[BUFFER_SIZE];
|
||||
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
|
||||
try {
|
||||
while (is.available() > 0) {
|
||||
int n = is.read(buffer);
|
||||
@@ -155,16 +171,16 @@ public class PFile {
|
||||
|
||||
public static String renameWithoutExtension(String path, String newName) {
|
||||
File file = new File(path);
|
||||
File newFile = new File(file.getParent(), newName + "." + getExtension(file.getName()));
|
||||
File newFile = new File(file.getParent(), newName + getExtension(file.getName()));
|
||||
file.renameTo(newFile);
|
||||
return newFile.getAbsolutePath();
|
||||
}
|
||||
|
||||
public static String getExtension(String fileName) {
|
||||
int i = fileName.lastIndexOf('.');
|
||||
if (i < 0)
|
||||
if (i < 0 || i == fileName.length() - 1)
|
||||
return "";
|
||||
return fileName.substring(i + 1);
|
||||
return fileName.substring(i);
|
||||
}
|
||||
|
||||
public static boolean write(String path, String text) {
|
||||
@@ -223,7 +239,7 @@ public class PFile {
|
||||
name += name.hashCode();
|
||||
}
|
||||
try {
|
||||
File tmpFile = File.createTempFile(name, "." + extension, context.getCacheDir());
|
||||
File tmpFile = File.createTempFile(name, extension, context.getCacheDir());
|
||||
copyAsset(context, path, tmpFile.getPath());
|
||||
return tmpFile;
|
||||
} catch (IOException e) {
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.stardust.pio;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/4/6.
|
||||
*/
|
||||
|
||||
public class PReadableBinaryFile extends PFile {
|
||||
|
||||
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
package com.stardust.pio;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -9,22 +8,22 @@ import java.util.List;
|
||||
* Created by Stardust on 2017/4/1.
|
||||
*/
|
||||
|
||||
public class PReadableFile extends PFile {
|
||||
public class PReadableTextFile extends PFile {
|
||||
|
||||
private BufferedReader mBufferedReader;
|
||||
private FileInputStream mFileInputStream;
|
||||
private int mBufferingSize;
|
||||
private String mEncoding;
|
||||
|
||||
public PReadableFile(String path) {
|
||||
this(path, Charset.defaultCharset().name());
|
||||
public PReadableTextFile(String path) {
|
||||
this(path, PFile.DEFAULT_ENCODING);
|
||||
}
|
||||
|
||||
public PReadableFile(String path, String encoding) {
|
||||
public PReadableTextFile(String path, String encoding) {
|
||||
this(path, encoding, -1);
|
||||
}
|
||||
|
||||
public PReadableFile(String path, String encoding, int bufferingSize) {
|
||||
public PReadableTextFile(String path, String encoding, int bufferingSize) {
|
||||
mEncoding = encoding;
|
||||
mBufferingSize = bufferingSize;
|
||||
try {
|
||||
@@ -1,15 +0,0 @@
|
||||
package com.stardust.pio;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/4/1.
|
||||
*/
|
||||
|
||||
public class PWritableFile extends PFile {
|
||||
|
||||
public PWritableFile(){
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
116
common/src/main/java/com/stardust/pio/PWritableTextFile.java
Normal file
116
common/src/main/java/com/stardust/pio/PWritableTextFile.java
Normal file
@@ -0,0 +1,116 @@
|
||||
package com.stardust.pio;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/4/1.
|
||||
*/
|
||||
|
||||
public class PWritableTextFile extends PFile {
|
||||
|
||||
public static PWritableTextFile open(String path, String encoding, int bufferSize) {
|
||||
return new PWritableTextFile(path, encoding, bufferSize, false);
|
||||
}
|
||||
|
||||
public static PWritableTextFile open(String path, String encoding) {
|
||||
return new PWritableTextFile(path, encoding);
|
||||
}
|
||||
|
||||
public static PWritableTextFile open(String path, boolean append) {
|
||||
return new PWritableTextFile(path, append);
|
||||
}
|
||||
|
||||
public static PWritableTextFile open(String path) {
|
||||
return new PWritableTextFile(path);
|
||||
}
|
||||
|
||||
private BufferedWriter mBufferedWriter;
|
||||
|
||||
public PWritableTextFile(String path, String encoding, int bufferingSize, boolean append) {
|
||||
if (bufferingSize <= 0) {
|
||||
bufferingSize = PFile.DEFAULT_BUFFER_SIZE;
|
||||
}
|
||||
try {
|
||||
mBufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path, append), encoding), bufferingSize);
|
||||
} catch (UnsupportedEncodingException | FileNotFoundException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public PWritableTextFile(String path) {
|
||||
this(path, PFile.DEFAULT_ENCODING, PFile.DEFAULT_BUFFER_SIZE, false);
|
||||
}
|
||||
|
||||
public PWritableTextFile(String path, boolean append) {
|
||||
this(path, PFile.DEFAULT_ENCODING, PFile.DEFAULT_BUFFER_SIZE, append);
|
||||
}
|
||||
|
||||
public PWritableTextFile(String path, int bufferSize) {
|
||||
this(path, PFile.DEFAULT_ENCODING, bufferSize, false);
|
||||
}
|
||||
|
||||
public PWritableTextFile(String path, String encoding) {
|
||||
this(path, encoding, DEFAULT_BUFFER_SIZE, false);
|
||||
}
|
||||
|
||||
public PWritableTextFile(String path, String encoding, boolean append) {
|
||||
this(path, encoding, DEFAULT_BUFFER_SIZE, append);
|
||||
}
|
||||
|
||||
public PWritableTextFile(String path, String encoding, int bufferSize) {
|
||||
this(path, encoding, bufferSize, false);
|
||||
}
|
||||
|
||||
|
||||
public void write(String str) {
|
||||
try {
|
||||
mBufferedWriter.write(str);
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void writeline(String line) {
|
||||
try {
|
||||
mBufferedWriter.write(line);
|
||||
mBufferedWriter.newLine();
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void writelines(String[] lines) {
|
||||
writelines(Arrays.asList(lines));
|
||||
}
|
||||
|
||||
public void writelines(List<String> lines) {
|
||||
for (String line : lines) {
|
||||
writeline(line);
|
||||
}
|
||||
}
|
||||
|
||||
public void close() {
|
||||
try {
|
||||
mBufferedWriter.close();
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void flush() {
|
||||
try {
|
||||
mBufferedWriter.flush();
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -11,4 +11,9 @@ public class UncheckedIOException extends RuntimeException {
|
||||
public UncheckedIOException(IOException cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized IOException getCause() {
|
||||
return (IOException) super.getCause();
|
||||
}
|
||||
}
|
||||
|
||||
22
common/src/main/java/com/stardust/util/DeveloperUtils.java
Normal file
22
common/src/main/java/com/stardust/util/DeveloperUtils.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package com.stardust.util;
|
||||
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/4/5.
|
||||
*/
|
||||
|
||||
public class DeveloperUtils {
|
||||
|
||||
private static final String PACKAGE_NAME = "com.stardust.scriptdroid";
|
||||
|
||||
public static void ensureRunningPackageNotSelf(@Nullable String runningPackage) {
|
||||
if (PACKAGE_NAME.equals(runningPackage)) {
|
||||
throw new SecurityException();
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isRunningPackageSelf(@Nullable String runningPackage) {
|
||||
return PACKAGE_NAME.equals(runningPackage);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user