前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java bytebuffer flip_java string转byte

java bytebuffer flip_java string转byte

作者头像
全栈程序员站长
发布2022-11-09 16:19:36
8740
发布2022-11-09 16:19:36
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

代码语言:javascript
复制
import java.nio.ByteBuffer;  
import java.nio.ByteOrder;  
public class bytebuffertest {  
public static void main(String[] args)  
{  
// Create a ByteBuffer using a byte array   
byte[] bytes = new byte[10];  
ByteBuffer buf = ByteBuffer.wrap(bytes);  
// Create a non-direct ByteBuffer with a 10 byte capacity   
// The underlying storage is a byte array.   
buf = ByteBuffer.allocate(10);  
// Create a direct (memory-mapped) ByteBuffer with a 10 byte capacity.   
buf = ByteBuffer.allocateDirect(10);  
// Create an empty ByteBuffer with a 10 byte capacity   
ByteBuffer bbuf = ByteBuffer.allocate(10);  
// Get the ByteBuffer's capacity   
int capacity = bbuf.capacity(); // 10   
System.out.println(capacity);  
// Use the absolute get().   
// This method does not affect the position.   
byte b = bbuf.get(5); // position=0   
// Set the position   
bbuf.position(5);  
// Use the relative get()   
b = bbuf.get();  
// Get the new position   
int pos = bbuf.position(); // 6   
// Get remaining byte count   
int rem = bbuf.remaining(); // 4   
// Set the limit   
bbuf.limit(7); // remaining=1   
// This convenience method sets the position to 0   
bbuf.rewind(); // remaining=7   
//Converting Between a ByteBuffer an a Byte Array    
// Create a ByteBuffer from a byte array   
byte[] bytes1 = new byte[10];  
ByteBuffer buf1 = ByteBuffer.wrap(bytes);  
// Retrieve bytes between the position and limit   
// (see Putting Bytes into a ByteBuffer)   
bytes1 = new byte[buf.remaining()];  
buf1.get(bytes1, 0, bytes1.length);  
// Retrieve all bytes in the buffer   
buf1.clear();  
bytes1 = new byte[buf1.capacity()];  
buf1.get(bytes1, 0, bytes1.length);  
// Use the absolute put().   
// This method does not affect the position.   
bbuf.put((byte)0xFF); // position=0   
// Set the position   
bbuf.position(5);  
// Use the relative put()   
bbuf.put((byte)0xFF);  
// Get the new position   
int pos1 = bbuf.position(); // 6   
//Setting the Byte Ordering for a ByteBuffer    
// Get default byte ordering   
ByteOrder order = buf.order(); // ByteOrder.BIG_ENDIAN   
// Put a multibyte value   
buf.putShort(0, (short)123);  
buf.get(0); // 0   
buf.get(1); // 123   
// Set to little endian   
buf.order(ByteOrder.LITTLE_ENDIAN);  
// Put a multibyte value   
buf.putShort(0, (short)123);  
buf.get(0); // 123   
buf.get(1); // 0   
}  
} 

这是一篇转载的博客我还没有来得及进行验证,放在这里只是为了以后自己方便查看,如有错误的地方还请以评论的方式告知,我会进行改正。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/186742.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年10月2日 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档