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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:18:08+00:00 2026-05-29T05:18:08+00:00

I’m new to Java and currently doing some experiments on it. I wrote a

  • 0

I’m new to Java and currently doing some experiments on it.
I wrote a little program that does read and write stream of std I/O but I
kept getting exceptions thrown for out of range. Here is my code

int BLOCKSIZE = 128*1024;                                                                                                                                                
InputStream inStream = new BufferedInputStream(System.in);

OutputStream outStream = new BufferedOutputStream(System.out);



byte[] buffer = new byte[BLOCKSIZE];




int bytesRead = 0;
int writePos = 0;
int readPos = 0;
while ((bytesRead = inStream.read(buffer,readPos,BLOCKSIZE)) != -1) {
 outStream.write(buffer,writePos,BLOCKSIZE);
 readPos += bytesRead;
 writePos += BLOCKSIZE;
 buffer = new byte[BLOCKSIZE];
}

Here is the exception thrown:”Exception in thread “main” java.lang.IndexOutOfBoundsException
at java.io.BufferedInputStream.read(BufferedInputStream.java:327)
at JavaPigz.main(JavaPigz.java:73)”

73th col is the inStream.read(…) statement. Basically I want to read 128kb bytes from stdin once and write it to the stdout and go back to read another 128kb chunk, so on and so forth. The same exception is also thrown to outStream.write()

I did some debugging and it looks BufferedInputStream buffers at most 64kb chunk once. Don’t know if this is true. Thank you.

Edit: I also tried doing
InputStream inStream = new BufferedInputStream(System.in,BLOCKSIZE);
to specify the size of buffered chunk I want. But turns out it keeps giving size of 64kb
no matter what is specified

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

    You’re increasing your readPos (and writePos) in your loop. The subsequent reads are starting at that offset for inserting into your buffer, and attempting to write BLOCKSIZE bytes into it … which won’t fit, thus giving you an index out of bounds error.

    The way you have that loop written, readPos and writePos should always be 0 especially since you’re creating a new buffer every time. That being said … you really don’t want to do that, you want to re-use the buffer. It looks like you’re just trying to read from the input stream and write it to the output stream …

    while ((bytesRead = inStream.read(buffer,readPos,BLOCKSIZE)) != -1) {
        outStream.write(buffer,writePos,bytesRead);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am doing a simple coin flipping experiment for class that involves flipping a
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
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 just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into

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.