一個byte緩衝--用於圖片或視頻

來源:互聯網
上載者:User

標籤:des   style   http   os   資料   io   

public class ImageCache {


private List<byte[]> list = new ArrayList<byte[]>(); //儲存所有部分緩衝

private int initSize;    //初始化大小,每次申請時都會申請等同於initSize大小的byte[] buf

private int size;    //image cache 的實際大小

public ImageCache(int initSize){

this.initSize = initSize;

byte[] buf = new byte[initSize];

list.add(buf);

}

/**

* 返回此image對象對應的byte數組

* @return

*/

public byte[] getImage(){

byte[] buf = new byte[size];

int init = size;

int index = 0;

for(byte[] temp :list){

if(init < initSize){

setElement(buf, temp, 0, init, index);

break;

}else{

init = init - initSize;

setElement(buf, temp, 0, temp.length, index);

index = index + initSize;

}

}

return buf;

}

/**

* @param dest  目標數組

* @param src  原數組

* @param start 原數組開始位置

* @param end  原數組結束位置

* @param index 目標數組開始位置

*/

private void setElement(byte[] dest,byte[] src,int start ,int end,int index){

int temp = index;

for(int i = start ; i< end ; i++){

byte value = src[i];

dest[temp] = value;

temp++;

}

}

/**

* 擷取實際的索引位置

* @return

*/

public int getIndex(){

int listSize = list.size();

int index = size - initSize * (listSize - 1 );

return index;

}

public ImageCache append(byte[] buf,int start,int end){

int length = buf.length;

if(start <0 || start >end  || end >length){

throw new RuntimeException("ImageCache append param error,start:"+start+",end:"+end+",length:"+length);

}

byte[] temp = new byte[end-start];

setElement(temp, buf, start, end, 0);

return append(temp);

}

/**

* 以追加的方式向iamge對象中添加byte映像資料

* @param buf

*/

public ImageCache append(byte[] buf){

int length = buf.length;

int listSize = list.size();

int remain = initSize * listSize - size;  //還剩餘空間

byte[] bs = list.get(listSize-1);//剩餘空間集合

if(remain >= length){//剩餘空間多餘需要的空間,直接追加

setElement(bs, buf, 0, buf.length, getIndex());

}else{//剩餘空間不足,需要申請空間

setElement(bs, buf, 0, remain, getIndex());//將之前空間填滿

int bufSzie = (length -remain)/initSize;// 計算還需要buf的個數

int startIndex = remain;  

for(int i=0 ;i < bufSzie ;i++){

byte[] apply = new byte[initSize] ;

setElement(apply, buf, startIndex, startIndex+initSize, 0);

list.add(apply);

startIndex = startIndex + initSize;

}

byte[] apply = new byte[initSize];

setElement(apply, buf, startIndex, buf.length, 0);

list.add(apply);

}

size += length;

return this;

}

public void destroy(){

this.list = null;

this.initSize =0;

this.size = 0;

}

@Override

public String toString() {

StringBuffer buf = new StringBuffer();

buf.append("initSize:"+this.initSize+",size:"+size+",list.size:"+list.size());

buf.append(";---content--:");

printBytes(this.list, buf);

return buf.toString();

}


public int getInistSize(){

return this.initSize;

}

public List<byte[]> getList(){

return this.list;

}

public int getSize(){

return this.size;

}

public void printBytes(byte[] buf,StringBuffer sb){

for(byte temp : buf){

sb.append(temp).append(",");

}

}

private void printBytes(List<byte[]> list,StringBuffer buf){

for(byte[] temp : list){

printBytes(temp,buf);

}

}

}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.