[e-lang] Joe-E ByteArray API for working with streams
David Wagner
daw at cs.berkeley.edu
Sat Sep 22 19:01:01 EDT 2007
Tyler writes:
>I'd like to add a couple new members to the ByteArray class to make it
>easier to work with streams. In particular, the method [open]:
>and the inner class [Generator]:
Looks good to me.
One minor nitpick on the inner class Generator:
> public void
> write(final byte[] b, final int off, final int len) {
> if (size + len > buffer.length) {
> System.arraycopy(buffer,0, buffer = new byte[size+len],0, size);
> }
> System.arraycopy(b, off, buffer, size, len);
> size += len;
> }
If someone with a reference to a Generator g calls
g.write(.., .., -1);
then this destroys the Generator's internal invariants and renders
it in an inconsistent state. In particular, the body of the method
will create a new byte array that is a bit shorter and whose elements
are initialized to zero, set g.buffer to that new byte array, and
then throw IndexOutOfBoundsException.
If I understand correctly, I believe this indicates that Generator
is not defensively consistent. For instance, here is a (ab)use case.
Alice creates a Generator, and writes some data to it. She then
gives Bob a reference to her Generator, thinking that all Bob can
do is to append data (but not erase the data that Alice has already
written). Unbeknownst to her, Bob can destroy the data she already
wrote. Also, if Alice later appends some more data of her own after
Bob has had a chance to interact with the Generator, then Alice's
attempt to write data may unexpectedly throw.
I don't know whether this matters much. It seems like a minor
blemish.
More information about the e-lang
mailing list