作为字符串解码时,ByteBuffer丢失数据

时间:2018-12-13 11:59:26

标签: java string byte bytebuffer

我正在读写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. 有第二种方法可以提取整数吗?

1 个答案:

答案 0 :(得分:1)

好的,因此您尝试从占用int的缓冲区中读取4 bits,然后在读取4 bits后尝试获取全部数据

我所做的是在阅读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时,它会重置各种变量,并且还会重置其从中获得的位置,这就是其解决方法。