Java的:偏移一个byte []不执行复制操作 [英] Java: Offsetting a byte[] without performing a copy operation

查看:131
本文介绍了Java的:偏移一个byte []不执行复制操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在想,如果有可能,访问字节[] 用,而不必四处复制数据的偏差?结果
我看了阵列。* ByteArrayInputStream的 System.arraycopy ,但它们都需要分配一个新的字节[] 复制到。

我要的是一个相当于这 C ++

 的char *缓冲区=新的char [256];
字符* buf_offset =缓冲区+ 128; //< - 无可复制


解决方案

您可以通过的ByteBuffer 各地来代替。它可以前进,复制,而不进行复制切片。

的ByteBuffer真的是丑陋和反直觉。但是它被新的API JDK广泛使用,因此可以接受可能,这是一个基本的类型。

I was wondering if it is possible, access a byte[] with an offset without having to copy data around?
I've looked at Arrays.*, ByteArrayInputStream and System.arraycopy, but they all require to allocate a new byte[] to copy to.

What I want is an equivalent to this in C++:

char* buffer = new char[256];
char* buf_offset = buffer + 128; // <- no copy

解决方案

You can pass ByteBuffer around instead. It can be advanced, duplicated, sliced without copying.

ByteBuffer is really ugly and counter-intuitive. However it's being used extensively in new JDK APIs, so one can probably accept that it's a basic type.

这篇关于Java的:偏移一个byte []不执行复制操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆