使用大字节数组输入进行操作

我正在一个项目中,我要获得一大堆(70k)字节。


我必须对其进行解码,并根据数据正确解析结果。


数组是这样构建的:


{ header_cells, dataType1, dataType1..., dataType2, dataType2...}

我知道所有长度,基本上我想使用以下方法将其拆分:


byte[] arr = new byte[SIZE];

byte[] output = buffer.get(arr, offset, length);

然后将其包裹到我的对象中。


是好的解决方案,还是有更好的解决方案?




守候你守候我
浏览 119回答 2
2回答

慕婉清6462132

70kbyte[]阵列大约需要68kb的内存,这在任何现代硬件上都不是。着重于首先阅读可理解的代码,并仅在发现性能问题时进行优化。

一只名叫tom的猫

如果不想每次需要子序列时都深拷贝内存,则可以使用缓冲区片但是,请注意,除非您至少有一个切片引用,否则整个背景数组都将在内存中。例如:ByteBuffer buff = ByteBuffer.allocate(128);buff.order(ByteOrder.nativeOrder());for(int i=0; i < 128; i++) {&nbsp; &nbsp; buff.put((byte)i);}// use custom code instead of flip, to provide a slicebuff.position( 32 );buff.limit(64);ByteBuffer subBuffer = buff.slice();// custom flipbuff.limit(128);buff.position(0);System.out.print("Sub buffer: [");for(int i=0; i < subBuffer.limit(); i++) {&nbsp; &nbsp; System.out.print( String.format(" %d,", subBuffer.get(i) ) );}System.out.println(" ]");System.out.print("Whole buffer: [");for(int i=0; i < buff.limit(); i++) {&nbsp; &nbsp; System.out.print( String.format(" %d,", buff.get(i) ) );}System.out.println(" ]");输出:Sub buffer: [ 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, ]Whole buffer: [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, ]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java