gpt4 book ai didi

java - 解码为字符串时 ByteBuffer 丢失数据

转载 作者:行者123 更新时间:2023-12-02 10:28:22 24 4
gpt4 key购买 nike

我正在读取和写入 ByteBuffer

import org.assertj.core.api.Assertions;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;

public class Solution{
public static void main(String[] args) throws Exception{

final CharsetEncoder messageEncoder = Charset.forName("ISO-8859-1").newEncoder();
String message = "TRANSACTION IGNORED";
String carrierName= "CARR00AB";
int messageLength = message.length()+carrierName.length()+8;


System.out.println(" --------Fill data---------");
ByteBuffer messageBuffer = ByteBuffer.allocate(4096);
messageBuffer.order(ByteOrder.BIG_ENDIAN);
messageBuffer.putInt(messageLength);
messageBuffer.put(messageEncoder.encode(CharBuffer.wrap(carrierName)));
messageBuffer.put(messageEncoder.encode(CharBuffer.wrap(message)));
messageBuffer.put((byte) 0x2b);
messageBuffer.flip();

System.out.println("------------Extract Data Approach 1--------");

CharsetDecoder messageDecoder = Charset.forName("ISO-8859-1").newDecoder();
int lengthField = messageBuffer.getInt();
System.out.println("lengthField="+lengthField);
int responseLength = lengthField - 12;
System.out.println("responseLength="+responseLength);
String messageDecoded= messageDecoder.decode(messageBuffer).toString();
System.out.println("messageDecoded="+messageDecoded);
String decodedCarrier = messageDecoded.substring(0, carrierName.length());
System.out.println("decodedCarrier="+ decodedCarrier);
String decodedBody = messageDecoded.substring(carrierName.length(), messageDecoded.length() - 1);
System.out.println("decodedBody="+decodedBody);

Assertions.assertThat(messageLength).isEqualTo(lengthField);
Assertions.assertThat(decodedBody).isEqualTo(message);
Assertions.assertThat(decodedBody).isEqualTo(message);

ByteBuffer messageBuffer2 = ByteBuffer.allocate(4096);
messageBuffer2.order(ByteOrder.BIG_ENDIAN);
messageBuffer2.putInt(messageLength);
messageBuffer2.put(messageEncoder.encode(CharBuffer.wrap(carrierName)));
messageBuffer2.put(messageEncoder.encode(CharBuffer.wrap(message)));
messageBuffer2.put((byte) 0x2b);
messageBuffer2.flip();

System.out.println("---------Extract Data Approach 2--------");

byte [] data = new byte[messageBuffer2.limit()];
messageBuffer2.get(data);
String dataString =new String(data, "ISO-8859-1");
System.out.println(dataString);

}
}

它工作正常,但后来我想重构它,请参阅上面代码中的方法 2

    byte [] data = new byte[messageBuffer.limit()];
messageBuffer.get(data);
String dataString =new String(data, "ISO-8859-1");
System.out.println(dataString);

Output= #CARR00ABTRANSACTION IGNORED+

你们能帮我解释一下吗

  1. 为什么在解码时第二种方法中整数丢失了???

  2. 有没有办法在第二种方法中提取整数?

最佳答案

好吧,你正在尝试从缓冲区读取一个 int,它占用 4 位,然后尝试在读取 4 位后获取整个数据

我所做的是在读取 int 后调用 messageBuffer2.clear(); 来解决此问题。这是完整的代码

System.out.println(messageBuffer2.getInt());
byte[] data = new byte[messageBuffer2.limit()];
messageBuffer2.clear();
messageBuffer2.get(data);
String dataString = new String(data, StandardCharsets.ISO_8859_1);
System.out.println(dataString);

输出是:

35
#CARR0033TRANSACTION IGNORED+

编辑:所以基本上,当您调用 clear 时,它会重置各种变量,并且还会重置它的来源位置,这就是它修复它的方式。

关于java - 解码为字符串时 ByteBuffer 丢失数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53761386/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com