Skip to content

Commit

Permalink
Add IBDO method to ensure data is available.
Browse files Browse the repository at this point in the history
  • Loading branch information
James Cover jdcove2 committed Mar 6, 2024
1 parent 5b3ff6f commit d9b6590
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/main/java/emissary/core/BaseDataObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,16 @@ public void setData(@Nullable final byte[] newData, final int offset, final int
safeUsageChecker.resetCacheThenRecordSnapshot(theData);
}

/**
* Checks if the data is defined with a non-zero length.
*
* @return if data is undefined or zero length.
*/
@Override
public boolean isDataEmpty() throws IOException {
return getDataState() == DataState.NO_DATA || getChannelSize() == 0;
}

/**
* Convenience method to get the size of the channel or byte array providing access to the data.
*
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/emissary/core/IBaseDataObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ enum MergePolicy {
*/
void setData(final byte[] newData, int offset, int length);

/**
* Checks if the data is defined with a non-zero length.
*
* @return if data is undefined or zero length.
*/
boolean isDataEmpty() throws IOException;

/**
* Set the byte channel factory using whichever implementation is providing access to the data.
*
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/emissary/core/BaseDataObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,17 @@ void testExceptionWhenGettingChannelSize() throws IOException {
}
}

@Test
void testIsDataEmpty() throws IOException {
final IBaseDataObject nullDataIbdo = new BaseDataObject();
final IBaseDataObject zeroLengthDataIbdo = new BaseDataObject(new byte[0], "zeroLengthDataIbdo");
final IBaseDataObject nonZeroLengthDataIbdo = new BaseDataObject(new byte[1], "nonZeroLengthDataIbdo");

assertTrue(nullDataIbdo.isDataEmpty());
assertTrue(zeroLengthDataIbdo.isDataEmpty());
assertFalse(nonZeroLengthDataIbdo.isDataEmpty());
}

@Test
void testDataLengthBothNull() {
BaseDataObject bdo = new BaseDataObject();
Expand Down

0 comments on commit d9b6590

Please sign in to comment.