Skip to content
Closed
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 @@ -38,6 +38,7 @@ public RecordHeaders() {
}

public RecordHeaders(Header[] headers) {
checkNullHeader(headers);
if (headers == null) {
this.headers = new ArrayList<>();
} else {
Expand All @@ -46,6 +47,7 @@ public RecordHeaders(Header[] headers) {
}

public RecordHeaders(Iterable<Header> headers) {
checkNullHeader(headers);
//Use efficient copy constructor if possible, fallback to iteration otherwise
if (headers == null) {
this.headers = new ArrayList<>();
Expand Down Expand Up @@ -117,6 +119,23 @@ public Header[] toArray() {
return headers.isEmpty() ? Record.EMPTY_HEADERS : headers.toArray(new Header[headers.size()]);
}

private void checkNullHeader(Header[] headers) throws IllegalArgumentException {
if (headers != null) {
if (!Arrays.stream(headers).allMatch(Objects::nonNull)) {
throw new IllegalArgumentException("header value cannot be null.");
}
}
}

private void checkNullHeader(Iterable<Header> headers) throws IllegalArgumentException {
if (headers != null) {
headers.forEach(header -> {
if (header == null)
throw new IllegalArgumentException("header value cannot be null.");
});
}
}

private void checkKey(String key) {
if (key == null)
throw new IllegalArgumentException("key cannot be null.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,16 @@ public void shouldThrowNpeWhenAddingNullHeader() {
new RecordHeaders().add(null);
}

@Test(expected = java.lang.IllegalArgumentException.class)
public void shouldThrowExceptionWhenPassingArrayWithNullValueInConstructor() {
new RecordHeaders(new Header[] {new RecordHeader("key", "value".getBytes()), null});
}

@Test(expected = java.lang.IllegalArgumentException.class)
public void shouldThrowExceptionWhenPassingIterableWithNullValueInConstructor() {
new RecordHeaders(Arrays.asList(new Header[] {new RecordHeader("key", "value".getBytes()), null}));
}

private int getCount(Headers headers) {
int count = 0;
Iterator<Header> headerIterator = headers.iterator();
Expand Down