imrichardcole
imrichardcole

Reputation: 4685

Is it possible to clear ByteArrayOutputStream

Let's assume I have the following:

final ByteArrayOutputStream boas = new ByteArrayOutputStream();
final byte[] sample = {1,2,3,4,5,6,7,8,9,10};
boas.write(sample);

At this point the backing array byte buf[] within boas contains the 10 bytes above (and 22 padding bytes).

If I call reset on boas the count and size and all other factors indicate it's empty but internally byte buf[] remains unchanged and populated.

Is there anyway to truly clear this down without creating an entirely new ByteArrayOutputStream?

More generally, is there a reason why ByteArrayOutputStream behaves like this rather than emptying the byte[]?

Upvotes: 9

Views: 18551

Answers (4)

Little Santi
Little Santi

Reputation: 8803

If your problem is to cope with old legacy code without modifying it, I understand there is some subclass of ByteArrayOutputStream reading the protected buf and count variables.

Given that:

  1. JRE authors should have never made variables protected (it violates the most basic principle of OO encapsulation).
  2. Legacy code author should have interpreted correctly the ByteArrayOutputStream API.
  3. Current programmers (you) should probably throw away all the legacy code and write it down from the scratch.

... Given all of these points, I said, I guess that any fast solution to fix the problem easily won't do much more harm.

For example: If -as I understand- you have to maintain some LegacyOutputStream class as subclass of ByteArrayOutputStream, could you write down your own subclass?

public class MyFixOutputStream extends LegacyOutputStream
{
    @Override
    public void reset()
    {
        // Do here with this.buf wathever blunders required by LegacyOutputStream.
    }
}

... and then, replace in the existing code any use of LegacyOutputStream by MyFixOutputStream?

Upvotes: -1

Seo Jeong
Seo Jeong

Reputation: 54

No you cannot unless do what others say in comments on your question. Check amortized analysis(https://en.wikipedia.org/wiki/Amortized_analysis). I guess the cost of erasing internal buffer memory every time is bad for performance.

Upvotes: 0

wero
wero

Reputation: 33000

The Javadoc of ByteArrayOutputStream.reset() states

Resets the count field of this byte array output stream to zero, so that all currently accumulated output in the output stream is discarded. The output stream can be used again, reusing the already allocated buffer space.

Performance wise it makes sense to reuse an already allocated buffer.

If you want a different behaviour you can derive a class from ByteArrayOutputStream and override reset(). Both buf[] and count are protected members, so for instance you could clear buf if you want.

Upvotes: 15

user5547025
user5547025

Reputation:

ByteArrayOutputStream provides no method to empty the byte[] buf. Take a look at the source code. buf can not directly be modified.

You could write a class the inherits from ByteArrayOutputStream and add a method to empty buf.

Upvotes: 1

Related Questions