当前位置: 首页 > news >正文

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

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

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);

}

}

}


转载于:https://my.oschina.net/mutianya/blog/291630

相关文章:

  • 微软 WPC 2014 合作伙伴keynote
  • 【墙裂推荐】适合高中生看的HTML5网页开发实例入门书
  • yum功能:安装软件组,全系统更新
  • SendMessage,BroadcastMessage
  • Windows网络命令
  • 数据库设计原则
  • UESTC 2014 Summer Training #6 Div.2
  • 堆栈原理
  • C++11 FAQ中文版--转
  • AVC1与H264的差别
  • sql点滴42—mysql中的时间转换
  • EucOne调试
  • 产品经理入职第一个月要做的12件事
  • 【转自ITPUB】SYNONYM关于underlying table权限的小小发现
  • allocator 的用法
  • angular组件开发
  • ES6核心特性
  • Flex布局到底解决了什么问题
  • Laravel核心解读--Facades
  • React 快速上手 - 07 前端路由 react-router
  • SwizzleMethod 黑魔法
  • vue自定义指令实现v-tap插件
  • 第十八天-企业应用架构模式-基本模式
  • 基于Mobx的多页面小程序的全局共享状态管理实践
  • 如何学习JavaEE,项目又该如何做?
  • 实现简单的正则表达式引擎
  • 使用 5W1H 写出高可读的 Git Commit Message
  • 因为阿里,他们成了“杭漂”
  • 用 vue 组件自定义 v-model, 实现一个 Tab 组件。
  • 由插件封装引出的一丢丢思考
  • 终端用户监控:真实用户监控还是模拟监控?
  • const的用法,特别是用在函数前面与后面的区别
  • 整理一些计算机基础知识!
  • #include
  • #NOIP 2014#day.2 T1 无限网络发射器选址
  • (0)Nginx 功能特性
  • (12)Linux 常见的三种进程状态
  • (cljs/run-at (JSVM. :browser) 搭建刚好可用的开发环境!)
  • (超简单)构建高可用网络应用:使用Nginx进行负载均衡与健康检查
  • (第二周)效能测试
  • (附源码)springboot金融新闻信息服务系统 毕业设计651450
  • (离散数学)逻辑连接词
  • (四)Android布局类型(线性布局LinearLayout)
  • (转)Sublime Text3配置Lua运行环境
  • .Mobi域名介绍
  • .net core 3.0 linux,.NET Core 3.0 的新增功能
  • .NetCore Flurl.Http 升级到4.0后 https 无法建立SSL连接
  • .NET运行机制
  • .NET中 MVC 工厂模式浅析
  • .vue文件怎么使用_我在项目中是这样配置Vue的
  • @configuration注解_2w字长文给你讲透了配置类为什么要添加 @Configuration注解
  • @KafkaListener注解详解(一)| 常用参数详解
  • @德人合科技——天锐绿盾 | 图纸加密软件有哪些功能呢?
  • [.net] 如何在mail的加入正文显示图片
  • [AI]文心一言出圈的同时,NLP处理下的ChatGPT-4.5最新资讯