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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:45:17+00:00 2026-05-26T05:45:17+00:00

I have an InputStream which takes the html file as input parameter. I have

  • 0

I have an InputStream which takes the html file as input parameter. I have to get the bytes from the input stream .

I have a string: "XYZ". I’d like to convert this string to byte format and check if there is a match for the string in the byte sequence which I obtained from the InputStream. If there is then, I have to replace the match with the bye sequence for some other string.

Is there anyone who could help me with this? I have used regex to find and replace. however finding and replacing byte stream, I am unaware of.

Previously, I use jsoup to parse html and replace the string, however due to some utf encoding problems, the file seems to appear corrupted when I do that.

TL;DR: My question is:

Is a way to find and replace a string in byte format in a raw InputStream in Java?

  • 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-26T05:45:18+00:00Added an answer on May 26, 2026 at 5:45 am

    Not sure you have chosen the best approach to solve your problem.

    That said, I don’t like to (and have as policy not to) answer questions with “don’t” so here goes…

    Have a look at FilterInputStream.

    From the documentation:

    A FilterInputStream contains some other input stream, which it uses as its basic source of data, possibly transforming the data along the way or providing additional functionality.


    It was a fun exercise to write it up. Here’s a complete example for you:

    import java.io.*;
    import java.util.*;
    
    class ReplacingInputStream extends FilterInputStream {
    
        LinkedList<Integer> inQueue = new LinkedList<Integer>();
        LinkedList<Integer> outQueue = new LinkedList<Integer>();
        final byte[] search, replacement;
    
        protected ReplacingInputStream(InputStream in,
                                       byte[] search,
                                       byte[] replacement) {
            super(in);
            this.search = search;
            this.replacement = replacement;
        }
    
        private boolean isMatchFound() {
            Iterator<Integer> inIter = inQueue.iterator();
            for (int i = 0; i < search.length; i++)
                if (!inIter.hasNext() || search[i] != inIter.next())
                    return false;
            return true;
        }
    
        private void readAhead() throws IOException {
            // Work up some look-ahead.
            while (inQueue.size() < search.length) {
                int next = super.read();
                inQueue.offer(next);
                if (next == -1)
                    break;
            }
        }
    
        @Override
        public int read() throws IOException {    
            // Next byte already determined.
            if (outQueue.isEmpty()) {
                readAhead();
    
                if (isMatchFound()) {
                    for (int i = 0; i < search.length; i++)
                        inQueue.remove();
    
                    for (byte b : replacement)
                        outQueue.offer((int) b);
                } else
                    outQueue.add(inQueue.remove());
            }
    
            return outQueue.remove();
        }
    
        // TODO: Override the other read methods.
    }
    

    Example Usage

    class Test {
        public static void main(String[] args) throws Exception {
    
            byte[] bytes = "hello xyz world.".getBytes("UTF-8");
    
            ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
    
            byte[] search = "xyz".getBytes("UTF-8");
            byte[] replacement = "abc".getBytes("UTF-8");
    
            InputStream ris = new ReplacingInputStream(bis, search, replacement);
    
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
    
            int b;
            while (-1 != (b = ris.read()))
                bos.write(b);
    
            System.out.println(new String(bos.toByteArray()));
    
        }
    }
    

    Given the bytes for the string "Hello xyz world" it prints:

    Hello abc world
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an InputStream from which I'm reading characters. I would like multiple readers
I have a piece of java code which reads strings from a file and
I have an FTP client class which returns InputStream pointing the file. I would
My input is a InputStream which contains an XML document. Encoding used in XML
I have an inputStream and i want to write it to a file. I
I have an FFmpeg-based video-playing app which is able to play content from any
i have a txt file which contains many chinese characters, and the txt file
I call a service which returns a gzipped file. I have the data as
I am basically trying to return from a method which reads user input from
I have an application which has a font stored within a jar file. It

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.