Skip to content

Clarify and correct EndianUtils and SwappedDataInputStream API doc#566

Merged
garydgregory merged 3 commits into
apache:masterfrom
elharo:copydir
Jan 26, 2024
Merged

Clarify and correct EndianUtils and SwappedDataInputStream API doc#566
garydgregory merged 3 commits into
apache:masterfrom
elharo:copydir

Conversation

@elharo

@elharo elharo commented Jan 25, 2024

Copy link
Copy Markdown
Contributor

Most of the methods in this class are badly named. Except for the swap methods, they all just read and write in little endian order. E.g. consider readSwappedInteger

    public static int readSwappedInteger(final InputStream input) throws IOException {
        final int value1 = read(input);
        final int value2 = read(input);
        final int value3 = read(input);
        final int value4 = read(input);
        return ((value1 & 0xff) << 0) + ((value2 & 0xff) << 8) + ((value3 & 0xff) << 16) + ((value4 & 0xff) << 24);
    }

This always reads four bytes from an input stream and always considers the first byte read to be the least significant. It would have been better named readLittleEndianInteger.

I'm tempted to deprecate the whole thing and replace it with something better named, but for now at least let's correct the Java doc.

@garydgregory

@elharo elharo changed the title Clarify and correct API doc Clarify and correct EndianUtils API doc Jan 25, 2024
/**
* Reads a "float" value from a byte array at a given offset. The value is
* converted to the opposed endian system while reading.
* Reads a little endian "float" value from a byte array at a given offset.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that using {@code _type_} would be better here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe. What's really confusing here is we need to distinguish two different "floats":

  1. The Java float primitive in memory in the VM.
  2. A 4 byte chunk read from an input stream or an array.

I'm not sure I've got this distinction clear enough yet. Let me play with it a bit more.

@elharo

elharo commented Jan 26, 2024

Copy link
Copy Markdown
Contributor Author

PTAL


/**
* Converts an "int" value between endian systems.
* Converts an {@code int} value between endian systems.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be clear that the toggle is between big and little-endian. We'll then avoid the shadow of even thinking about middle-endian madness.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Curious, has any system ever actually used middle-endianess or is it just a theoretical possibility? Anyway, let me update this.

@garydgregory

Copy link
Copy Markdown
Member

According to Wikipedia, PDP has something close also referred to PDP-endian!

@elharo elharo left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also corrected Javadoc for SwappedDataInputStream, which had all these problems and some more copy paste errors besides.

@elharo elharo changed the title Clarify and correct EndianUtils API doc Clarify and correct EndianUtils and SwappedDataInputStream API doc Jan 26, 2024
@codecov-commenter

codecov-commenter commented Jan 26, 2024

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 85.99%. Comparing base (4bd93ec) to head (552d6ad).
Report is 975 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff            @@
##             master     #566   +/-   ##
=========================================
  Coverage     85.98%   85.99%           
- Complexity     3496     3497    +1     
=========================================
  Files           231      231           
  Lines          8251     8253    +2     
  Branches        964      964           
=========================================
+ Hits           7095     7097    +2     
- Misses          865      866    +1     
+ Partials        291      290    -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

* Reads an unsigned integer (32-bit) value from a byte array at a given
* offset. The value is converted to the opposed endian system while
* reading.
* Reads a little endian unsigned integer (32-bit) value from a byte array at a given

@garydgregory garydgregory Jan 26, 2024

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @elharo

This is slightly confusing to me: The method returns a long (64-bit) but we are reading an 32-bit integer? Should we clarify this point? Something like "Reads a foo as a bar".

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since Java's ints are signed you can't fit the unsigned int values from ~2.2 billion to ~4 billion into an int, so you have to return a long. The long will always be <= 2^32, but might be greater than Integer.MAX_INT. Not sure how much of that to say here.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, the comment already mentions "unsigned". TY for the clarification.

@garydgregory garydgregory merged commit 96de251 into apache:master Jan 26, 2024
@elharo elharo deleted the copydir branch January 26, 2024 15:14
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.

3 participants