Skip to content

Added bound checking, toString() and doc update for unsigned int #8545

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 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions java/src/main/java/com/google/flatbuffers/IntVector.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,49 @@ public final class IntVector extends BaseVector {
* `vector`.
*/
public IntVector __assign(int _vector, ByteBuffer _bb) {
if(_bb == null){
throw new IllegalArgumentException("ByteBuffer cannot be null");
}
__reset(_vector, Constants.SIZEOF_INT, _bb); return this;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[");
for(int i = 0; i < length(); i++){
if(i > 0){
sb.append(", ");
}
sb.append(get(i));
}
sb.append("]");
return sb.toString();
}

/**
* Reads the integer at the given index.
*
* @param j The index from which the integer will be read.
* @return the 32-bit value at the given index.
*/
public int get(int j) {
if(j < 0 || j >= length()){
throw new IndexOutOfBoundsException("Index " + j + " is out of bounds for length " + length());
}
return bb.getInt(__element(j));
}

/**
* Reads the integer at the given index, zero-extends it to type long, and returns the result,
* which is therefore in the range 0 through 4294967295.
*
* Java does not natively support unsigned integers, so this method returns a 'long' to represent the unsigned 32-bit value.
*
* @param j The index from which the integer will be read.
* @return the unsigned 32-bit at the given index.
*/

public long getAsUnsigned(int j) {
return (long) get(j) & 0xFFFFFFFFL;
}
Expand Down