⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cliprocessor.java

📁 Java version of ABC/HR comparator v0.5. by schnofler. Runs on Sun JRE 1.5 or later
💻 JAVA
字号:
package abchr.audio;

import abchr.InputStreamEater;
import abchr.XMLable;

import java.io.*;
import java.util.zip.Checksum;
import java.util.zip.Adler32;
import java.util.Arrays;
import java.util.Map;
import java.util.HashMap;
import java.util.ArrayList;

import org.jdom.Element;

public class CLIProcessor implements SampleProcessor, Serializable, XMLable {
	private static final File tempdir=new File("."+File.separatorChar+"abchrtemp");

	private static int computeChecksum(File f) throws IOException {
		FileInputStream stream=new FileInputStream(f);
		Checksum checksum=new Adler32();
		byte[] buffer=new byte[4096];
		int read;
		while(stream.available()>0) {
			read=stream.read(buffer);
			checksum.update(buffer,0,read);
		}
		return (int)checksum.getValue();
	}

	private String outputExtension;
	// commandLine==cmdLine.split("\\s");
	private String cmdLine;
	private String[] commandLine;
	private String[] acceptExts;

	public boolean accept(File f) {
		final String name=f.getName();
		for(int i=0;i<acceptExts.length;i++) {
			if(name.length()>=acceptExts[i].length() && name.substring(name.length()-acceptExts[i].length()).equalsIgnoreCase(acceptExts[i])){return true;}
		}
		return false;
	}

	private String replace(String s,String target,String replacement) {
		for(int i=s.indexOf(target);i!=-1;i=s.indexOf(target)) {
			s=s.substring(0,i)+replacement+(i+target.length()<s.length()?s.substring(i+target.length()):"");
		}
		return s;
	}

	public File process(File f) throws DecodeFailedException {
		//System.out.println("in decode()");
		int checksum=0;
		if(f.getParentFile().equals(tempdir)) {
			checksum=f.getName().hashCode();
		} else {
			try {
				checksum=computeChecksum(f);
			} catch(IOException e) {
				throw new DecodeFailedException("I/O error while processing "+f.getName()+": "+e.getMessage());
			}
		}
		for(int i=0;i<commandLine.length;i++) {
			checksum=31*checksum+commandLine[i].hashCode();
		}
		if(!tempdir.exists()){tempdir.mkdir();}
		String outputFileName=/*"."+File.separatorChar+*/"abchrtemp"+File.separatorChar+"tmp"+Integer.toString(checksum,36)+outputExtension;
		File decodedFile=new File(outputFileName);
		if(decodedFile.exists()) {
			//System.out.println("result of "+Arrays.toString(commandLine)+" on "+f+" already exists.");
			return decodedFile;
		}
		String inFile=/*"\""+*/f.getAbsolutePath()/*+"\""*/;
		String outFile=/*"\""+*/outputFileName/*+"\""*/;
		String[] cmd=(String[])commandLine.clone();
		for(int i=0;i<cmd.length;i++) {
			cmd[i]=replace(cmd[i],"%i",inFile);
			cmd[i]=replace(cmd[i],"%o",outFile);
		}
		/*System.out.print("cmdarray: ");
		for(int i=0;i<cmd.length;i++){System.out.print(cmd[i]+" ");}
		System.out.println();*/
		try {
			Process p=Runtime.getRuntime().exec(cmd);
			InputStreamEater inEater=new InputStreamEater(p.getInputStream());
			InputStreamEater errEater=new InputStreamEater(p.getErrorStream());
			new Thread(inEater).start();
			new Thread(errEater).start();
			try {
				p.waitFor();
				//System.out.println("done");
			} catch(InterruptedException e) {
				//System.out.println("interrupt");
				e.printStackTrace();
			}
			inEater.stop();
			errEater.stop();
			if(p.exitValue()!=0) {
				throw new DecodeFailedException("Call to "+commandLine[0]+" failed.");
			}
			decodedFile.deleteOnExit();
			return decodedFile;
		} catch(IOException e) {
			throw new DecodeFailedException("I/O error while processing "+f.getName()+": "+e.getMessage());
		}
	}

	public CLIProcessor(String commandLine,String outExt,String[] acceptExts) throws IllegalArgumentException {
		this.outputExtension='.'+outExt;
		commandLine=commandLine.trim();
		this.cmdLine=commandLine;
		String command="";
		if(commandLine.charAt(0)=='\"') {
			command=commandLine.substring(0,commandLine.indexOf('\"',1)+1);
		} else {
			command=commandLine.substring(0,commandLine.indexOf(' '));
		}
		commandLine=commandLine.substring(command.length());
		String[] params=commandLine.trim().split("\\s");
		this.commandLine=new String[params.length+1];
		this.commandLine[0]=command;
		for(int i=0;i<params.length;i++){this.commandLine[i+1]=params[i].trim();}
		if(commandLine.indexOf("%i")==-1 || commandLine.indexOf("%o")==-1) {
			throw new IllegalArgumentException();
		}
		this.setAcceptedExtensions(acceptExts);
	}

	public void setAcceptedExtensions(String[] exts) {
		this.acceptExts=new String[exts.length];
		for(int i=0;i<exts.length;i++){this.acceptExts[i]='.'+exts[i].trim();}
	}

	public CLIProcessor(String commandLine,String outExt) throws IllegalArgumentException {
		this(commandLine,outExt,new String[]{"wav"});
	}

	public String getCommandLine(){return cmdLine;}
	public String getOutputExtension(){return outputExtension;}

	public String toString() {
		return cmdLine;
	}

	public Element toXML() {
		Element e=new Element("cli");
		e.addContent(new Element("cmdline").setText(cmdLine));
		e.addContent(new Element("outext").setText(outputExtension.substring(1)));
		StringBuilder acceptExtsString=new StringBuilder(acceptExts.length*4);
		for(int i=0;i<acceptExts.length;i++) {
			if(i!=0){acceptExtsString.append(',');}
			acceptExtsString.append(acceptExts[i].substring(1));
		}
		e.addContent(new Element("acceptexts").setText(acceptExtsString.toString()));
		return e;
	}

	public static CLIProcessor construct(Element e) {
		return new CLIProcessor(e.getChildTextTrim("cmdline"),e.getChildTextTrim("outext"),e.getChildTextTrim("acceptexts").split(","));
	}

	public static CLIProcessor makeFromString(String s) {
		String[] split=s.split(">");
		if(split.length>3){throw new IllegalArgumentException();}
		if(split.length==1) {
			String[] spl=s.trim().split("\\s",2);
			if(shortcuts.containsKey(spl[0])) {
				return makeFromString(shortcuts.get(spl[0]).replace("%p",spl[1]));
			} else {
				throw new IllegalArgumentException();
			}
		}
		CLIProcessor p;
		if(split.length==2) {
			p=new CLIProcessor(split[0].trim(),split[1].trim());
		} else {
			p=new CLIProcessor(split[1].trim(),split[2].trim(),split[0].split(","));
		}
		return p;
	}

	public String[] getAcceptedExtensions() {
		String[] res=new String[acceptExts.length];
		System.arraycopy(acceptExts,0,res,0,acceptExts.length);
		return res;
	}

	private static Map<String,String> shortcuts=new HashMap<String,String>(10);

	static {
		File f=new File("cliapps.cfg");
		if(f.exists()) {
			try {
				BufferedReader reader=new BufferedReader(new InputStreamReader(new FileInputStream(f)));
				String line;
				while((line=reader.readLine())!=null) {
					line=line.trim();
					if(line.equals("")){continue;}
					if(line.indexOf('=')==-1) {
						System.out.println("This line from cliapps.cfg is faulty: "+line);
						continue;
					}
					String shortcut=line.substring(0,line.indexOf('=')).trim();
					shortcuts.put(shortcut,line.substring(line.indexOf('=')+1,line.length()));
				}
				//System.out.println(shortcuts);
			} catch(IOException e) {
				e.printStackTrace();
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -