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 925607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:32:49+00:00 2026-05-15T19:32:49+00:00

How should I really go about implementing the following? I will have to handle

  • 0

How should I really go about implementing the following? I will have to handle a byte array that will contain text on several lines. The average size of the data is probably going to be around 10 kilobytes of data.

After unspecified amount of lines there will be a line starting with special token (“FIRSTSTRING”). Later somewhere on the same file there will be an other line also starting with a special token (“SECONDSTRING”). If both the first and second lines are defined in the byte array the second line should be copied in place of the first line. After that the resultant byte array should be returned.

Below is my first attempt. I did not refactor it to reduce complexity yet. I am concerned about reliablity and also very much about performance. It seems there are too many ways going around this and I lack experience required for judgement. I would really appreciate some good input on this.

 private byte[] handleHeader(final byte[] input) throws IOException {

  // input
  ByteArrayInputStream bais = new ByteArrayInputStream(input);
  InputStreamReader isr = new InputStreamReader(bais);
  BufferedReader brs = new BufferedReader (isr);
  // output
  ByteArrayOutputStream data = new ByteArrayOutputStream();
  ByteArrayOutputStream after = new ByteArrayOutputStream();

  String line=null;
  String original=null;
  String changeWith=null;

    while ((line = brs.readLine())!=null) {
        line+="\n";
          if (line.startsWith("FIRSTSTRING")) {
              original = line;
              continue;
          }
          if (line.startsWith("SECONDSTRING")) {
              changeWith = line;
              continue;
          }
          if ("".equals(original)) {
              data.write(line.getBytes());
          } else {
              after.write(line.getBytes());
          }

      }

if (changeWith!=null && original != null) {
    changeWith+="\n";
    data.write(changeWith.getBytes());
} else if (original != null){
    data.write(original.getBytes());
}

after.writeTo(data);

return data.toByteArray();
  }
  • 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-05-15T19:32:49+00:00Added an answer on May 15, 2026 at 7:32 pm

    For starters it doesn’t sound like you’ve defined your problem precisely – you say that there will be a “FIRSTSTRING” line, and there will be a “SECONDSTRING” line, but then you go on to say “if both lines are present”… If you know there will always be the second line things get a lot simpler

    In any case, an algorithm like the following should be reasonably easy to implement and understand later, and shouldn’t be too inefficient:

    • Create a StringBuilder to hold the overall output.
    • Iterate through the lines, adding all “normal” lines straight to the output.
    • When(/if) you encounter the “FIRSTSTRING” line, store this in a separate variable and create a second StringBuilder to hold the “second half” of the text.
    • Continue iterating, adding all further normal lines to this second StringBuilder.
    • When(/if) you encounter the “SECONDSTRING” line, append this to the main output, then append the entirety of the second StringBuilder to the main output, then append the remaining lines to the main output.
    • If you reach the end of the file without finding the second string line, then append the saved FIRSTSTRING line to the overall output and follow it up with the contents of the second StringBuilder.

    Oh, and you’re turning bytes into Strings without specifying an explicit character encoding. Never do that. If you know what the character encoding is, specific it explicitly (in the InputStreamReader‘s constructor). If you don’t know what the character encoding of the stream of bytes is, then you cannot read it reliably at all.

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

Sidebar

Related Questions

I have an application which really should be installed, but does work fine when
I really feel that I should learn Lisp and there are plenty of good
What should i use to code Classic ASP under Linux. I have really tried
This should be straight forward for a guru. I don't have any code really
I'm creating a Django powered website that will have numerous applications (Blog, Shop, Portfolio,
I really should upgrade to Firefox 3, but I'm very dependent on Firebug working
Ok, so it's almost as easy as pie already. But it really should be
This should be a really really simple thing, but for some reason it is
I'm not really asking whether I should use either a RDBMS or config files
Can Thread.getContextClassLoader() be null ? The javadoc is not really clear. Should a library

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.