📄 riad3.java
字号:
import java.io.IOException;
import java.io.RandomAccessFile;
class Raid3 implements Raid {
String inFileName;
String outFileName;
RandomAccessFile in;
RandomAccessFile out[];
int startdisk=0;
int checkdisk;
int n;
int length;
int CAPACITY;
public int[] capRemain;
int[] waste;
byte[] cbuf;
int ReadAble;
public boolean Readable()
{
return(ReadAble!=-1);
}
public boolean Storeable()
{
boolean a=true;
for(int i=0;i<n;i++)
{
if(capRemain[i]-waste[i]<length)
{
a=false;
}
if(a==false)System.out.println ("磁盘"+i+"空间不足,整条将不再写入");
}
return a;
}
public int Capcity()
{
return CAPACITY;
}
public void capcity_Remained(int[]a)
{
for(int i=0;i<n;i++)
{
a[i]=capRemain[i];
}
}
public int Cap_available()
{
int a=0;
for(int i=0;i<n;i++)
{
a+=capRemain[i]-waste[i];
}
return a*(n-1)/n;
}
public void ReadBlock() throws IOException
{
ReadAble=in.read(cbuf,0,(n-1)*length);
if(ReadAble==-1){System.out.println("文件结束或不足一条,读入终止");}
//这一行以后转为throw exception
}
public void StoreBlock()throws IOException{
int id =startdisk;
int begin=0;
Cal ca=new Cal();
byte[] xor_check = new byte[length];
ca.Init(xor_check, length);
for (int i=0;i<n;i++)
{
if (capRemain[id]-waste[id] < length) {
System.out.println("磁盘" + id + "容量不足,写入失败");
break;
}
if (id == checkdisk) {
out[id].write(xor_check);
capRemain[id] -= length;
System.out.println("磁盘" + id + "写入校验");
//checkdisk = ((checkdisk==0)?n-1:(checkdisk - 1));
ca.Init(xor_check, length);// 清为全零
break;
}
out[id].write(cbuf,begin,length);
System.out.println("磁盘" + id + "写入数据");
capRemain[id] -= length;
System.out.println("磁盘" + id + "剩余容量为" + capRemain[id]);
begin += length;
//startdisk=(startdisk+1)%n;
xor_check = ca.Xor(cbuf,length,n-1);
id = (id + 1) % n;
//System.out.println("id:" + id + "begin:" + begin + "check" + check);
}
}
public void Recover(int downID) throws IOException
{
int key=0;
Cal cacu=new Cal();
RandomAccessFile[] bac=new RandomAccessFile[n];
byte[] temp=new byte[1];//防止恢复是出猥琐的错误 单字节恢复而不是单块恢复
byte[] result =new byte[1];
cacu.Init(result, 1);
for(int i=0;i<n;i++)
{
if(i!=downID)
bac[i]= new RandomAccessFile("" +i+ "_"+outFileName,"r");
}
RandomAccessFile rec= new RandomAccessFile("" +downID+ "_"+"RECOVER_"+outFileName,"rw");
while(key!=-1)
{
for(int i=0;i<n;i++)
{
if(i!=downID)
{
key= bac[i].read(temp);
if(key!=-1)
result=cacu.Xor(result, temp, 1);
}
}
if(key!=-1)
{
rec.write(result);
cacu.Init(result, 1);
}
}
}
public Raid3(String inf,String outf,int n,int l,int[]cap) throws IOException
{
Cal c=new Cal();
int min=c.Min(cap, n);
cbuf=new byte[(n-1)*l];
inFileName=inf;
outFileName=outf;
checkdisk=n-1;
this.n=n;
capRemain=new int[n];
for(int i=0;i<n;i++)
{
capRemain[i]=cap[i];
System.out.println("磁盘"+i+"剩余容量为"+capRemain[i]);
}
length=l;
in= new RandomAccessFile(inf,"r");
out = new RandomAccessFile[n];
for(int i=0;i<n;i++)
out[i] = new RandomAccessFile ("" + i + "_" +outFileName,"rw");
CAPACITY=min/length*length*(n-1);
waste=new int[n];
for(int i=0;i<n;i++)
{
waste[i]=cap[i]-min/length*length;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -