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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:04:51+00:00 2026-05-31T08:04:51+00:00

I’m trying to read a webpage using following code : URL url = new

  • 0

I’m trying to read a webpage using following code :

URL url = new URL("somewebsitecomeshere");
                URLConnection c = url.openConnection();
                if(getHttpResponseCode(c) == 200)
                {

                    if (isContentValid(c))//accept html/xml only!
                    {
                        InputStream is = c.getInputStream();


                        Reader r = new InputStreamReader(is);


                        System.out.println(r.toString());
                                                //after commenting this everything works great!
                        setHTMLString(getStringFromReader(r));
                        System.out.println(getHTMLString());

                        ParserDelegator parser = new ParserDelegator();
                        parser.parse(r, new Parser(url), true);
                        r.close();
                        is.close();

                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                    else
                        log("content is not valid!");
                }
                else
                {
                    System.out.println("ERROR" + c.getContentType() + c.getURL());
                }
//---------------------------------------------------
    private String getStringFromReader(Reader reader) throws IOException {
          char[] arr = new char[8*1024]; // 8K at a time
          StringBuffer buf = new StringBuffer();
          int numChars;

          while ((numChars = reader.read(arr, 0, arr.length)) > 0) {
              buf.append(arr, 0, numChars);
          }
              //Reset position to 0
          reader.reset();

          return buf.toString();
            }

if try to read string using getStringFromReader() the rest of the code will be ignored due to changing position of Reader to EOF so I tried to reset the position to 0 but I got the following error :

java.io.IOException: reset() not supported
    at java.io.Reader.reset(Unknown Source)
    at sample.getStringFromReader(Spider.java:248)
    at default(sample.java:286)
    at default.main(sample.java:130)

How can I reset the Reader position to 0?

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

    Short answer, your stream doesn’t support reset or mark methods. Check the result of:

    is.markSupported()
    

    Long answer, an InputStream is a flow of bytes. Bytes can come from a file, a network resource, a string, etc. So basically, there are streams that don’t support resetting the reader position to the start of the stream, while others do (random access file).

    A stream from a web site will normally use underlying network connection to provide the data. It means that it’s up to the underlying network protocol (TCP/IP for example) to support or not resetting the stream, and normally they don’t.

    In order to reset any stream you would have to know the entire flow, from start to end. Network communications send a bunch of packages (which may be in order or not) to transfer data. Packages may get lost or even be duplicated, so normally information is buffered and interpreted as it is received. It would be very expensive to reconstruct all messages at network level. So that is normally up to the receiver, if it wants to do that.

    In your case If what you want is print the input stream I would recommend creating a custom InputStream, which receives the original InputStream and whenever it is read it prints the read value and returns it at the same time. For example:

    class MyInputStream extends InputStream {
    
      InputStream original = null;
    
      public MyInputStream(InputStream original) {
        this.original = original;
      }
    
      @Override
      public int read() throws IOException {
        int c = original.read();
        System.out.printf("%c", c);
        return c;
      }
    }
    

    Then wrap your original InputStream with that:

    .
    .
    .
    
    InputStream myIs = new MyInputStream(is);
    Reader r = new InputStreamReader(myIs);
    
    .
    .
    .
    

    Hope it helps.

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

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I want use html5's new tag to play a wav file (currently only supported

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.