Skip to content

BUG: FastByteArrayOutputStream.write(byte[],int,int) can fail to move position #367

@magicprinc

Description

@magicprinc
@Test
void _bug_write_array0 () {
  val o = new it.unimi.dsi.fastutil.io.FastByteArrayOutputStream ();
  o.write(new byte[]{1,2,3,4,5});
  assertEquals(5, o.length());
  assertEquals(5, o.position());

  o.position(0);
  o.write(new byte[]{1,2,3});
  assertEquals(5, o.length());
  assertEquals(0, o.position());//!!! MUST be 3: 0+3
}

fix

@Override
public void write (byte[] b, int off, int len) {
  ByteArrays.ensureOffsetLength(b, off, len);
  if (position + len > buf.length)
      buf = ByteArrays.grow(buf, position + len, position);
  System.arraycopy(b, off, buf, position, len);
  position += len;
  if (count < position) count = position;
}

PS: please also see my PR #360 with useful extra functionality

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions