java ByteBuffer和byte 数组相互转换

// Create a byte array

byte[] bytes = new byte[10];

// Wrap a byte array into a buffer

ByteBuffer buf = ByteBuffer.wrap(bytes);

// Retrieve bytes between the position and limit

// (see Putting Bytes into a ByteBuffer)

bytes = new byte[buf.remaining()];

// transfer bytes from this buffer into the given destination array

buf.get(bytes, 0, bytes.length);

// Retrieve all bytes in the buffer

buf.clear();

bytes = new byte[buf.capacity()];

// transfer bytes from this buffer into the given destination array

buf.get(bytes, 0, bytes.length);

时间: 2024-07-31 05:34:45

java ByteBuffer和byte 数组相互转换的相关文章

java File和Byte[]数组 相互转换

public class Test { public static void main(String[] args){ String filePath = "E:\\softoon\\workspace_softoon\\TestMobile\\src\\1.docx"; String outFilePath = "E:\\softoon\\workspace_softoon\\TestMobile\\src"; String outFileName = "

Redis入门 – Jedis存储Java对象 - (Java序列化为byte数组方式)

Redis入门 – Jedis存储Java对象 - (Java序列化为byte数组方式) 原文地址:http://alanland.iteye.com/admin/blogs/1600685(欢迎转载 - 转载请保留该原文链接) 07/19/12 03:08:05 PM 在Jedis开发中,我们很多时候希望直接把一个对象放到Redis中,然后在需要的时候取出来.Redis的key和value都支持二进制安全的字符串,存储Java对象不是问题,下面我们看一下如何来实现. 1要存储的对象 现在写一个

Java 基础类型转换byte数组, byte数组转换基础类型

Java 基础类型转换byte数组, byte数组转换基础类型 Java类型转换 java类对象转化为byte数组

Java 图片与byte数组互相转换

1 //图片到byte数组 2 public byte[] image2byte(String path){ 3 byte[] data = null; 4 FileImageInputStream input = null; 5 try { 6 input = new FileImageInputStream(new File(path)); 7 ByteArrayOutputStream output = new ByteArrayOutputStream(); 8 byte[] buf =

Java 文件和byte数组转换

/**      * 获得指定文件的byte数组      */      private byte[] getBytes(String filePath){          byte[] buffer = null;          try {              File file = new File(filePath);              FileInputStream fis = new FileInputStream(file);              Byte

java 字符串与字符数组相互转换

public class ssssss { public static void main(String[] args){ String str1 = "Hello Java"; char c[] = str1.toCharArray();//将一个字符串变为字符数组toCharArray(); for (int i = 0; i<c.length;i++){ System.out.print(c[i] + ","); } System.out.println

C# 对象、文件与二进制串(byte数组)之间的转换

1.关于本文 在使用C#下的TCP(类TcpClient).UDP(类UdpClient)协议传输信息时,都需要将信息转换为byte类型的数组进行发送.本文实现了两种object与byte数组的转换和一种文件与byte数组转换的方式.基础类型的数据,可以用BitConverter类中的函数进行转换. 2.object与byte[]的相互转换:使用IFormatter的Serialize和Deserialize进行序列化与反序列化 实现这个功能,需要先引用三个命名空间:System.IO.Syst

java byte数组与String互转

java byte数组与String互转 CreationTime--2018年7月6日14点53分 Author:Marydon 1.String-->byte[] 方法:使用String.getBytes(charset)实现 String website = "http://www.cnblogs.com/Marydon20170307"; // String-->byte[],并指定字符集 byte[] b = website.getBytes("utf-

java 中byte[] 数组的合并

因工作的需要,在从事 .Net 的开发中接触到了 Java, 虽然在大学的时候学过一段Java 编程,但并没有在实际的工作中使用过, Java 和 .Net的C#语法很相似,都是面向对象的,感觉在语法上只有些细微的差异,这里主要介绍以下,将两个数组合并成的操作,废话不多说,直接上代码: //System.arraycopy()方法 public static byte[] byteMerger(byte[] bt1, byte[] bt2){ byte[] bt3 = new byte[bt1.