Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8677343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:25:59+00:00 2026-06-12T20:25:59+00:00

I have a situation where I need to process some data from a stream

  • 0

I have a situation where I need to process some data from a stream line by line. The problem is that the encoding of the data is not known in advance; it might be UTF-8 or any legacy single-byte encoding (e.g. Latin1, ISO-8859-5, etc). It will not be UTF16 or exotics like EBCDIC, so I can reasonably expect \n to be unambiguous, so in theory I can split it into lines. At some point, when I encounter an empty line, I will need to feed the rest of the stream somewhere else (without splitting it into lines, but still without any reencoding); think in terms of HTTP-style headers followed by an opaque body.

Here is what I got:

function processStream(stream) {
    var buffer = '';

    function splitLines(data) {
        buffer += data;
        var lf = buffer.indexOf('\n');
        while (lf >= 0) {
            var line = buffer.substr(0, lf - 1);
            buffer = buffer.substr(lf + 1);
            this.emit('line', line);
            lf = buffer.indexOf('\n');
        }
    }

    function processHeader(line) {
        if (line.length) {
            // do something with the line
        } else {
            // end of headers, stop splitting lines and start processing the body
            this
            .removeListener('data', splitLines)
            .removeAllListeners('line')
            .on('data', processBody);
            if (buffer.length) {
                // process leftover buffer as part of the body
                processBody(buffer);
                buffer = '';
            }
        }
    }

    function processBody(data) {
        // do something with the body chunks
    }

    stream.setEncoding('binary');
    stream
    .on('data', splitLines)
    .on('line', processHeader);
}

It does the job, but the problem is that the binary encoding is deprecated and will probably disappear in the future, leaving me without that option. All other Buffer encodings will either mangle the data or fail to decode it altogether if (most likely, when) it does not match the encoding. Working with Uint8Array instead will mean slow and inconvenient Javascript loops over the data just to find a newline.

Any suggestions on how to split a stream into lines on the fly, while remaining encoding-agnostic without using binary encoding?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T20:26:01+00:00Added an answer on June 12, 2026 at 8:26 pm

    Disclaimer: I’m not a Javascript developer.

    At some point, when I encounter an empty line, I will need to feed the rest of the stream somewhere else (without splitting it into lines, but still without any reencoding)

    Right. In that case, it sounds like you really don’t want to think about the data as text at all. Treat it as you would any binary data, and split it on byte 0x0A. (Note that if it came from Windows to start with, you might want to also remove any trailing 0x0D value.)

    I know it’s text really, but without any encoding information, it’s dangerous to impose any sort of interpretation on the data.

    So you should keep two pieces of state:

    • A list of byte arrays
    • The current buffer

    When you receive data, you logically want to create a new array with the current buffer prepending the new data. (For efficiency you may not want to actually create such an array, but I would do so to start with, until you’ve got it working.) Look for any 0x0A bytes, and split the array accordingly (create a new byte array as a “slice” of the existing array, and add the slice to the list). The new “current buffer” will be whatever data you have left after the final 0x0A.

    If you see two 0x0A values in a row, then you’d go into your second mode of just copying the data.

    This is all assuming that the Javascript / Node combination allows you to manipulate binary data as binary data, but I’d be shocked if it didn’t. The important point is not to interpret it as text at any point.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a situation where we need to display data from a database in
I have a .net web service that takes some xml data. From within the
I have a situation where I need to spawn a helper process from within
I have a situation where I need to notify some users when something in
I have a situation where I need to set my process' locale to en-US.
I have android app that talks to server and syncs some data int SQLite
I have a task endpoint that needs to process data (say >1MB file) uploaded
Using Python 2.7 Situation: I have some piece of Python code (that the user
I'm writing an application that uses a 3rd party library to process some data.
I have situation where I need to change the order of the columns/adding new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.