I am trying to read a stdin from a WebExtentions application. The documentation says:
Each message is serialized using JSON, UTF-8 encoded and is preceded with a 32-bit value containing the message length in native byte order.
https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Native_messaging
They have an example in Python to read the message sent:
rawLength = sys.stdin.read(4)
if len(rawLength) == 0:
sys.exit(0)
messageLength = struct.unpack('@I', rawLength)[0]
message = sys.stdin.read(messageLength)
return json.loads(message)
So it removes the 32-bit number from the message and then gets the message. How can I do the same in Java?
Edit someone asked for me to attempt a translation. Please note I know little.
System.in.skip(4); // 4 bytes is 32 bits
Scanner sc = new Scanner(System.in);
String message = sc.next();
And it doesn't remove the 4 four bytes from the message. My only other solution is to find the last { symbol in the message (since it is a JSON object with only one key : value pair) and take the substring after that.