• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 279
  • Last Modified:

Variable Buffer Size

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
            }else
            {    long seeD=200030L;
                  Random gen=new Random(seeD);
                  int extend=gen.nextInt();
                  if(extend>0){
                  bufferCapacity=+extend;
                 
                 
                  }
                  
            
            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()
0
sanjoybasu
Asked:
sanjoybasu
1 Solution
 
StillUnAwareCommented:
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);
  }
0
 
hoomanvCommented:
why dont you use java.io.ByteArrayInputStream ?
0
 
objectsCommented:

// 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
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

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

Why would you want to do that?
0
 
Mayank SAssociate Director - Product EngineeringCommented:
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 () ;
do
{
  int c = inputStream.read () ;
  if ( c >= 0 )
    baos.write ( c ) ;
  else
    break ;
} while ( true ) ;

Now you can convert it to a byte array using: byte[] array = baos.toByteArray () ;
0
 
fffej78Commented:
This chunk of code:

long seeD=200030L;
Random gen=new Random(seeD);
int extend=gen.nextInt();
if(extend>0)
  bufferCapacity+=extend;

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?  
0
 
StillUnAwareCommented:
glad to help
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now