📄 bufferedfileadapter.java
字号:
/*
* BufferedFileAdapter.java
*
* Created on 2007年12月6日, 上午6:47
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package Compressor;
import java.io.*;
import java.util.*;
/**
* buffered file adapter.
* @author yuhui_bear
*/
public class BufferedFileAdapter {
private FileAdapter source;
private LinkedList<LinkedList<DoubleBuffer>> filebuf;
private Iterator<DoubleBuffer> dataIterator;
private Iterator<LinkedList<DoubleBuffer>> groupIterator;
private long group=0;
private long curCnt=0;
/**
* Creates a new instance of BufferedFileAdapter
* @param sfile the file to be buffered and adapted.
* @param ds the size of the output data array.
* @param encode TRUE , work in encode mode.
* @throws java.io.IOException If there is no such file or other errors about I/O system, the IOException will be thrown.
*/
public BufferedFileAdapter(File sfile, int ds ,boolean encode) throws IOException{
LinkedList<DoubleBuffer> filebufSubgourp = new LinkedList();
filebuf = new LinkedList();
filebuf.add(filebufSubgourp);
int cnt=0;
double[] data ;
try {
source = new FileAdapter(sfile,ds,encode);
data = source.nextGroup();
while(data != null){
cnt++;
group++;
filebufSubgourp.add(new DoubleBuffer(data));
if(cnt > 4096){
//new subgroup.
filebufSubgourp = new LinkedList();
filebuf.add(filebufSubgourp);
cnt=0;
}
data = source.nextGroup();
}
dataIterator = filebuf.get(0).iterator();
groupIterator = filebuf.iterator();
} catch (IOException ex) {
ex.printStackTrace();
throw new IOException();
}
}
/**
* return how many batches the file divided.
* @return return how many batches the file divided.
*/
public int size(){
return filebuf.size();
}
/**
* get the size of batch.
* @return numbers of group included in per batch.
*/
public int[] subGroupSize(){
int[] temp = new int[filebuf.size()];
for (int i =0 ; i< filebuf.size();i++){
temp[i] = filebuf.get(i).size();
}
return temp;
}
/**
* get next group data.
* @return one group of data , Normalized.
*/
public double[] nextGroup(){
if(dataIterator.hasNext()){
curCnt ++;
return dataIterator.next().data;
}else{
if(groupIterator.hasNext()){
dataIterator = groupIterator.next().iterator();
if(dataIterator.hasNext()){
curCnt ++;
return dataIterator.next().data;
} //get data in new group.
} //check next gourp.
} //search next gourp.
return null;
}
/**
* reset iterator to the starting point.
*/
public void resetIterator(){
groupIterator = filebuf.iterator();
dataIterator = filebuf.get(0).iterator();
}
/**
* inqure progress by present.
* @return int number between 0 ~100
*/
public int getProgress (){
return (int)(100 * curCnt / group);
}
/**
* write back data the file.The data will not be instantly writen until buffer managed by JVM was ful.
* @param toWrite data to be writen.
* @return -1,write error.
* -2,close error.
* 1,work done.
* 2,close stream succeed by file size.
*/
public int writeGroup(double[] toWrite){
return source.writeBack(toWrite);
}
/**
*box of double arrays.
*/
class DoubleBuffer {
public double[] data;
public DoubleBuffer(double[] indouble){
data =indouble;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -