package com.reandroid.lib.arsc.array; import com.reandroid.lib.arsc.base.Block; import com.reandroid.lib.arsc.base.BlockArray; import com.reandroid.lib.arsc.base.BlockCounter; import com.reandroid.lib.arsc.io.BlockLoad; import com.reandroid.lib.arsc.io.BlockReader; import com.reandroid.lib.arsc.item.ByteArray; import com.reandroid.lib.arsc.item.IntegerArray; import com.reandroid.lib.arsc.item.IntegerItem; import java.io.IOException; import java.io.OutputStream; public abstract class OffsetBlockArray extends BlockArray implements BlockLoad { private final IntegerArray mOffsets; private final IntegerItem mItemStart; private final IntegerItem mItemCount; private final ByteArray mEnd4Block; private byte mEnd4Type; public OffsetBlockArray(IntegerArray offsets, IntegerItem itemCount, IntegerItem itemStart){ super(); this.mOffsets=offsets; this.mItemCount=itemCount; this.mItemStart=itemStart; this.mEnd4Block=new ByteArray(); mItemCount.setBlockLoad(this); } void setEndBytes(byte b){ this.mEnd4Type=b; this.mEnd4Block.fill(b); } @Override public void clearChildes(){ super.clearChildes(); mOffsets.clear(); mItemStart.set(0); mItemCount.set(0); mEnd4Block.clear(); } @Override public int countBytes(){ int result=super.countBytes(); int endCount=mEnd4Block.countBytes(); return result+endCount; } @Override public void onCountUpTo(BlockCounter counter){ super.onCountUpTo(counter); if(counter.FOUND){ return; } mEnd4Block.onCountUpTo(counter); } @Override public byte[] getBytes(){ byte[] results=super.getBytes(); if(results==null){ return null; } byte[] endBytes=mEnd4Block.getBytes(); results=addBytes(results, endBytes); return results; } @Override public int onWriteBytes(OutputStream stream) throws IOException { int result=super.onWriteBytes(stream); if(result==0){ return 0; } result+=mEnd4Block.writeBytes(stream); return result; } @Override protected void onRefreshed() { int count=childesCount(); mOffsets.setSize(count); T[] childes=getChildes(); int sum=0; if(childes!=null){ int max=childes.length; for(int i=0;imaxPos){ maxPos=pos; } } reader.seek(maxPos); refreshEnd4Block(reader, mEnd4Block); } @Override public void onBlockLoaded(BlockReader reader, Block sender) throws IOException { if(sender==mItemCount){ int count=mItemCount.get(); setChildesCount(count); mOffsets.setSize(count); } } @Override public String toString(){ StringBuilder builder=new StringBuilder(); builder.append(getClass().getSimpleName()); builder.append(": count = "); int s= childesCount(); builder.append(s); int count=mItemCount.get(); if(s!=count){ builder.append(", countValue="); builder.append(count); } builder.append(", start="); builder.append(mItemStart.get()); return builder.toString(); } }