Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

263
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda