java 根据特定数据结构 打自定义包------解包篇
自定义的数据结构在解包的过程中用到,没有这个数据结构的话,解包就无从谈起了!
好了,直接上代码!酷毙的程序员一切尽在代码中!
/** * read len off ->len * 读一段从偏移位置开始读 * @param fis * @param off * @param len * @return buf -> int * @throws IOException */public static int readNext(FileInputStream fis ,int off,int len) throws IOException{off++;if(len==1){byte [] buf = new byte[off+len];fis.read(buf,off,len);int length = (int)buf[off];System.out.println( "this readNext length is :"+length);return length;}else{byte [] buf = new byte[off+len];fis.read(buf,off,len);byte [] buf2 = new byte[4];for(int i=buf.length-4;i<=buf.length;i++){buf2[3]=buf[buf.length-1];buf2[2]=buf[buf.length-2];buf2[1]=buf[buf.length-3];buf2[0]=buf[buf.length-4];}System.out.println( "this readNext ==byte 2 int== length is :"+bytesToInt(buf2));return bytesToInt(buf2);}}