提交 1822e386 编写于 作者: 檀越@新空间's avatar 檀越@新空间 🐭

fix:和string互转

上级 9537cfb6
package com.kwan.shuyu.heima.bytebuffer;
import lombok.extern.slf4j.Slf4j;
import java.nio.ByteBuffer;
import static com.kwan.shuyu.until.ByteBufferUtil.debugAll;
/**
* ByteBuffer放入数据并读取数据
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/18 17:15
*/
@Slf4j
public class ByteBuffer_003_Test_put {
public static void main(String[] args) {
ByteBuffer buffer = ByteBuffer.allocate(10);
buffer.put((byte) 0x61);
debugAll(buffer);
buffer.put(new byte[]{0x62, 0x63, 0x64});
buffer.flip();
System.out.println(buffer.get());
debugAll(buffer);
}
}
package com.kwan.shuyu.heima.bytebuffer;
import lombok.extern.slf4j.Slf4j;
import java.nio.ByteBuffer;
import static com.kwan.shuyu.until.ByteBufferUtil.debugAll;
/**
* ByteBuffer放入数据并读取数据
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/18 17:15
*/
@Slf4j
public class ByteBuffer_004_Test_flip {
public static void main(String[] args) {
ByteBuffer buffer = ByteBuffer.allocate(10);
buffer.put((byte) 0x61);
debugAll(buffer);
buffer.put(new byte[]{0x62, 0x63, 0x64});
buffer.flip();
System.out.println(buffer.get());
debugAll(buffer);
buffer.compact();
debugAll(buffer);
buffer.put(new byte[]{0x65, 0x6f});
debugAll(buffer);
}
}
package com.kwan.shuyu.heima.bytebuffer;
import lombok.extern.slf4j.Slf4j;
import java.nio.ByteBuffer;
import static com.kwan.shuyu.until.ByteBufferUtil.debugAll;
/**
* ByteBuffer放入数据并读取数据
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/18 17:15
*/
@Slf4j
public class ByteBuffer_005_Test_compact {
public static void main(String[] args) {
ByteBuffer buffer = ByteBuffer.allocate(10);
buffer.put((byte) 0x61);
debugAll(buffer);
buffer.put(new byte[]{0x62, 0x63, 0x64});
buffer.flip();
System.out.println(buffer.get());
debugAll(buffer);
buffer.compact();
debugAll(buffer);
buffer.put(new byte[]{0x65, 0x6f});
debugAll(buffer);
}
}
package com.kwan.shuyu.heima.bytebuffer;
import lombok.extern.slf4j.Slf4j;
import java.nio.ByteBuffer;
/**
* ByteBuffer放入数据并读取数据
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/18 17:15
*/
@Slf4j
public class ByteBuffer_006_Test_allocate {
public static void main(String[] args) {
System.out.println(ByteBuffer.allocate(16).getClass().getSimpleName());
System.out.println(ByteBuffer.allocateDirect(16).getClass().getSimpleName());
}
}
package com.kwan.shuyu.heima.bytebuffer;
import com.kwan.shuyu.until.ByteBufferUtil;
import lombok.extern.slf4j.Slf4j;
import java.nio.ByteBuffer;
/**
* ByteBuffer放入数据并读取数据
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/18 17:15
*/
@Slf4j
public class ByteBuffer_007_Test_read {
public static void main(String[] args) {
ByteBuffer buffer = ByteBuffer.allocate(10);
buffer.put(new byte[]{'a', 'b', 'c', 'd'});
buffer.flip();
buffer.get(new byte[4]);
ByteBufferUtil.debugAll(buffer);
buffer.rewind();//恢复到起始位置
System.out.println((char) buffer.get());
}
}
package com.kwan.shuyu.heima.bytebuffer;
import lombok.extern.slf4j.Slf4j;
import java.nio.ByteBuffer;
/**
* ByteBuffer放入数据并读取数据
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/18 17:15
*/
@Slf4j
public class ByteBuffer_008_Test_mark {
public static void main(String[] args) {
ByteBuffer buffer = ByteBuffer.allocate(10);
buffer.put(new byte[]{'a', 'b', 'c', 'd'});
buffer.flip();
System.out.println((char) buffer.get());
System.out.println((char) buffer.get());
buffer.mark();// 加标记,泰列2的位置
System.out.println((char) buffer.get());
System.out.println((char) buffer.get());
buffer.reset();//将position 重置到泰列 2
System.out.println((char) buffer.get());
System.out.println((char) buffer.get());
}
}
package com.kwan.shuyu.heima.bytebuffer;
import lombok.extern.slf4j.Slf4j;
import java.nio.ByteBuffer;
/**
* ByteBuffer放入数据并读取数据
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/18 17:15
*/
@Slf4j
public class ByteBuffer_009_Test_get {
public static void main(String[] args) {
ByteBuffer buffer = ByteBuffer.allocate(10);
buffer.put(new byte[]{'a', 'b', 'c', 'd'});
buffer.flip();
System.out.println((char) buffer.get());
System.out.println((char) buffer.get());
buffer.mark();// 加标记,泰列2的位置
System.out.println((char) buffer.get());
System.out.println((char) buffer.get());
buffer.reset();//将position 重置到泰列 2
System.out.println((char) buffer.get());
System.out.println((char) buffer.get());
//get(i)不会改交读索引的位骂
System.out.println((char) buffer.get(3));
System.out.println((buffer));
}
}
package com.kwan.shuyu.heima.bytebuffer;
import lombok.extern.slf4j.Slf4j;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import static com.kwan.shuyu.until.ByteBufferUtil.debugAll;
/**
* 字符串放入到ByteBuffer中
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/18 17:15
*/
@Slf4j
public class ByteBuffer_010_Test_String {
public static void main(String[] args) {
//方式一,不会自动切换到读模式
ByteBuffer buffer1 = ByteBuffer.allocate(16);
buffer1.put("hello".getBytes());
debugAll(buffer1);
//方式二,自动切换到读模式
ByteBuffer buffer2 = StandardCharsets.UTF_8.encode("hello");
debugAll(buffer2);
//方式三,自动切换到读模式
final ByteBuffer buffer3 = ByteBuffer.wrap("hello".getBytes());
debugAll(buffer3);
//转为字符串
final String s2 = StandardCharsets.UTF_8.decode(buffer2).toString();
System.out.println(s2);
final String s1 = StandardCharsets.UTF_8.decode(buffer1).toString();
System.out.println(s1);//解码失败
final String s3 = StandardCharsets.UTF_8.decode(buffer3).toString();
System.out.println(s3);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册