0

I have a byte[] which contains serialised C++ data types (so it has a weird format that the standard Java serialisation classes can't understand). I've created my own deserialiser which matches flags to types and converts it into the appropriate object, like so (input could be something like this int64 with value 4 [3, 0, 0, 0, 0, 0, 0, 0, 4], 3 is the flag for int64)

private final ByteBuffer byteWrapper;

// member ByteBuffer which is allocated in the ctor

ctor { byteWrapper = ByteBuffer.allocateDirect(8); }

// somewhere in the parse function, this case matches an int64
case 3:
{
        return byteWrapper.wrap(ios.getBytes(8)).getLong();
}

Where ios contains the full serialisation data for easy reading. Now this works fine, but high throughput is very important for this piece of the code and I'm not a regular Java programmer, so I'm just wondering if anyone has any suggestions for speeding this process up, either by reworking what I've got or using different classes/functions entirely or anything? Memory is not an issue, I'm just interested in speed

1 Answer 1

1

Dont copy your data into a new buffer object. Read it directly from the ios buffer.

byte type = ios.get();
switch (type) {
    case 3:
        return ios.getLong();
}

All getters in ByteBuffer will increase the position according to the length of the data type. get() increases the position by 1, getLong() by 8, getFloat() by 4... You can directly use ios.getLong(). Assuming that the values are written one after the other, the new position will point to the next type-byte. So you can directly call ios.get() to get the next type.

Edit:

If ios is an InputStream you can use DataInputStream to read. It also supports various datatypes.

DataInputStream input = new DataInputStream(ios);

byte type = input.readByte();
switch (type) {
case 3:
    return input.readLong();
}

In Java, there are essentially two packages, that provide the functionality you are looking for. java.io and java.nio. java.io contains Streams, while java.nio contains Buffers. java.nio is faster, because it uses very low level memory operations. If you are looking for speed, you should use that. That also means you should change the type of ios to ByteBuffer if possible and then use my first approach.

Sign up to request clarification or add additional context in comments.

2 Comments

That sounds good but it looks like my ios (java.io.InputStream) doesn't have that method, is there a different stream I should be using?
@Madden edited once more theres something you should consider.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.