Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

261
Views
Getting a 32-bit preceding value from standard input in Java

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.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

To read the first 4 bytes from standard in as a 32 bit int do the following:

DataInputStream data = new DataInputStream(System.in);
int length = data.readInt();
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!