-2

I have an stream that is created with a fixed size. The buffer is empty. I want to be able to get the size of this buffer. What is an efficient way of doing this?

Here is what I have done.

static OutputStream CreateBuffer()
{
    return (OutputStream) new ByteArrayOutputStream(100);
}

static int GetBufferSize(OutputStream out)
{
    return ( (ByteArrayOutputStream) out).size();
}


public static void main(String[] args)
{
    System.out.println(GetBufferSize(CreateBuffer()));
}

I expect 100. I know why it is 0 -- the stream was only allocated but nothing has actually been written to it yet. What is a good implementation to get the size of an empty outputstream without actually copying data?

Holger
  • 285,553
  • 42
  • 434
  • 765
nikk
  • 2,627
  • 5
  • 30
  • 51
  • 1
    If you're hoping to get out 100, I'm fairly certain what you're asking for is impossible. Not on a vanilla `ByteArrayOutputStream`, not without evil reflection hackery. The API is designed specifically to avoid answering this question. – Louis Wasserman Jul 13 '16 at 22:14
  • 1
    Don't put the answer in your question. – shmosel Jul 13 '16 at 22:48
  • it was a mistake. I was updating with proper foramtting, ended up adding the wrong version. corrected. thank you. – nikk Jul 13 '16 at 22:56

2 Answers2

1

You can get the internal buffer size by subclassing ByteArrayOutputStream:

private static class ByteArrayOutputStreamWithCapacity extends ByteArrayOutputStream {
    public ByteArrayOutputStreamWithCapacity(int size) {
        super(size);
    }

    public synchronized int capacity() {
        return buf.length;
    }
}

void CreateBuffer()
{
    OutputStream out = new ByteArrayOutputStreamWithCapacity(100);
}

int GetBufferSize(OutPutStream out)
{
    return ( (ByteArrayOutputStreamWithCapacity) out). capacity();
}
shmosel
  • 49,289
  • 6
  • 73
  • 138
1

I have a java.io.OutputStream that is created with a fixed size.

No you don't. You have a ByteArrayOutputStream that is created with a non-zero initial capacity.

The buffer is empty. I want to be able to get the size of this buffer.

The logical size of the buffer is zero. The initial capacity of the buffer is whatever you specified in the constructor.

What is an efficient way of doing this?

Remember what you specified in the constructor.

I expect 100.

You should expect zero. See the Javadoc.

I know why it is 0 -- the stream was only allocated but nothing has actually been written to it yet.

Exactly. So why you expected 100 is a mystery.

What is a good implementation to get the size of an empty outputstream without actually copying data?

'The size of an empty output stream' is zero. The initial capacity of your ByteArrayOutputStream is whatever you specified when constructing it. Unclear why you think you need this. The capacity will change efficiently as required. You seem to be confused between 'fixed size' and 'initial capacity'. What you specified was the latter.

user207421
  • 305,947
  • 44
  • 307
  • 483