Skip to content

DataOutput and DataInput support in FastByteArray streams #360

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

magicprinc
Copy link
Contributor

@magicprinc magicprinc commented Jun 22, 2025

  1. FastByteArrayOutputStream
    toString(Charset) as in ByteArrayOutputStream
    implements DataOutput (without IOException)

  2. FastByteArrayInputStream
    fast in-memory implementation of all inherited methods
    implements DataInput (without IOException)

With this addition FastByteArray*Streams can be used instead of JDK ByteBuffer:
I see a lot of potential 🔥

String latin1text = "   latin1 text is ok";

var sms = new FastByteArrayOutputStream(160);
sms.writeByte(0);// udh len

sms.writeByte(IE_CONCATENATED_SHORT_MESSAGES_16BIT_REF);
sms.writeByte(4);// iel
sms.writeShort(12345);
sms.writeByte(2);
sms.writeByte(1);

sms.writeByte(IE_APPLICATION_PORT_ADDRESSING_SCHEME_16BIT_ADDR);
sms.write(4);
sms.writeShort(8080);
sms.writeShort(12728);

sms.array[0] = (byte)(sms.position() -1);

sms.writeBytes(latin1text);

assertEquals(
	"0c08043039020105041f9031b82020206c6174696e312074657874206973206f6b",
	toHex(sms.toByteArray())
);
//
var parse = new FastByteArrayInputStream(sms.toByteArray());
int len = parse.readByte();
assertEquals(12, len);
assertEquals(IE_CONCATENATED_SHORT_MESSAGES_16BIT_REF, parse.readByte());
assertEquals(4, parse.readUnsignedByte());// iel
assertEquals(12345, parse.readShort());
assertEquals(2, parse.readUnsignedByte());
assertEquals(1, parse.readUnsignedByte());

assertEquals(IE_APPLICATION_PORT_ADDRESSING_SCHEME_16BIT_ADDR, parse.readByte());
assertEquals(4, parse.readUnsignedByte());
assertEquals(8080, parse.readUnsignedShort());
assertEquals(12728, parse.readUnsignedShort());

assertEquals(latin1text, parse.readLine());// just as a bad example!

@magicprinc
Copy link
Contributor Author

magicprinc commented Jun 22, 2025

no IOException, no wrappers, out-of-box, primitive types friendly, compact and easy

I have 100% test coverage locally, but my unit tests need refactoring to push them to this high quality project.
And I feel more safe if you or somebody's "production" 😅 extra test this code.

PS:
This is my last PR 🥳
the quality of the fastutil is very high, and feature set is very rich ❤
I sent everything I have found

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant