📄 fileadapter.java
字号:
/*
* FileAdapter.java
*
* Created on 2007骞?1鏈?3鏃? 2:47
*
* To change this template, choose Tools | Template Manager
* and open the template infile the editor.
*/
package Compressor;
import java.io.*;
import java.util.*;
/**
* file adapter will normalize binary code to numbers in the range between 0.1 * Range and 0.9 * Range.
* Or translate back.
* @author yuhui_bear
*/
public class FileAdapter {
private BufferedInputStream infile;
private BufferedOutputStream outfile;
private long groupSize;
private long filelength;
private long orglength;
private int dividedSize;
// private int writeCnt=0;
private final int VALUE =4;
private double Range =1 * 0.8D;
private double BASE = 1 * 0.1258D;
private double enDIF = Range / VALUE;
private double deDIF = Range / (VALUE *2 );
private int BIT=2;
/**
* Creates a new instance of FileAdapter
* @param inf work file.
* @param ds the size of data array.
* @param encode TRUE ,work in encoding mode.
* FALSE, work in decoding mode.
* @throws java.io.IOException If IOException encountered ,IOException was thrown.
*/
public FileAdapter(File inf , int ds ,boolean encode) throws IOException{
dividedSize = ds;
String path = inf.getPath().substring(0 , inf.getPath().length()-inf.getName().length());
infile = new BufferedInputStream(new FileInputStream(inf));
//write length of the original file.
if(encode){
filelength = inf.length();
outfile =new BufferedOutputStream(new FileOutputStream(new File(path,inf.getName().replaceAll("\\.\\w{1,3}",".nc"))));
DataOutputStream tout =new DataOutputStream(outfile);
tout.writeUTF(inf.getName());
tout.writeLong(filelength);
orglength = filelength / BIT;
if(filelength %2 !=0){
orglength ++;
}
}else{
//decode.
DataInputStream tin =new DataInputStream(infile);
String outname = tin.readUTF();
orglength = tin.readLong();
filelength = inf.length()- 8 -2- outname.length();
outfile =new BufferedOutputStream(new FileOutputStream(new File(path,outname)));
}
if(dividedSize == 4){
groupSize=filelength ;
}else{
groupSize=(long)((double)filelength / (double)dividedSize / 4D) +1;
}
}
/**
* get numbers of group.
* @return how many group the source data divided to.
*/
public long getGroupSize(){
return groupSize;
}
/**
* get next group values needed to encode or decode.
* @return ,an array of adjusted values.
*/
public double[] nextGroup(){
double[] source = new double[dividedSize];
Arrays.fill(source,-1);
int cnt=0;
try {
for(int i = 0;i<dividedSize;i +=4){
cnt = infile.read();
//reach end of the file.
filelength --;
if( filelength <0){
infile.close();
return source;
}
source[i]=cnt; //highest 2 bites.
source[i+1]=cnt;
source[i+2]=cnt; //lowest 2 bites.
source[i+3]=cnt;
source[i] = (((int)source[i] >>>6 & 0x00000003))* enDIF + BASE;
source[i+1] = (((int)source[i+1] >>>4& 0x00000003))* enDIF + BASE;
source[i+2] = (((int)source[i+2] >>>2& 0x00000003))* enDIF + BASE;
source[i+3] = (((int)source[i+3] & 0x00000003))* enDIF + BASE;
}
} catch (IOException ex) {
// ex.printStackTrace();
return null;
}
return source;
}
/**
* get next byte value.
* @return next byte.
*/
public int nextByte(){
int source =0;
try {
for(int i = 0;i<dividedSize;i++){
source =infile.read();
//reach end of the file.
if( source <0){
infile.close();
return -1;
}
}
} catch (IOException ex) {
ex.printStackTrace();
return -1;
}
return source;
}
/**
* write encoded or decoded data to file.
* @param ind , data to write between 0.1 * Range ~ 0.9 * Range.
* @return ,if succeed 0, error -1,IOException -2.
*/
public int writeBack(double[] ind){
if(ind ==null){
try {
outfile.close();
} catch (IOException ex) {
ex.printStackTrace();
return -1;
}
return 1;
}else{
if(ind.length % 4 == 0){
try {
// if(writeCnt !=0){
//low 4 bites gained.write back.
// for (int id =0;id<ind.length;id +=2){
orglength -= ind.length / 4;
for(int iw =0;iw < ind.length / 4;iw++){
outfile.write(((int)(((ind[iw + 3] - BASE)/ deDIF +1)/2 ) <<2 & 0x000000C0) //lowest bit.
| ((int)(((ind[iw + 2] - BASE)/ deDIF +1)/2 ) & 0x00000030)
| ((int)(((ind[iw + 1] - BASE)/ deDIF +1)/2 ) <<6 & 0x000000C0)
| ((int)(((ind[iw] - BASE)/ deDIF +1)/2 ) << 4 & 0x00000030)); //highest bit.
}
// writeCnt=0;
if(orglength <1){
try {
outfile.close();
} catch (IOException ex) {
ex.printStackTrace();
return -1;
}
return 2;
}
// }//for end.
// }else{
//store high 4 bites.
// writeCnt=((int)(((ind[0] - BASE)/ deDIF +1)/2 ) <<6 & 0x000000C0) | ((int)(((ind[1] - BASE)/ deDIF +1)/2 ) << 4 & 0x00000030);
// }
} catch (IOException ex) {
ex.printStackTrace();
return -2;
}
}else{
return -1;
}
}
return 1;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -