java中构造字节流的一种实现

发布时间:2019-11-17 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了java中构造字节流的一种实现脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

java中构造字节流的一种实现

import java.util.ArrayList; import java.util.List;  /**  * Created by kaven on 2018/4/2.  */ public class testBytes {      /**      * 专门创建一个类来保存指定byte数      */     public class MyByte {         byte[] data;          MyByte(int n) {             data = new byte[n];         }          void setData(String s) {             byte[] tmp = s.getBytes();              // 提示数据超了             if (tmp.length > data.length) {                 System.err.print("too many data");             }              for (int i = 0; i < this.data.length &amp;& i < tmp.length; i++) {                 data[i] = tmp[i];             }         }          // 设置一个值到第一位         void setData(int i) {             data[0] = (byte) i;         }          byte[] getByte() {             return data;         }          int size() {             return data.length;         }     }       void LOGin() {          MyByte msgType = new MyByte(1);         MyByte userId = new MyByte(16);         MyByte msg = new MyByte(12);          msgType.setData(1);         userId.setData("1");         msg.setData("222");          List<;myByte> list = new ArrayList();         list.add(msgType);         list.add(userId);         list.add(msg);          byte[] res = join(list);         System.out.print(res.toString());     }      /**      * 将列表中的数据合并成一个数据      * @param list      * @return      */     private byte[] join(List<MyByte> list) {         int count = 0;         for (int i = 0; i < list.size(); i++) {             count += list.get(i).size();         }         byte[] res = new byte[count];          int start = 0;         for (int i = 0; i < list.size(); i++) {             byte[] tmp = list.get(i).getByte();             for (int j = 0; j < tmp.length; j++) {                 res[start] = tmp[j];                 start += 1;             }         }         return res;     }      public static void main(String args[]) {         TestBytes a = new TestBytes();         a.login();     }  } 

脚本宝典总结

以上是脚本宝典为你收集整理的java中构造字节流的一种实现全部内容,希望文章能够帮你解决java中构造字节流的一种实现所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。