Refactor: split into modules autojs, common, app and automator
This commit is contained in:
239
common/src/main/java/com/stardust/pio/PFile.java
Normal file
239
common/src/main/java/com/stardust/pio/PFile.java
Normal file
@@ -0,0 +1,239 @@
|
||||
package com.stardust.pio;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/4/1.
|
||||
*/
|
||||
|
||||
public class PFile {
|
||||
|
||||
private static final int BUFFER_SIZE = 8192;
|
||||
|
||||
public static PFile open(String path, String mode) {
|
||||
switch (mode) {
|
||||
case "r":
|
||||
return new PReadableFile(path);
|
||||
case "w":
|
||||
return new PWritableFile();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static boolean createIfNotExists(String path) {
|
||||
ensureDirectory(path);
|
||||
File file = new File(path);
|
||||
if (!file.exists()) {
|
||||
try {
|
||||
return file.createNewFile();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean ensureDirectory(String path) {
|
||||
int i = path.lastIndexOf("\\");
|
||||
if (i < 0)
|
||||
i = path.lastIndexOf("/");
|
||||
if (i >= 0) {
|
||||
String folder = path.substring(0, i);
|
||||
File file = new File(folder);
|
||||
if (file.exists())
|
||||
return true;
|
||||
return file.mkdirs();
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static String read(String path, String encoding) {
|
||||
return read(new File(path), encoding);
|
||||
}
|
||||
|
||||
public static String read(String path) {
|
||||
return read(new File(path));
|
||||
}
|
||||
|
||||
|
||||
public static String read(File file, String encoding) {
|
||||
try {
|
||||
return read(new FileInputStream(file), encoding);
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static String read(File file) {
|
||||
return read(file, "utf-8");
|
||||
}
|
||||
|
||||
public static String read(InputStream is, String encoding) {
|
||||
try {
|
||||
byte[] bytes = new byte[is.available()];
|
||||
is.read(bytes);
|
||||
return new String(bytes, encoding);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static String read(InputStream inputStream) {
|
||||
return read(inputStream, "utf-8");
|
||||
}
|
||||
|
||||
public static boolean copyRaw(Context context, int rawId, String path) {
|
||||
InputStream is = context.getResources().openRawResource(rawId);
|
||||
return copyStream(is, path);
|
||||
}
|
||||
|
||||
public static boolean copyStream(InputStream is, String path) {
|
||||
if (!ensureDirectory(path))
|
||||
return false;
|
||||
File file = new File(path);
|
||||
try {
|
||||
if (!file.exists())
|
||||
if (!file.createNewFile())
|
||||
return false;
|
||||
FileOutputStream fos = new FileOutputStream(file);
|
||||
return write(is, fos);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean write(InputStream is, OutputStream os) {
|
||||
byte[] buffer = new byte[BUFFER_SIZE];
|
||||
try {
|
||||
while (is.available() > 0) {
|
||||
int n = is.read(buffer);
|
||||
os.write(buffer, 0, n);
|
||||
}
|
||||
is.close();
|
||||
os.close();
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static boolean copy(String pathFrom, String pathTo) {
|
||||
try {
|
||||
return copyStream(new FileInputStream(pathFrom), pathTo);
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean copyAsset(Context context, String assetFile, String path) {
|
||||
try {
|
||||
return copyStream(context.getAssets().open(assetFile), path);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static String renameWithoutExtension(String path, String newName) {
|
||||
File file = new File(path);
|
||||
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)
|
||||
return "";
|
||||
return fileName.substring(i + 1);
|
||||
}
|
||||
|
||||
public static boolean write(String path, String text) {
|
||||
return write(new File(path), text);
|
||||
}
|
||||
|
||||
public static boolean write(File file, String text) {
|
||||
try {
|
||||
return write(new FileOutputStream(file), text);
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean write(OutputStream outputStream, String text) {
|
||||
try {
|
||||
outputStream.write(text.getBytes());
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static String generateNotExistingPath(String path, String extension) {
|
||||
if (!new File(path + extension).exists())
|
||||
return path + extension;
|
||||
int i = 0;
|
||||
while (true) {
|
||||
String pathI = path + "(" + i + ")" + extension;
|
||||
if (!new File(pathI).exists())
|
||||
return pathI;
|
||||
i++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static String getNameWithoutExtension(String fileName) {
|
||||
int a = fileName.lastIndexOf('/');
|
||||
if (a < 0)
|
||||
a = fileName.lastIndexOf('\\');
|
||||
int b = fileName.lastIndexOf('.');
|
||||
if (a < 0)
|
||||
a = -1;
|
||||
if (b < 0)
|
||||
b = fileName.length();
|
||||
fileName = fileName.substring(a + 1, b);
|
||||
return fileName;
|
||||
}
|
||||
|
||||
public static File copyAssetToTmpFile(Context context, String path) {
|
||||
String extension = getExtension(path);
|
||||
String name = getNameWithoutExtension(path);
|
||||
if (name.length() < 5) {
|
||||
name += name.hashCode();
|
||||
}
|
||||
try {
|
||||
File tmpFile = File.createTempFile(name, "." + extension, context.getCacheDir());
|
||||
copyAsset(context, path, tmpFile.getPath());
|
||||
return tmpFile;
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean deleteRecursively(File file) {
|
||||
if (file.isFile())
|
||||
return file.delete();
|
||||
for (File child : file.listFiles()) {
|
||||
if (!deleteRecursively(child))
|
||||
return false;
|
||||
}
|
||||
return file.delete();
|
||||
}
|
||||
}
|
||||
106
common/src/main/java/com/stardust/pio/PReadableFile.java
Normal file
106
common/src/main/java/com/stardust/pio/PReadableFile.java
Normal file
@@ -0,0 +1,106 @@
|
||||
package com.stardust.pio;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/4/1.
|
||||
*/
|
||||
|
||||
public class PReadableFile extends PFile {
|
||||
|
||||
private BufferedReader mBufferedReader;
|
||||
private FileInputStream mFileInputStream;
|
||||
private int mBufferingSize;
|
||||
private String mEncoding;
|
||||
|
||||
public PReadableFile(String path) {
|
||||
this(path, Charset.defaultCharset().name());
|
||||
}
|
||||
|
||||
public PReadableFile(String path, String encoding) {
|
||||
this(path, encoding, -1);
|
||||
}
|
||||
|
||||
public PReadableFile(String path, String encoding, int bufferingSize) {
|
||||
mEncoding = encoding;
|
||||
mBufferingSize = bufferingSize;
|
||||
try {
|
||||
mFileInputStream = new FileInputStream(path);
|
||||
} catch (FileNotFoundException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void ensureBufferReader() {
|
||||
if (mBufferedReader == null) {
|
||||
try {
|
||||
if (mBufferingSize == -1)
|
||||
mBufferedReader = new BufferedReader(new InputStreamReader(mFileInputStream, mEncoding));
|
||||
else
|
||||
mBufferedReader = new BufferedReader(new InputStreamReader(mFileInputStream, mEncoding), mBufferingSize);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public String read() {
|
||||
try {
|
||||
byte[] data = new byte[mFileInputStream.available()];
|
||||
mFileInputStream.read(data);
|
||||
return new String(data, mEncoding);
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public String read(int size) {
|
||||
ensureBufferReader();
|
||||
try {
|
||||
char[] chars = new char[size];
|
||||
int len = mBufferedReader.read(chars);
|
||||
return new String(chars, 0, len);
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public String readline() {
|
||||
ensureBufferReader();
|
||||
try {
|
||||
return mBufferedReader.readLine();
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public String[] readlines() {
|
||||
ensureBufferReader();
|
||||
List<String> lines = new ArrayList<>();
|
||||
try {
|
||||
while (mBufferedReader.ready()) {
|
||||
lines.add(mBufferedReader.readLine());
|
||||
}
|
||||
return lines.toArray(new String[lines.size()]);
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void close() {
|
||||
try {
|
||||
if (mBufferedReader != null) {
|
||||
mBufferedReader.close();
|
||||
} else {
|
||||
mFileInputStream.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
15
common/src/main/java/com/stardust/pio/PWritableFile.java
Normal file
15
common/src/main/java/com/stardust/pio/PWritableFile.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package com.stardust.pio;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/4/1.
|
||||
*/
|
||||
|
||||
public class PWritableFile extends PFile {
|
||||
|
||||
public PWritableFile(){
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.stardust.pio;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/4/1.
|
||||
*/
|
||||
|
||||
public class UncheckedIOException extends RuntimeException {
|
||||
|
||||
public UncheckedIOException(IOException cause) {
|
||||
super(cause);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user