Variable Buffer Size

Posted on 2006-06-03
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

    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 =; // 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

    why dont you use ?
    LVL 92

    Expert Comment


    // 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
    LVL 86

    Expert Comment

    >>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

    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 = () ;
      if ( c >= 0 )
        baos.write ( c ) ;
        break ;
    } while ( true ) ;

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

    Expert Comment

    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

    glad to help

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Suggested Solutions

    Title # Comments Views Activity
    Fibonacci challenge 11 61
    Spring Controller - inheritance in request parameter 3 29
    strCopies  challenge 17 60
    groovy example issue 10 32
    For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
    Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
    Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
    This video teaches viewers about errors in exception handling.

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now