Variable Buffer Size

Posted on 2006-06-03
Medium Priority
Last Modified: 2011-10-03
I am trying to write a Class which is subclass of Inputstream. In the class I'm trying to write a method called advRead which reads a stream from file. What I am trying to achive is byte[] buffer in read method should be variable. I inittialize by

Length=30; // Small foot print  //
byte[] buffer= new byte[Length];

In the same class I wrote another method which checks the buffer size with existing buffer capacity and return a integer
Code of the  method
public int adjustBufferSize(int bufferUsed, int bufferCapacity){
            if ((bufferCapacity-bufferUsed)>=10){
                  // Do Nothing
            {    long seeD=200030L;
                  Random gen=new Random(seeD);
                  int extend=gen.nextInt();
            return bufferCapacity;

Can any one please give me hint how do I use this method such that after every read() operation (till -1 or EOF is encountered) it calls the method adjustBufferSize() and replace value of Length with integer returend by adjustBufferSize()
Question by:sanjoybasu
LVL 14

Accepted Solution

StillUnAware earned 750 total points
ID: 16822700
first of all, I think this line:

  bufferCapacity=+extend; // effectively it's the same as 'bufferCapacity  = extend'

should be changed to

  bufferCapacity += extend; // and this 'bufferCapacity = bufferCapacity + extend'

And for Your question, You'll have to override the read methods, e.g.:

  public int read(byte[] b) {
    int l = super.read(b); // call the InputStream's read
    //now that You have the actual length of an input, do what ever You want with it:
    return adjustBufferSize(l, b.length);
LVL 14

Expert Comment

ID: 16822702
why dont you use java.io.ByteArrayInputStream ?
LVL 92

Expert Comment

ID: 16822710

// read at end of buffer
int nread = read(buffer, bufferUsed);
// adjust buffer size
Length = adjustBufferSize(bufferUsed, Length);

// now create new buffer and copy existing data into it
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

LVL 86

Expert Comment

ID: 16822722
>>What I am trying to achive is byte[] buffer in read method should be variable

Why would you want to do that?
LVL 30

Expert Comment

by:Mayank S
ID: 16823726
You can use a ByteArrayOutputStream for holding as many variable number of bytes as you want, if that's what you want to do:

ByteArrayOutputStream baos = new ByteArrayOutputStream () ;
  int c = inputStream.read () ;
  if ( c >= 0 )
    baos.write ( c ) ;
    break ;
} while ( true ) ;

Now you can convert it to a byte array using: byte[] array = baos.toByteArray () ;

Expert Comment

ID: 16824140
This chunk of code:

long seeD=200030L;
Random gen=new Random(seeD);
int extend=gen.nextInt();

Strikes me as rather bizarre :)  You are generating a non-random number each time because you are seeding the random number generator so it always produces the same sequence.

If we ignore the fact that I have absolutely no idea what the code is trying to achieve then perhaps it would be better as

if ( new Random().nextBoolean() )
  bufferCapacity += extend;

And you should probably store the random number generator as a member variable, rather than recreating it everytime.

I think the real question here is what are you trying to achieve?  
LVL 14

Expert Comment

ID: 16845281
glad to help

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month17 days, 2 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question