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

  • SEARCH
  • Home
  • 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 4006084
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:28:19+00:00 2026-05-20T08:28:19+00:00

i am looking for a method in java, to extract certain bytes from an

  • 0

i am looking for a method in java, to extract certain bytes from an input stream.
for example
i will have this stream of data occuring

0x01,0x02,0x00,0x01,0x00,0x01,0x03,0x04,0x00,0x01,0x09,0x08,0x00,0x01,0x00,0x01

my encoding scheme is type data ending
firstly i will check the first byte,
then i will want to store all the data in a byte array from 0x01 untill the occurance of 0x00,0x01,0x00,0x01 except for the 0x01‘s

so the first piece of data i would place into the array

0x01,0x02,0x00,0x00 

and then onto the next ,
this begins with a 0x03 and ends with 0x00,0x01,0x00,0x01
i would like for this to be placed in another byte array as,

0x03,0x04,0x00,0x01,0x09,0x08,0x00,0x00

how would i go about doing this, i began with using

a ByteArrayOutputStream to add dynamically to the byte array, without needing to know the size,
but im lost on the logic on how would extract out each pattern and remove each 0x01 following a 0x00,
also im rading a byte in from an input stream , one byte at a time (its the only way i can get the bytes)

  • 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-20T08:28:20+00:00Added an answer on May 20, 2026 at 8:28 am

    You need a finite-state recognizer. For your simple syntax the following pseudocode should do the trick:

    state = 0;
    while( (byte=input.read()) != EOF)
    {
        switch(state)
        {
            case 0:     // "normal" state
                if (byte == 0x00)
                {
                    state = 1;
                    buf.append(byte);
                }
                else
                    output.write(byte)
                break;
            case 1:     // We've seen a 0x00
                if (byte == 0x00)
                {
                    state = 1;
                    output.write(buf);
                }
                else if (byte == 0x01)
                {
                    state = 2;
                    buf.append(byte);
                }
                else
                {
                    output.write(buf);
                    buf.clear();
                    state = 0;
                }
                break;
            case 2:     // We've seen 0x00,0x01
                if (byte == 0x00)
                {
                    state = 3;
                    buf.append(byte);
                }
                else if (byte == 0x01)
                {
                    output.write(0x00);
                    buf.clear();
                    state = 0;
                }
                else
                {
                    output.write(buf);
                    buf.clear();
                    state = 0;
                }
                break;
            case 3:     // We've seen 0x00,0x01,0x00
                if (byte == 0x00)
                {
                    state = 1;
                    output.write(buf);
                    buf.clear();
                    buf.append(byte);
                }
                else if (byte == 0x01)
                {
                    // The last four input bytes were 0x00,0x01,0x00,0x01
                    state = 0;
                    output.write(0x00,0x00);
                    buf.clear
                }
                else
                {
                    output.write(buf);
                    buf.clear();
                    state = 0;
                }
                break;
        }
    }
    if (!buf.empty()) output.write(buf);
    

    This works by reading bytes one at a time.

    If it detects a 0x00, we need to start looking for the delimiter pattern but save the bytes in case later we find it was a false alarm. The “state” variable keeps track of what we’ve seen so far. At each point if the input matches the next expected delimiter byte we save it, bump the state and keep going. If at any point we don’t get the next expected delimiter byte we just write out all the saved data, clear the buffer and go back to “normal” state. However, if we eventually see the entire delimiter string, we write out 0x00,0x00 and discard the saved bytes (which would be 0x00,0x01,0x00,0x01).

    EDIT: Code modified to handle additional condition from OP and comment from @Shaded

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

Sidebar

Related Questions

Possible Duplicates: I am looking forward to call some javascript method from a java
I am looking for the best method to run a Java Application as a
I'm looking for the best method to parse various XML documents using a Java
I was looking at the source code of java.uti.concurrent.locks.AbstractQueuedSynchronizer and the acquire() method looks
I am looking for a method of storing Application Messages, such as You have
I'm looking for a method (or function) to strip out the example.ext part of
I'm looking for the Highlight code -> right-click -> Extract Method type of functionality
I'm looking for a way to extract the essence of a signature in Java.
I'm looking for a simple method of converting between java.util.Date and javax.xml.datatype.XMLGregorianCalendar in both
I'm trying to override the toString method in Java to output from two arrays

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.