码迷,mamicode.com
首页 > 其他好文 > 详细

一个byte缓存--用于图片或视频

时间:2014-07-21 10:14:13      阅读:270      评论:0      收藏:0      [点我收藏+]

标签: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);

}

}

}


一个byte缓存--用于图片或视频,布布扣,bubuko.com

一个byte缓存--用于图片或视频

标签:des   style   http   os   数据   io   

原文地址:http://my.oschina.net/mutianya/blog/291630

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!