Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.TimeZone;
import java.util.UUID;
Expand Down Expand Up @@ -667,7 +668,7 @@ public static class ArrayValue {

int nextPos = 0;

ArrayValue(Class<?> itemType, int length) {
public ArrayValue(Class<?> itemType, int length) {
this.itemType = itemType;
this.length = length;

Expand Down Expand Up @@ -721,6 +722,34 @@ public synchronized <T> List<T> asList() {
}
return (List<T>) list;
}

/**
* Returns internal array. This method is only useful to work with array of primitives (int[], boolean[]).
* Otherwise use {@link #getArrayOfObjects()}
*
* @return
*/
public Object getArray() {
return array;
}

/**
* Returns array of objects.
* If item type is primitive then all elements will be converted into objects.
*
* @return
*/
public Object[] getArrayOfObjects() {
if (itemType.isPrimitive()) {
Object[] result = new Object[length];
for (int i = 0; i < length; i++) {
result[i] = Array.get(array, i);
}
return result;
} else {
return (Object[]) array;
}
}
}

public static class EnumValue extends Number {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.lang.reflect.Array;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.TimeZone;

import org.testng.Assert;
Expand Down Expand Up @@ -176,4 +178,16 @@ private Object[][] provideDateTimeTestData() {
};
}

@Test
public void testArrayValue() throws Exception {
BinaryStreamReader.ArrayValue array = new BinaryStreamReader.ArrayValue(int.class, 10);

for (int i = 0; i < array.length(); i++) {
array.set(i, i);
}

int[] array1 = (int[]) array.getArray();
Object[] array2 = array.getArrayOfObjects();
Assert.assertEquals(array1.length, array2.length);
}
}
Loading
Loading