打包解包

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

// 主程序类
public final class CodeFile{
// 此程序的入口点
public static void main(String[] args){
TaskManager taskManager = new TaskManager(args);
taskManager.runTask();
}

// 任务调度类,提供了此程序的主要功能
private static final class TaskManager{
private ParameterManager parameterManager;
private EntityManager entityManager;
private StreamManager streamManager;
private TimerManager timerManager;

public TaskManager(String[] args){
this.parameterManager = new ParameterManager(args);
this.streamManager = new StreamManager();
this.entityManager = new EntityManager(this.streamManager.getContentBytes());
this.timerManager = new TimerManager();
}

// 执行任务
public final void runTask(){
if(this.parameterManager.getCodeModel().equals(ParameterManager.ENCRYPT)){
if(this.parameterManager.getExportModel().equals(ParameterManager.SEPARATE)){
this.runEncryptSeparateTask();
}else{
this.runEncryptIncorporateTask();
}
}else{
if(this.parameterManager.getExportModel().equals(ParameterManager.SEPARATE)){
this.runRecoverSeparateTask();
}else{
this.runRecoverIncorporateTask();
}
}
}

// 执行简单的一对一加密任务
private final void runEncryptSeparateTask(){
String name = this.parameterManager.getFileImportPath().getName();
if(name.equals("")){
name = this.parameterManager.getFileImportPath().getPath().substring(0,1);
}
String exportRoot =
this.parameterManager.getFileExportPath().getPath()
+ ParameterManager.FILE_PATH_DELIMITER + name;
int prefixLength = this.parameterManager.getFileImportPath().getPath().length();
for(int pointer = 0; pointer < this.parameterManager.getFilesAmount(); pointer++){
File importFile = this.parameterManager.getImportFiles()[pointer];
this.streamManager.createContentReader(importFile);
File exportFile =
new File(exportRoot + importFile.getPath().substring(prefixLength));
File filePath = new File(exportFile.getParent());
if(!filePath.exists()){
filePath.mkdirs();
}
this.streamManager.createContentWriter(exportFile);
this.timerManager.setThisTime(System.currentTimeMillis());
while(this.streamManager.readContent()){
this.entityManager.encryptEntities(EntityTree.root,this.streamManager.getContentBytesAmount());
this.streamManager.writeContent();
}
this.streamManager.closeContentWriter();
this.streamManager.closeContentReader();
System.out.println("已处理文件 \"" + importFile + "\"");
System.out.println("结果文件为 \"" + exportFile + "\"");
System.out.println("处理用时为 " + this.timerManager.countThisTime());
}
System.out.println("\n总共处理了" + this.parameterManager.getFilesAmount() + "个文件,处理用时为 "
+ this.timerManager.countTotalTime());
}

// 执行打包式加密任务
private final void runEncryptIncorporateTask(){
String name = this.parameterManager.getFileImportPath().getName();
if(name.equals("")){
name = this.parameterManager.getFileImportPath().getPath().substring(0,1);
}
File contentFile =
new File(this.parameterManager.getFileExportPath()
+ ParameterManager.FILE_PATH_DELIMITER + name);
this.streamManager.createContentWriter(contentFile);
File tempFile = new File(contentFile + ParameterManager.TEMPFILE);
tempFile.deleteOnExit();
this.streamManager.createInfoWriter(tempFile);
int filesAmount = this.parameterManager.getFilesAmount();
for(int pointer = 0; pointer < filesAmount; pointer++){
File importFile = this.parameterManager.getImportFiles()[pointer];
this.streamManager.createContentReader(importFile);
String blockInfo =
importFile.getPath().substring((int)this.parameterManager.getFileImportPath().getPath().length() + 1);
int blockAmount = 0;
int theLastBlockSize = 0;
this.timerManager.setThisTime(System.currentTimeMillis());
while(this.streamManager.readContent()){
theLastBlockSize = this.streamManager.getContentBytesAmount();
this.entityManager.encryptEntities(EntityTree.root,theLastBlockSize);
this.streamManager.writeContent();
blockAmount++;
}
this.streamManager.closeContentReader();
this.streamManager.writeInfo(blockInfo,blockAmount,theLastBlockSize);
System.out.println("已处理文件 \"" + importFile + "\"");
String time = this.timerManager.countThisTime();
System.out.println("处理用时为 " + time);
}
this.streamManager.closeWriters();
this.streamManager.createContentReader(tempFile);
File infoFile = new File(contentFile + ParameterManager.CODEINFO);
this.streamManager.createContentWriter(infoFile);
this.timerManager.setThisTime(System.currentTimeMillis());
while(this.streamManager.readContent()){
this.entityManager.encryptEntities(EntityTree.root,this.streamManager.getContentBytesAmount());
this.streamManager.writeContent();
}
this.streamManager.closeContentWriter();
this.streamManager.closeContentReader();
System.out.println("\n已处理密码信息文件 \"" + infoFile + "\"");
System.out.println("处理用时为 " + this.timerManager.countThisTime());
System.out.println("\n总共处理了" + filesAmount + "个文件,处理用时为 "
+ this.timerManager.countTotalTime());
System.out.println("\n结果文件集为:");
System.out.println("\"" + contentFile + "\"(密码文件)");
System.out.println("\"" + infoFile + "\"(密码信息文件)");
}

// 执行简单的一对一解密任务
private final void runRecoverSeparateTask(){
String exportRoot = this.parameterManager.getFileExportPath().getPath();
int prefixLength = this.parameterManager.getFileImportPath().getPath().length();
for(int pointer = 0; pointer < this.parameterManager.getFilesAmount(); pointer++){
File importFile = this.parameterManager.getImportFiles()[pointer];
this.streamManager.createContentReader(importFile);
File exportFile =
new File(exportRoot + importFile.getPath().substring(prefixLength));
File filePath = new File(exportFile.getParent());
if(!filePath.exists()){
filePath.mkdirs();
}
this.streamManager.createContentWriter(exportFile);
this.timerManager.setThisTime(System.currentTimeMillis());
while(this.streamManager.readContent()){
this.entityManager.recoverEntities(EntityTree.root,this.streamManager.getContentBytesAmount());
this.streamManager.writeContent();
}
this.streamManager.closeContentWriter();
this.streamManager.closeContentReader();
System.out.println("已处理文件 \"" + importFile + "\"");
System.out.println("结果文件为 \"" + exportFile + "\"");
System.out.println("处理用时为 " + this.timerManager.countThisTime());
}
System.out.println("\n总共处理了" + this.parameterManager.getFilesAmount() + "个文件,处理用时为 "
+ this.timerManager.countTotalTime());
}

// 执行拆包式解密任务
private final void runRecoverIncorporateTask(){
long totalTime = 0l;
int totalFile = 0;
int filesAmount = this.parameterManager.getFilesAmount();
int pointer = 0;
File[] tempFile = new File[filesAmount / 2];
for(; pointer < filesAmount / 2; pointer++){
File infoFile = this.parameterManager.getImportInfoFiles()[pointer];
this.streamManager.createContentReader(infoFile);
tempFile[pointer] = new File(infoFile + ParameterManager.TEMPFILE);
tempFile[pointer].deleteOnExit();
this.streamManager.createContentWriter(tempFile[pointer]);
this.timerManager.setThisTime(System.currentTimeMillis());
while(this.streamManager.readContent()){
this.entityManager.recoverEntities(EntityTree.root,this.streamManager.getContentBytesAmount());
this.streamManager.writeContent();
}
this.streamManager.closeContentWriter();
this.streamManager.closeContentReader();
System.out.println("已处理密码信息文件 \"" + infoFile + "\"");
System.out.println("处理用时为 " + this.timerManager.countThisTime());
}
totalTime = this.timerManager.getTotalTime();
this.timerManager.resetTimer();
for(pointer = 0; pointer < filesAmount / 2; pointer++){
File contentFile = this.parameterManager.getImportContentFiles()[pointer];
this.streamManager.createContentReader(contentFile);
this.streamManager.createInfoReader(tempFile[pointer]);
File exportRoot =
new File(this.parameterManager.getFileExportPath()
+ ParameterManager.FILE_PATH_DELIMITER + contentFile.getName());
exportRoot.mkdir();
while(this.streamManager.readInfo()){
String blockInfo = this.streamManager.getBlockInfo();
File resultFile =
new File(exportRoot + ParameterManager.FILE_PATH_DELIMITER + blockInfo);
File filePath = new File(resultFile.getParent());
if(!filePath.exists()){
filePath.mkdirs();
}
this.streamManager.createContentWriter(resultFile);
int blockCount = 0;
int blockAmount = this.streamManager.getBlockAmount();
int theLastBlockSize = this.streamManager.getTheLastBlockSize();
this.timerManager.setThisTime(System.currentTimeMillis());
while(++blockCount < blockAmount){
this.streamManager.readContent();
this.entityManager.recoverEntities(EntityTree.root,this.streamManager.getContentBytesAmount());
this.streamManager.writeContent();
}
this.streamManager.readContent(theLastBlockSize);
this.entityManager.recoverEntities(EntityTree.root,theLastBlockSize);
this.streamManager.writeContent();
this.streamManager.closeContentWriter();
totalFile++;
System.out.println("\n已处理文件 \"" + contentFile + ">>" + blockInfo + "\"");
System.out.println("结果文件为 \"" + resultFile + "\"");
System.out.println("处理用时为 " + this.timerManager.countThisTime());
}
this.streamManager.closeReaders();
System.out.println("\n已处理混合文件 \"" + contentFile + "\"");
System.out.println("结果文件路径为 \"" + exportRoot + "\"");
System.out.println("处理用时为 " + this.timerManager.countTotalTime());
totalTime += this.timerManager.getTotalTime();
this.timerManager.setTotalTime(0l);
}
this.timerManager.setTotalTime(totalTime);
System.out.println("\n\n总共处理了" + filesAmount + "个文件:");
System.out.println("包括" + filesAmount / 2 + "个密码信息文件;" + filesAmount / 2 + "个混合文件(包含"
+ totalFile + "个文件)。");
System.out.println("处理用时为 " + this.timerManager.countTotalTime());
}
}

// 参数管理类,用于解析此程序的输入参数和保存初始化信息
private static final class ParameterManager{
public static final String FILE_PATH_DELIMITER;
public static final String ENCRYPT;
public static final String RECOVER;
public static final String INCORPORATE;
public static final String SEPARATE;
public static final String CODEINFO;
public static final String TEMPFILE;
public static final String HELP_STRING;
private String fileCodeModel;
private String fileExportModel;
private File fileImportPath;
private File fileExportPath;
private File[] importFiles;
private File[] importContentFiles;
private File[] importInfoFiles;
private int filesAmount;
private boolean hasSubDirectories;
static{
FILE_PATH_DELIMITER = System.getProperty("file.separator");
ENCRYPT = "/encrypt";
RECOVER = "/recover";
INCORPORATE = "/incorporate";
SEPARATE = "/separate";
CODEINFO = ".CodeInfo";
TEMPFILE = ".TempFile";
HELP_STRING =
"\n程序名称:CodeFile\n"
+ "程序功能:支持对任意类型的文件进行加密,并支持加密树型目录结构\n"
+ "程序版本:1.0\n"
+ "最后修改日期:2009年4月20日\n"
+ "作者:李允\n\n"
+ "命令行: CodeFile /[encrypt | recover] /[incorporate | separate] ImportFilePath ExportFilePath\n"
+ "参数说明:\n"
+ "encrypt"
+ "\t\t加密文件\n"
+ "recover"
+ "\t\t解密文件\n"
+ "incorporate\t把所有输入文件加密并打包成一个文件,或解密一个或多个被加密并打包的文件\n"
+ "separate"
+ "\t简单的一对一加密或解密文件\n"
+ "ImportFilePath"
+ "\t输入文件路径\n"
+ "ExportFilePath"
+ "\t输出文件路径\n"
+ "注意:ImportFilePath 和 ExportFilePath 必须是目录,且不能为同一目录,ImportFilePath 不能为空目录,ExportFilePath 必须为空目录;"
+ "如果联用 /recover 和 /incorporate 解密文件,ImportFilePath 不能包含子目录,并且其中的密码文件和密码信息文件("
+ CODEINFO + ")是一一对应的,而后者的名称是由前者的名称加 " + CODEINFO + " 组成!";
}

private final void showHelp(){
System.out.println(HELP_STRING);
}

public ParameterManager(String[] args){
this.checkParameters(args);
}

private final void checkParameters(String[] args){
this.checkAmount(args,4);
this.checkModels(args[0],args[1]);
this.checkFilePath(args[2],args[3]);
}

private final void checkAmount(String[] args, int parametersAmount){
if(args.length != parametersAmount){
this.showHelp();
System.exit(0);
}
}

private final void checkModels(String codeModel, String exportModel){
this.fileCodeModel = codeModel;
if(!this.fileCodeModel.equals(ENCRYPT) && !this.fileCodeModel.equals(RECOVER)){
this.showHelp();
System.exit(0);
}
this.fileExportModel = exportModel;
if(!this.fileExportModel.equals(SEPARATE) && !this.fileExportModel.equals(INCORPORATE)){
this.showHelp();
System.exit(0);
}
}

private final void checkFilePath(String importPath, String exportPath){
if(importPath.equalsIgnoreCase(exportPath)){
System.err.println("输入与输出目录不能为同一目录!");
System.exit(0);
}
if(importPath.endsWith(FILE_PATH_DELIMITER)){
importPath = importPath.substring(0,importPath.length() - 1);
}
this.fileImportPath = new File(importPath);
if(!this.fileImportPath.isDirectory()){
System.err.println("\"" + importPath + "\" 不是一个有效的目录!");
System.exit(0);
}
if(exportPath.endsWith(FILE_PATH_DELIMITER)){
exportPath = exportPath.substring(0,exportPath.length() - 1);
}
this.fileExportPath = new File(exportPath);
if(!this.fileExportPath.isDirectory()){
System.err.println("\"" + exportPath + "\" 不是一个有效的目录!");
System.exit(0);
}
if(this.fileExportPath.list().length > 0){
System.err.println("输出目录必须为空目录!");
System.exit(0);
}
final StringBuilder builder = new StringBuilder();
this.recursiveDirectoryTrees(this.fileImportPath,builder);
final String[] filesPathName = builder.toString().split(";");
if(filesPathName[0].length() == 0){
System.err.println("\"" + importPath + "\" 为空目录!");
System.exit(0);
}
this.importFiles = new File[filesPathName.length];
this.filesAmount = this.importFiles.length;
if(this.fileCodeModel.equals(RECOVER) && this.fileExportModel.equals(INCORPORATE)){
if(this.hasSubDirectories || this.filesAmount % 2 != 0){
System.err.println("\"" + this.fileImportPath + "\" 目录中含有子目录或密码文件与密码信息文件("
+ CODEINFO + ")数量不等!");
System.exit(0);
}
}
for(int index = 0; index < this.filesAmount; index++){
this.importFiles[index] = new File(filesPathName[index]);
}
if(this.fileCodeModel.equals(RECOVER) && this.fileExportModel.equals(INCORPORATE)){
int position;
int infoFilesCount = 0;
int contentFilesCount = 0;
for(position = 0; position < this.filesAmount; position++){
if(this.importFiles[position].getName().endsWith(CODEINFO)){
infoFilesCount++;
}else{
contentFilesCount++;
}
}
if(infoFilesCount != contentFilesCount){
System.err.println("\"" + this.fileImportPath + "\" 目录中密码文件与密码信息文件(" + CODEINFO
+ ")数量不等!");
System.exit(0);
}
this.importContentFiles = new File[contentFilesCount];
this.importInfoFiles = new File[infoFilesCount];
contentFilesCount = 0;
infoFilesCount = 0;
for(position = 0; position < this.filesAmount; position++){
if(this.importFiles[position].getName().endsWith(CODEINFO)){
for(int pointer = 0; pointer < this.filesAmount; pointer++){
if(pointer != position
&& this.importFiles[position].getName().startsWith(this.importFiles[pointer].getName())
&& this.importFiles[position].getName().length() == this.importFiles[pointer].getName().length()
+ CODEINFO.length()){
this.importInfoFiles[infoFilesCount++] = this.importFiles[position];
this.importContentFiles[contentFilesCount++] =
this.importFiles[pointer];
}
}
}
}
if(infoFilesCount != this.filesAmount / 2){
System.err.println("\"" + this.fileImportPath + "\" 目录中密码文件与密码信息文件(" + CODEINFO
+ ")名称不匹配!");
System.exit(0);
}
}
}

private final void recursiveDirectoryTrees(File root, final StringBuilder builder){
File[] files = root.listFiles();
if(files != null){
for(int pointer = 0; pointer < files.length; pointer++){
if(files[pointer].isDirectory()){
this.hasSubDirectories = true;
this.recursiveDirectoryTrees(files[pointer],builder);
}else{
builder.append(files[pointer].getPath() + ";");
}
}
}
}

public final String getCodeModel(){
return this.fileCodeModel;
}

public final String getExportModel(){
return this.fileExportModel;
}

public final File getFileImportPath(){
return this.fileImportPath;
}

public final File getFileExportPath(){
return this.fileExportPath;
}

public final File[] getImportFiles(){
return this.importFiles;
}

public final File[] getImportContentFiles(){
return this.importContentFiles;
}

public final File[] getImportInfoFiles(){
return this.importInfoFiles;
}

public final int getFilesAmount(){
return this.filesAmount;
}
}

// 数据处理类,提供了此程序的输入输出数据的方法
private static final class StreamManager{
public static final int BLOCK_SIZE;
public static final byte BLOCK_DELIMITER;
public static final int INFO_SIZE;
private RandomAccessFile contentReader;
private RandomAccessFile contentWriter;
private RandomAccessFile infoReader;
private RandomAccessFile infoWriter;
private byte[] contentBytes;
private int contentBytesAmount;
private byte[] infoBytes;
private int infoBytesAmount;
private int bytesCount;
private String blockInfo;
private int blockAmount;
private int theLastBlockSize;
static{
BLOCK_SIZE = 1048576;
BLOCK_DELIMITER = 0;
INFO_SIZE = 512;
}

public StreamManager(){
this.contentReader = null;
this.contentWriter = null;
this.infoReader = null;
this.infoWriter = null;
this.contentBytes = new byte[BLOCK_SIZE];
this.contentBytesAmount = 0;
this.infoBytes = new byte[INFO_SIZE];
this.infoBytesAmount = 0;
this.bytesCount = 0;
this.blockInfo = "";
this.blockAmount = 0;
this.theLastBlockSize = 0;
}

public final void createContentReader(File file){
try{
this.contentReader = new RandomAccessFile(file,"r");
}catch(IOException e){
e.printStackTrace();
}
}

public final void createInfoReader(File file){
try{
this.infoReader = new RandomAccessFile(file,"r");
}catch(IOException e){
e.printStackTrace();
}
}

public final void createContentWriter(File file){
try{
file.createNewFile();
this.contentWriter = new RandomAccessFile(file,"rw");
}catch(IOException e){
e.printStackTrace();
}
}

public final void createInfoWriter(File file){
try{
file.createNewFile();
this.infoWriter = new RandomAccessFile(file,"rw");
}catch(IOException e){
e.printStackTrace();
}
}

public final boolean readContent(){
try{
this.contentBytesAmount = this.contentReader.read(this.contentBytes);
if(this.contentBytesAmount < 0){
return false;
}
}catch(IOException e){
e.printStackTrace();
}
return true;
}

public final boolean readContent(int bytesAmount){
try{
this.contentBytesAmount = this.contentReader.read(this.contentBytes,0,bytesAmount);
if(this.contentBytesAmount < 0){
return false;
}
}catch(IOException e){
e.printStackTrace();
}
return true;
}

public final boolean readInfo(){
try{
this.infoBytesAmount = this.infoReader.read(this.infoBytes);
if(this.infoBytesAmount < 0){
return false;
}
this.infoBytesToBlockInfo();
}catch(IOException e){
e.printStackTrace();
}
return true;
}

private final void infoBytesToBlockInfo(){
StringBuilder builder = new StringBuilder();
int position = 0;
for(this.bytesCount = 0;; this.bytesCount++){
if(this.infoBytes[this.bytesCount] == BLOCK_DELIMITER){
builder.append(new String(this.infoBytes,position,this.bytesCount - position));
if(this.infoBytes[this.bytesCount + 1] == BLOCK_DELIMITER){
this.blockInfo = builder.toString();
break;
}else{
builder.append(ParameterManager.FILE_PATH_DELIMITER);
position = this.bytesCount + 1;
}
}
}
this.bytesCount += 2;
position = this.bytesCount;
for(; this.infoBytes[this.bytesCount] != BLOCK_DELIMITER; this.bytesCount++)
;
this.blockAmount =
Integer.parseInt(new String(this.infoBytes,position,this.bytesCount - position));
this.bytesCount += 2;
position = this.bytesCount;
for(; this.infoBytes[this.bytesCount] != BLOCK_DELIMITER; this.bytesCount++)
;
this.theLastBlockSize =
Integer.parseInt(new String(this.infoBytes,position,this.bytesCount - position));
}

public final void writeContent(){
try{
this.contentWriter.write(this.contentBytes,0,this.contentBytesAmount);
}catch(IOException e){
e.printStackTrace();
}
}

public final void writeInfo(String blockInfo, int blockAmount, int theLastBlockSize){
try{
this.blockInfoToInfoBytes(blockInfo,blockAmount,theLastBlockSize);
this.infoWriter.write(this.infoBytes);
}catch(IOException e){
e.printStackTrace();
}
}

private final void blockInfoToInfoBytes(String blockInfo, int blockAmount, int theLastBlockSize){
int pointer = 0;
byte[] bytes = blockInfo.getBytes();
byte delimiter = ParameterManager.FILE_PATH_DELIMITER.getBytes()[0];
for(this.bytesCount = 0; pointer < bytes.length; pointer++){
this.infoBytes[this.bytesCount++] = (bytes[pointer] == delimiter?0:bytes[pointer]);
}
for(pointer = 0; pointer < 2; pointer++){
this.infoBytes[this.bytesCount++] = BLOCK_DELIMITER;
}
bytes = Integer.toString(blockAmount).getBytes();
for(pointer = 0; pointer < bytes.length; pointer++){
this.infoBytes[this.bytesCount++] = bytes[pointer];
}
for(pointer = 0; pointer < 2; pointer++){
this.infoBytes[this.bytesCount++] = BLOCK_DELIMITER;
}
bytes = Integer.toString(theLastBlockSize).getBytes();
for(pointer = 0; pointer < bytes.length; pointer++){
this.infoBytes[this.bytesCount++] = bytes[pointer];
}
for(; this.bytesCount < INFO_SIZE; this.bytesCount++){
this.infoBytes[this.bytesCount] = BLOCK_DELIMITER;
}
}

public final void closeReadersAndWriters(){
this.closeWriters();
this.closeReaders();
}

public final void closeReaders(){
this.closeContentReader();
this.closeInfoReader();
}

public final void closeContentReader(){
try{
if(this.contentReader != null){
this.contentReader.close();
this.contentReader = null;
}
}catch(IOException e){
e.printStackTrace();
}
}

public final void closeInfoReader(){
try{
if(this.infoReader != null){
this.infoReader.close();
this.infoReader = null;
}
}catch(IOException e){
e.printStackTrace();
}
}

public final void closeWriters(){
this.closeContentWriter();
this.closeInfoWriter();
}

public final void closeContentWriter(){
try{
if(this.contentWriter != null){
this.contentWriter.close();
this.contentWriter = null;
}
}catch(IOException e){
e.printStackTrace();
}
}

public final void closeInfoWriter(){
try{
if(this.infoWriter != null){
this.infoWriter.close();
this.infoWriter = null;
}
}catch(IOException e){
e.printStackTrace();
}
}

public final byte[] getContentBytes(){
return this.contentBytes;
}

public final int getContentBytesAmount(){
return this.contentBytesAmount;
}

public final String getBlockInfo(){
return this.blockInfo;
}

public final int getBlockAmount(){
return this.blockAmount;
}

public final int getTheLastBlockSize(){
return this.theLastBlockSize;
}
}

// 时间管理类,负责统计处理的每个文件的耗时,精确到毫秒
private static final class TimerManager{
public static final String HOUR;
public static final String MINUTE;
public static final String SECOND;
public static final String MILLISECOND;
private long thisTime;
private long totalTime;
private long hour;
private long minute;
private long second;
private long millisecond;
private String timerReport;
static{
HOUR = "小时";
MINUTE = "分";
SECOND = "秒";
MILLISECOND = "毫秒";
}

public TimerManager(){
this.thisTime = 0l;
this.totalTime = 0l;
this.hour = 0l;
this.minute = 0l;
this.second = 0l;
this.millisecond = 0l;
this.timerReport = "";
}

public final String countThisTime(){
this.thisTime = System.currentTimeMillis() - this.thisTime;
this.totalTime += this.thisTime;
this.countTime(this.thisTime);
return this.timerReport;
}

public final String countTotalTime(){
this.countTime(this.totalTime);
return this.timerReport;
}

private final void countTime(long time){
this.resetTimer();
if(time >= 3600000l){
this.hour = time / 3600000l;
this.minute = (time - this.hour * 3600000l) / 60000l;
this.second = (time - this.hour * 3600000l - this.minute * 60000l) / 1000l;
this.millisecond =
time - this.hour * 3600000l - this.minute * 60000l - this.second * 1000l;
}else if(time >= 60000l){
this.minute = time / 60000l;
this.second = (time - this.minute * 60000l) / 1000l;
this.millisecond = time - this.minute * 60000l - this.second * 1000l;
}else if(time >= 1000l){
this.second = time / 1000l;
this.millisecond = time - this.second * 1000l;
}else{
this.millisecond = time;
}
this.timerReport =
this.hour + HOUR + this.minute + MINUTE + this.second + SECOND + this.millisecond
+ MILLISECOND;
}

private final void resetTimer(){
this.hour = 0l;
this.minute = 0l;
this.second = 0l;
this.millisecond = 0l;
}

public final long getThisTime(){
return this.thisTime;
}

public final void setThisTime(long thisTime){
this.thisTime = thisTime;
}

public final long getTotalTime(){
return this.totalTime;
}

public final void setTotalTime(long totalTime){
this.totalTime = totalTime;
}
}

// 实体管理类,给出了所有基于二叉实体树的方法
private static final class EntityManager{
private final EntityTree[] entities;
private int entitiesCount;
private final byte[] bytes;

public EntityManager(byte[] contentBytes){
this.bytes = contentBytes;
this.entities = new EntityTree[this.bytes.length];
this.entities[0] = EntityTree.root;
for(int i = 1; i < this.bytes.length; i++){
this.entities[i] = new EntityTree();
}
}

// 加密实体
public final void encryptEntities(EntityTree treeRoot, int entitiesAmount){
this.createEntityTree(treeRoot,entitiesAmount);
this.recursiveEncrypt(treeRoot);
}

// 解密实体
public final void recoverEntities(EntityTree treeRoot, int entitiesAmount){
this.createEntityTree(treeRoot,entitiesAmount);
this.recursiveRecover(treeRoot);
this.entitiesContentToBytes(EntityTree.root);
}

// 创建实体树
private final void createEntityTree(EntityTree treeRoot, int entitiesAmount){
this.entitiesCount = 0;
if(entitiesAmount == 1){
treeRoot.reset(this.bytes[0]);
return;
}
treeRoot.setContent(this.bytes[0]);
this.setLeftEntity();
if(entitiesAmount > 2){
this.setRightEntity();
}
for(this.entitiesCount = 1; 2 * this.entitiesCount + 2 < entitiesAmount; this.entitiesCount++){
this.setLeftEntity();
this.setRightEntity();
}
if(2 * this.entitiesCount + 1 < entitiesAmount){
this.setLeftEntity();
}
for(this.entitiesCount = 0; this.entitiesCount < entitiesAmount - 1; this.entitiesCount++){
this.entities[this.entitiesCount].setLink(this.entities[this.entitiesCount + 1]);
}
this.entitiesCount = 0;
}

// 初始化左孩子实体
private final void setLeftEntity(){
this.entities[this.entitiesCount].setLeft(this.entities[2 * this.entitiesCount + 1]);
this.entities[2 * this.entitiesCount + 1].reset(this.bytes[2 * this.entitiesCount + 1]);
}

// 初始化右孩子实体
private final void setRightEntity(){
this.entities[this.entitiesCount].setRight(this.entities[2 * this.entitiesCount + 2]);
this.entities[2 * this.entitiesCount + 2].reset(this.bytes[2 * this.entitiesCount + 2]);
}

// 递归加密
private final void recursiveEncrypt(EntityTree entity){
if(entity != null){
this.recursiveEncrypt(entity.getLeft());
this.bytes[this.entitiesCount++] = entity.getContent();
this.recursiveEncrypt(entity.getRight());
}
}

// 递归解密
private final void recursiveRecover(EntityTree entity){
if(entity != null){
this.recursiveRecover(entity.getLeft());
entity.setContent(this.bytes[this.entitiesCount++]);
this.recursiveRecover(entity.getRight());
}
}

// 导出实体内容到字节数组
private final void entitiesContentToBytes(EntityTree treeRoot){
EntityTree entity = treeRoot;
for(this.entitiesCount = 0; entity != null; entity = entity.getLink()){
this.bytes[this.entitiesCount++] = entity.getContent();
}
}

public final byte[] getBytes(){
return this.bytes;
}
}

// 二叉实体树类,此程序中用来加密解密文件的数据结构
private static final class EntityTree{
// 树根节点
public static final EntityTree root;
// 左孩子节点
private EntityTree left;
// 右孩子节点
private EntityTree right;
// 按照层序遍历方式的当前节点的下一个节点
private EntityTree link;
// 树节点的数据内容
private byte content;
static{
root = new EntityTree();
}

public final void reset(byte content){
this.left = null;
this.right = null;
this.link = null;
this.content = content;
}

public final EntityTree getLeft(){
return this.left;
}

public final void setLeft(EntityTree left){
this.left = left;
}

public final EntityTree getRight(){
return this.right;
}

public final void setRight(EntityTree right){
this.right = right;
}

public final EntityTree getLink(){
return link;
}

public final void setLink(EntityTree link){
this.link = link;
}

public final byte getContent(){
return this.content;
}

public final void setContent(byte content){
this.content = content;
}
}
}

你可能感兴趣的:(数据结构)