mirror of
https://github.com/RfidResearchGroup/ChameleonBLEAPI.git
synced 2026-05-12 11:20:47 -07:00
Add files via upload
This commit is contained in:
+122
-122
@@ -1,122 +1,122 @@
|
||||
package com.proxgrind.chameleon.utils.stream;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
|
||||
/*
|
||||
* 用于监听文件更新并且回调各项操作!
|
||||
* */
|
||||
public class FileListenUtils {
|
||||
|
||||
private static String LOG_TAG = FileListenUtils.class.getSimpleName();
|
||||
private OnPrintLisenter mPrintLisenter = null;
|
||||
private volatile boolean isWorking = false;
|
||||
private volatile boolean isPause = false;
|
||||
private FileInputStream fis = null;
|
||||
private File mTarget;
|
||||
|
||||
public FileListenUtils(File target) {
|
||||
mTarget = target;
|
||||
}
|
||||
|
||||
/*
|
||||
* 接口,监听到文件新行会回调!
|
||||
* */
|
||||
public interface OnPrintLisenter {
|
||||
void onPrint(String out);
|
||||
}
|
||||
|
||||
/*
|
||||
* 监听标准输出的线程!
|
||||
* */
|
||||
private class PrintThread extends Thread {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if (mTarget == null) return;
|
||||
Log.d(LOG_TAG, "控制台打印线程被执行!");
|
||||
try {
|
||||
fis = new FileInputStream(mTarget);
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
while (isWorking) {
|
||||
//暂停,也就是跳过!
|
||||
if (isPause) continue;
|
||||
//开始执行监听!
|
||||
if (fis != null) {
|
||||
//Log.d(LOG_TAG, "控制台打印线程正在运行!");
|
||||
try {
|
||||
//处理行数据,进行回调!
|
||||
int byteCount = fis.available();
|
||||
if (byteCount > 0) {
|
||||
byte[] bytes = new byte[byteCount];
|
||||
int len = fis.read(bytes);
|
||||
if (len != byteCount) Log.d(LOG_TAG, "接收到的字节长度和欲接收的字节长度不一样!");
|
||||
String str = new String(bytes, "UTF-8");
|
||||
mPrintLisenter.onPrint(str);
|
||||
//Log.d(LOG_TAG, "控制台打印线程: " + str);
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
ioe.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
isWorking = false;
|
||||
//在停止线程后应当释放资源!
|
||||
try {
|
||||
if (fis != null)
|
||||
fis.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
Log.d(LOG_TAG, "控制台打印线程被结束!");
|
||||
}
|
||||
}
|
||||
|
||||
public void setPrintLisenter(OnPrintLisenter lisenter) {
|
||||
this.mPrintLisenter = lisenter;
|
||||
}
|
||||
|
||||
//开始打印线程,从文件种读取被重定向到文件的标准输出!
|
||||
public void start() {
|
||||
if (mPrintLisenter != null) {
|
||||
//检查标准输出重定向的文件!
|
||||
if (!mTarget.exists() || !mTarget.isFile()) {
|
||||
mPrintLisenter.onPrint("中转文件不存在或非文件,请检查文件读写权限!\n");
|
||||
mPrintLisenter.onPrint("标准输出重定向执行失败,无法获得运行时消息!\n");
|
||||
mPrintLisenter.onPrint("尝试打印重定向文件信息:" + mTarget.getAbsolutePath() + "\n");
|
||||
return;
|
||||
}
|
||||
//不在运行状态则开始新的线程!
|
||||
if (!isWorking) {
|
||||
//标志位改变,开始执行监听线程!
|
||||
isWorking = true;
|
||||
new PrintThread().start();
|
||||
} else {
|
||||
//当前已有在执行的线程!
|
||||
Log.d(LOG_TAG, "当前已有在执行的控制台转发监听线程!");
|
||||
if (isPause) {
|
||||
Log.d(LOG_TAG, "当前已在执行的控制台转发监听线程被暂停,将会重新启动!");
|
||||
isPause = false;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Log.w(LOG_TAG, "mPrintLisenter是空引用,请检查逻辑!");
|
||||
}
|
||||
}
|
||||
|
||||
//停止打印线程!
|
||||
public void stop() {
|
||||
isWorking = false;
|
||||
}
|
||||
|
||||
//暂停打印线程!
|
||||
public void pause() {
|
||||
isPause = true;
|
||||
}
|
||||
}
|
||||
package com.proxgrind.chameleon.utils.stream;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
|
||||
/*
|
||||
* 用于监听文件更新并且回调各项操作!
|
||||
* */
|
||||
public class FileListenUtils {
|
||||
|
||||
private static String LOG_TAG = FileListenUtils.class.getSimpleName();
|
||||
private OnPrintLisenter mPrintLisenter = null;
|
||||
private volatile boolean isWorking = false;
|
||||
private volatile boolean isPause = false;
|
||||
private FileInputStream fis = null;
|
||||
private File mTarget;
|
||||
|
||||
public FileListenUtils(File target) {
|
||||
mTarget = target;
|
||||
}
|
||||
|
||||
/*
|
||||
* 接口,监听到文件新行会回调!
|
||||
* */
|
||||
public interface OnPrintLisenter {
|
||||
void onPrint(String out);
|
||||
}
|
||||
|
||||
/*
|
||||
* 监听标准输出的线程!
|
||||
* */
|
||||
private class PrintThread extends Thread {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if (mTarget == null) return;
|
||||
Log.d(LOG_TAG, "控制台打印线程被执行!");
|
||||
try {
|
||||
fis = new FileInputStream(mTarget);
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
while (isWorking) {
|
||||
//暂停,也就是跳过!
|
||||
if (isPause) continue;
|
||||
//开始执行监听!
|
||||
if (fis != null) {
|
||||
//Log.d(LOG_TAG, "控制台打印线程正在运行!");
|
||||
try {
|
||||
//处理行数据,进行回调!
|
||||
int byteCount = fis.available();
|
||||
if (byteCount > 0) {
|
||||
byte[] bytes = new byte[byteCount];
|
||||
int len = fis.read(bytes);
|
||||
if (len != byteCount) Log.d(LOG_TAG, "接收到的字节长度和欲接收的字节长度不一样!");
|
||||
String str = new String(bytes, "UTF-8");
|
||||
mPrintLisenter.onPrint(str);
|
||||
//Log.d(LOG_TAG, "控制台打印线程: " + str);
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
ioe.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
isWorking = false;
|
||||
//在停止线程后应当释放资源!
|
||||
try {
|
||||
if (fis != null)
|
||||
fis.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
Log.d(LOG_TAG, "控制台打印线程被结束!");
|
||||
}
|
||||
}
|
||||
|
||||
public void setPrintLisenter(OnPrintLisenter lisenter) {
|
||||
this.mPrintLisenter = lisenter;
|
||||
}
|
||||
|
||||
//开始打印线程,从文件种读取被重定向到文件的标准输出!
|
||||
public void start() {
|
||||
if (mPrintLisenter != null) {
|
||||
//检查标准输出重定向的文件!
|
||||
if (!mTarget.exists() || !mTarget.isFile()) {
|
||||
mPrintLisenter.onPrint("中转文件不存在或非文件,请检查文件读写权限!\n");
|
||||
mPrintLisenter.onPrint("标准输出重定向执行失败,无法获得运行时消息!\n");
|
||||
mPrintLisenter.onPrint("尝试打印重定向文件信息:" + mTarget.getAbsolutePath() + "\n");
|
||||
return;
|
||||
}
|
||||
//不在运行状态则开始新的线程!
|
||||
if (!isWorking) {
|
||||
//标志位改变,开始执行监听线程!
|
||||
isWorking = true;
|
||||
new PrintThread().start();
|
||||
} else {
|
||||
//当前已有在执行的线程!
|
||||
Log.d(LOG_TAG, "当前已有在执行的控制台转发监听线程!");
|
||||
if (isPause) {
|
||||
Log.d(LOG_TAG, "当前已在执行的控制台转发监听线程被暂停,将会重新启动!");
|
||||
isPause = false;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Log.w(LOG_TAG, "mPrintLisenter是空引用,请检查逻辑!");
|
||||
}
|
||||
}
|
||||
|
||||
//停止打印线程!
|
||||
public void stop() {
|
||||
isWorking = false;
|
||||
}
|
||||
|
||||
//暂停打印线程!
|
||||
public void pause() {
|
||||
isPause = true;
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,31 @@
|
||||
package com.proxgrind.chameleon.utils.stream;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.Flushable;
|
||||
import java.io.IOException;
|
||||
|
||||
public class IOUtils {
|
||||
public static boolean close(@Nullable Closeable closeable) {
|
||||
if (closeable == null) return false;
|
||||
try {
|
||||
closeable.close();
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean flush(@Nullable Flushable flushable) {
|
||||
if (flushable == null) return false;
|
||||
try {
|
||||
flushable.flush();
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package com.proxgrind.chameleon.utils.stream;
|
||||
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
|
||||
public class LineParseUtils extends Thread {
|
||||
|
||||
private String LOG_TAG = this.getClass().getSimpleName();
|
||||
|
||||
/*
|
||||
* 回调接口,当有完整的一行出现时回调!
|
||||
* */
|
||||
public interface OnNewLineLisenter {
|
||||
void onNewLine(String str);
|
||||
}
|
||||
|
||||
//队列,生产消费!
|
||||
private Queue<Character> mConsoleQueue = new LinkedBlockingQueue<>();
|
||||
//回调接口!
|
||||
private OnNewLineLisenter mLisenter;
|
||||
//标志,是否暂停!
|
||||
private volatile boolean mPauseLabel = false;
|
||||
//标志,是否结束!
|
||||
private volatile boolean mCancelLabel = false;
|
||||
|
||||
public LineParseUtils(OnNewLineLisenter lisenter) {
|
||||
mLisenter = lisenter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
//迭代队列
|
||||
StringBuilder sb = new StringBuilder(256);
|
||||
while (!mCancelLabel) {
|
||||
if (mPauseLabel) {
|
||||
//Log.d(LOG_TAG, "DynamicLineParseThread is pause!");
|
||||
continue;
|
||||
}
|
||||
if (mConsoleQueue.size() == 0) continue;
|
||||
if (mLisenter != null) {
|
||||
Character c;
|
||||
while ((c = mConsoleQueue.poll()) != null) {
|
||||
//遇到的是换行符,则需要处理换行符!
|
||||
if (c == '\n') {
|
||||
//回调之前的结果!
|
||||
String s = sb.toString();
|
||||
//Log.d("****", "DLPU新行: " + s);
|
||||
//开始回调!
|
||||
mLisenter.onNewLine(s);
|
||||
//清除缓存,重新建立对象!
|
||||
sb = new StringBuilder(256);
|
||||
//尝试通知GC
|
||||
System.gc();
|
||||
//直接跳过下一步的换行符添加!
|
||||
break;
|
||||
}
|
||||
//否则一直追加进缓冲区
|
||||
sb.append((char) c);
|
||||
//Log.d("****", "DLPU: " + sb.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void start() {
|
||||
//处理启动相关的实现!
|
||||
mPauseLabel = false;
|
||||
if (!isAlive()) {
|
||||
super.start();
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void appendText(Character txt) {
|
||||
mConsoleQueue.add(txt);
|
||||
}
|
||||
|
||||
public synchronized void pause() {
|
||||
mPauseLabel = true;
|
||||
}
|
||||
|
||||
public synchronized void cancel() {
|
||||
mCancelLabel = true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,207 @@
|
||||
package com.proxgrind.chameleon.utils.stream;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileFilter;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FilenameFilter;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipFile;
|
||||
import java.util.zip.ZipInputStream;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
public class ZipUtils {
|
||||
public static final String TAG = "ZIP";
|
||||
|
||||
private ZipUtils() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 解压zip到指定的路径
|
||||
*
|
||||
* @param zipFileString ZIP的名称
|
||||
* @param outPathString 要解压缩路径
|
||||
* @throws Exception
|
||||
*/
|
||||
public static void unZipFolder(String zipFileString, String outPathString) throws Exception {
|
||||
ZipInputStream inZip = new ZipInputStream(new FileInputStream(zipFileString));
|
||||
ZipEntry zipEntry;
|
||||
String szName = "";
|
||||
while ((zipEntry = inZip.getNextEntry()) != null) {
|
||||
szName = zipEntry.getName();
|
||||
if (zipEntry.isDirectory()) {
|
||||
//获取部件的文件夹名
|
||||
szName = szName.substring(0, szName.length() - 1);
|
||||
File folder = new File(outPathString + File.separator + szName);
|
||||
folder.mkdirs();
|
||||
} else {
|
||||
Log.e(TAG, outPathString + File.separator + szName);
|
||||
File file = new File(outPathString + File.separator + szName);
|
||||
if (!file.exists()) {
|
||||
Log.e(TAG, "Create the file:" + outPathString + File.separator + szName);
|
||||
file.getParentFile().mkdirs();
|
||||
file.createNewFile();
|
||||
}
|
||||
// 获取文件的输出流
|
||||
FileOutputStream out = new FileOutputStream(file);
|
||||
int len;
|
||||
byte[] buffer = new byte[1024];
|
||||
// 读取(字节)字节到缓冲区
|
||||
while ((len = inZip.read(buffer)) != -1) {
|
||||
// 从缓冲区(0)位置写入(字节)字节
|
||||
out.write(buffer, 0, len);
|
||||
out.flush();
|
||||
}
|
||||
out.close();
|
||||
}
|
||||
}
|
||||
inZip.close();
|
||||
}
|
||||
|
||||
public static void unZipFolder(String zipFileString, String outPathString, String szName) throws Exception {
|
||||
ZipInputStream inZip = new ZipInputStream(new FileInputStream(zipFileString));
|
||||
ZipEntry zipEntry;
|
||||
while ((zipEntry = inZip.getNextEntry()) != null) {
|
||||
//szName = zipEntry.getName();
|
||||
if (zipEntry.isDirectory()) {
|
||||
//获取部件的文件夹名
|
||||
szName = szName.substring(0, szName.length() - 1);
|
||||
File folder = new File(outPathString + File.separator + szName);
|
||||
folder.mkdirs();
|
||||
} else {
|
||||
Log.e(TAG, outPathString + File.separator + szName);
|
||||
File file = new File(outPathString + File.separator + szName);
|
||||
if (!file.exists()) {
|
||||
Log.e(TAG, "Create the file:" + outPathString + File.separator + szName);
|
||||
file.getParentFile().mkdirs();
|
||||
file.createNewFile();
|
||||
}
|
||||
// 获取文件的输出流
|
||||
FileOutputStream out = new FileOutputStream(file);
|
||||
int len;
|
||||
byte[] buffer = new byte[1024];
|
||||
// 读取(字节)字节到缓冲区
|
||||
while ((len = inZip.read(buffer)) != -1) {
|
||||
// 从缓冲区(0)位置写入(字节)字节
|
||||
out.write(buffer, 0, len);
|
||||
out.flush();
|
||||
}
|
||||
out.close();
|
||||
}
|
||||
}
|
||||
inZip.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* 压缩文件和文件夹
|
||||
*
|
||||
* @param srcFileString 要压缩的文件或文件夹
|
||||
* @param zipFileString 压缩完成的Zip路径
|
||||
* @throws Exception
|
||||
*/
|
||||
public static void zipFolder(String srcFileString, String zipFileString, FilenameFilter filter) throws Exception {
|
||||
//创建ZIP
|
||||
ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream(zipFileString));
|
||||
//创建文件
|
||||
File file = new File(srcFileString);
|
||||
//压缩
|
||||
zipFiles(file.getParent() + File.separator, file.getName(), outZip, filter);
|
||||
//完成和关闭
|
||||
outZip.finish();
|
||||
outZip.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* 压缩文件
|
||||
*
|
||||
* @param folderString
|
||||
* @param fileString
|
||||
* @param zipOutputSteam
|
||||
* @throws Exception
|
||||
*/
|
||||
private static void zipFiles(String folderString, String fileString, ZipOutputStream zipOutputSteam, FilenameFilter filter) throws Exception {
|
||||
if (zipOutputSteam == null)
|
||||
return;
|
||||
File file = new File(folderString + fileString);
|
||||
if (file.isFile()) {
|
||||
ZipEntry zipEntry = new ZipEntry(fileString);
|
||||
FileInputStream inputStream = new FileInputStream(file);
|
||||
zipOutputSteam.putNextEntry(zipEntry);
|
||||
int len;
|
||||
byte[] buffer = new byte[4096];
|
||||
while ((len = inputStream.read(buffer)) != -1) {
|
||||
zipOutputSteam.write(buffer, 0, len);
|
||||
}
|
||||
zipOutputSteam.closeEntry();
|
||||
} else {
|
||||
//文件夹
|
||||
String[] fileList = file.list(filter);
|
||||
if (fileList != null) {
|
||||
//没有子文件和压缩
|
||||
if (fileList.length <= 0) {
|
||||
ZipEntry zipEntry = new ZipEntry(fileString + File.separator);
|
||||
zipOutputSteam.putNextEntry(zipEntry);
|
||||
zipOutputSteam.closeEntry();
|
||||
}
|
||||
//子文件和递归
|
||||
for (String s : fileList) {
|
||||
zipFiles(folderString, fileString + File.separator + s, zipOutputSteam, filter);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回zip的文件输入流
|
||||
*
|
||||
* @param zipFileString zip的名称
|
||||
* @param fileString ZIP的文件名
|
||||
* @return InputStream
|
||||
* @throws Exception
|
||||
*/
|
||||
public static InputStream upZip(String zipFileString, String fileString) throws Exception {
|
||||
ZipFile zipFile = new ZipFile(zipFileString);
|
||||
ZipEntry zipEntry = zipFile.getEntry(fileString);
|
||||
return zipFile.getInputStream(zipEntry);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回ZIP中的文件列表(文件和文件夹)
|
||||
*
|
||||
* @param zipFileString ZIP的名称
|
||||
* @param bContainFolder 是否包含文件夹
|
||||
* @param bContainFile 是否包含文件
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static List<File> getFileList(String zipFileString, boolean bContainFolder, boolean bContainFile) throws Exception {
|
||||
List<File> fileList = new ArrayList<File>();
|
||||
ZipInputStream inZip = new ZipInputStream(new FileInputStream(zipFileString));
|
||||
ZipEntry zipEntry;
|
||||
String szName;
|
||||
while ((zipEntry = inZip.getNextEntry()) != null) {
|
||||
szName = zipEntry.getName();
|
||||
if (zipEntry.isDirectory()) {
|
||||
// 获取部件的文件夹名
|
||||
szName = szName.substring(0, szName.length() - 1);
|
||||
File folder = new File(szName);
|
||||
if (bContainFolder) {
|
||||
fileList.add(folder);
|
||||
}
|
||||
} else {
|
||||
File file = new File(szName);
|
||||
if (bContainFile) {
|
||||
fileList.add(file);
|
||||
}
|
||||
}
|
||||
}
|
||||
inZip.close();
|
||||
return fileList;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user