Skip to content
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

Ability to disable array content printout on UnsafeBuffer#toString(). #166

Merged
merged 1 commit into from
Feb 8, 2019
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
6 changes: 5 additions & 1 deletion agrona/src/main/java/org/agrona/ExpandableArrayBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
*/
public class ExpandableArrayBuffer implements MutableDirectBuffer
{

private static final boolean SHOULD_PRINT_ARRAY_CONTENT =
!Boolean.getBoolean(DISABLE_ARRAY_CONTENT_PRINTOUT_PROP_NAME);

/**
* Maximum length to which the underlying buffer can grow. Some JVMs set bits in the last few bytes.
*/
Expand Down Expand Up @@ -1107,7 +1111,7 @@ public int compareTo(final DirectBuffer that)
public String toString()
{
return "ExpandableArrayBuffer{" +
"byteArray=" + Arrays.toString(byteArray) +
"byteArray=" + (SHOULD_PRINT_ARRAY_CONTENT ? Arrays.toString(byteArray) : byteArray) +
'}';
}
}
6 changes: 6 additions & 0 deletions agrona/src/main/java/org/agrona/MutableDirectBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
*/
public interface MutableDirectBuffer extends DirectBuffer
{

/**
* Don't print the content of the array while calling toString() on buffer instance.
*/
String DISABLE_ARRAY_CONTENT_PRINTOUT_PROP_NAME = "agrona.disable.array.printout";

/**
* Is this buffer expandable to accommodate putting data into it beyond the current capacity?
*
Expand Down
5 changes: 4 additions & 1 deletion agrona/src/main/java/org/agrona/concurrent/UnsafeBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public class UnsafeBuffer implements AtomicBuffer

public static final String DISABLE_BOUNDS_CHECKS_PROP_NAME = "agrona.disable.bounds.checks";
public static final boolean SHOULD_BOUNDS_CHECK = !Boolean.getBoolean(DISABLE_BOUNDS_CHECKS_PROP_NAME);
public static final boolean SHOULD_PRINT_ARRAY_CONTENT =
!Boolean.getBoolean(DISABLE_ARRAY_CONTENT_PRINTOUT_PROP_NAME);

private long addressOffset;
private int capacity;
Expand Down Expand Up @@ -1752,7 +1754,8 @@ public String toString()
return "UnsafeBuffer{" +
"addressOffset=" + addressOffset +
", capacity=" + capacity +
", byteArray=" + Arrays.toString(byteArray) +
", byteArray=" + (SHOULD_PRINT_ARRAY_CONTENT ?
Arrays.toString(byteArray) : byteArray) +
", byteBuffer=" + byteBuffer +
'}';
}
Expand Down
24 changes: 22 additions & 2 deletions agrona/src/test/java/org/agrona/concurrent/UnsafeBufferTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.junit.experimental.theories.Theory;
import org.junit.runner.RunWith;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.nio.ByteBuffer;
import java.util.Arrays;

Expand All @@ -30,8 +32,7 @@
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.lessThan;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.*;

@RunWith(Theories.class)
public class UnsafeBufferTest
Expand Down Expand Up @@ -236,6 +237,25 @@ private void putAscii(final UnsafeBuffer buffer, final String value)
buffer.putBytes(INDEX, value.getBytes(US_ASCII));
}

@Test
public void shouldSkipArrayContentPrintout() throws Exception
{
final Field settingField = UnsafeBuffer.class.getDeclaredField("SHOULD_PRINT_ARRAY_CONTENT");
settingField.setAccessible(true);
final Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(settingField, settingField.getModifiers() & ~Modifier.FINAL);

final byte[] backingArray = new byte[10];
final UnsafeBuffer unsafeBuffer = new UnsafeBuffer(backingArray);

settingField.set(null, true);
assertTrue(unsafeBuffer.toString().contains(Arrays.toString(backingArray)));

settingField.set(null, false);
assertFalse(unsafeBuffer.toString().contains(Arrays.toString(backingArray)));
}

@DataPoints
public static int[][] valuesAndLengths()
{
Expand Down