I'm receiving data from a rabbit queue that I'm trying to convert to String
. The message always starts with the SOH character and always ends with a VT character, followed by an ETX character. The data is received correctly and the byte array printed to the console is correct. The problem is that after converting to String
, all the characters in the System.out.println
before the VT and ETX are omitted. I first thought that the byte[]
to String
conversion might be done wrong by me, but I think there might be an issue with printing to the console that I'm missing.
Here's all the conversion approaches that I tried:
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
System.out.println("received message");
String message1 = new String(delivery.getBody(), StandardCharsets.US_ASCII);
String message2 = new String(delivery.getBody(), StandardCharsets.UTF_8);
String message3 = new String(delivery.getBody(), StandardCharsets.UTF_16);
String message4 = new String(delivery.getBody(), StandardCharsets.UTF_16BE);
String message5 = new String(delivery.getBody(), StandardCharsets.UTF_16LE);
String message6 = new String(delivery.getBody(), StandardCharsets.ISO_8859_1);
String message7 = new String(delivery.getBody());
String message8 = Base64.getEncoder().encodeToString(delivery.getBody());
String message9 = new String(Base64.getEncoder().encode(delivery.getBody()), StandardCharsets.UTF_8);
System.out.println(Arrays.toString(delivery.getBody()) " - " delivery.getBody().length);
System.out.println("1 " message1 " - " message1.length() " - ");
System.out.println("2 " message2 " - " message2.length() " - ");
System.out.println("3 " message3 " - " message3.length() " - ");
System.out.println("4 " message4 " - " message4.length() " - ");
System.out.println("5 " message5 " - " message5.length() " - ");
System.out.println("6 " message6 " - " message6.length() " - ");
System.out.println("7 " message7 " - " message7.length() " - ");
System.out.println("8 " message8 " - " message8.length() " - ");
System.out.println("9 " message9 " - " message9.length() " - ");
};
The System.out.println
for messages 1, 2, 6, 7 print only VT, ETX and what is after, but nothing of what is before. The others are not properly converted and print asian characters or other random chars.
CodePudding user response:
Special characters
These are control characters
SOH
is start of header, ASCII code 1VT
is vertical tab, ASCII code 11, escaped"\v"
ETX
is end of text, ASCII code 3, escaped"\0x3"
See also:
C0 and C1 control codes for message transmission
These control characters have not only special meaning (semantics) for printing but only for text transmission.
Maybe you need to put a special message-conversion component into place, when receiving data from a RabbitMQ text-message queue.