0

I'm reading and writing to a 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);

}
}

It works fine but then I thought to refactor it, Please see approach 2 in above code

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

 Output=     #CARR00ABTRANSACTION IGNORED+

Could you guys help me with explanation

  1. why the integer is got missing in second approach while decoding ???

  2. Is there any way to extract the integer in second approach??

9
  • Without reading the question, Have you provided an encoding?
    – SamHoque
    Commented Dec 13, 2018 at 12:00
  • yes , Please see my code in question
    – edwin
    Commented Dec 13, 2018 at 12:01
  • Okay, I will compile the code my side and verify the issue if someone else hasn't already before me.
    – SamHoque
    Commented Dec 13, 2018 at 12:01
  • Where did you place your last code? Can you provide working example?
    – SamHoque
    Commented Dec 13, 2018 at 12:05
  • see updates , please remove Assertions before executing code
    – edwin
    Commented Dec 13, 2018 at 12:13

1 Answer 1

1

Okay so you are trying to read an int from the Buffer which takes up 4 bits and then trying to get the whole data after reading 4 bits

What I have done is call messageBuffer2.clear(); after reading the int to resolve this issue. here is the full code

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);

Output is:

35
   #CARR0033TRANSACTION IGNORED+

Edit: So basically when you are calling clear it resets various variables and it also resets the position it's getting from and thats how it fixes it.

1
  • Can you please explain how(logic behind) calling 'clear' fix this issue ??
    – edwin
    Commented Dec 13, 2018 at 14:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.