Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
m-anthony authored Jan 30, 2017
2 parents d4c65f9 + 8a51391 commit d680f1d
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 40 deletions.
17 changes: 8 additions & 9 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ apply plugin: 'osgi'
defaultTasks 'clean', 'build', 'install'

group = 'org.agrona'
version = '0.9.2-SNAPSHOT'
version = '0.9.3-SNAPSHOT'
ext.isReleaseVersion = !version.endsWith("SNAPSHOT")

ext {
Expand Down Expand Up @@ -74,25 +74,24 @@ task(generatePrimitiveExpansions, type: JavaExec) {
}

dependencies {
checkstyle 'com.puppycrawl.tools:checkstyle:7.3'
checkstyle 'com.puppycrawl.tools:checkstyle:7.4'
checkstyle 'com.github.sevntu.checkstyle:sevntu-checks:1.23.0'

testCompile(
'org.hamcrest:hamcrest-all:1.3',
'junit:junit:4.12',
'org.mockito:mockito-core:2.5.5',
'org.mockito:mockito-core:2.6.8',
sourceSets.agent.output
)


agentCompile "net.bytebuddy:byte-buddy:1.6.0"
agentCompile sourceSets.main.output

agentCompile "net.bytebuddy:byte-buddy:1.6.0"
agentCompile sourceSets.main.output
}


checkstyle {
configFile = new File(rootDir, 'config/checkstyle/checkstyle.xml')
toolVersion = '7.3'
toolVersion = '7.4'
}

javadoc {
Expand Down Expand Up @@ -157,7 +156,7 @@ uploadArchives {
}

pom.project {
name = 'Agrona'
name = 'agrona'
packaging = 'pom'
// optionally artifactId can be defined here
description = 'High performance primitives and utility library.'
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
rootProject.name = 'Agrona'
rootProject.name = 'agrona'

57 changes: 44 additions & 13 deletions src/main/java/org/agrona/ExpandableArrayBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -511,22 +511,32 @@ public void putChar(final int index, final char value)

public String getStringAscii(final int index)
{
final int length = getInt(index);
boundsCheck0(index, SIZE_OF_INT);

final int length = UNSAFE.getInt(byteArray, ARRAY_BASE_OFFSET + index);

return getStringAscii(index, length);
}

public String getStringAscii(final int index, final ByteOrder byteOrder)
{
final int length = getInt(index, byteOrder);
boundsCheck0(index, SIZE_OF_INT);

int bits = UNSAFE.getInt(byteArray, ARRAY_BASE_OFFSET + index);
if (NATIVE_BYTE_ORDER != byteOrder)
{
bits = Integer.reverseBytes(bits);
}

final int length = bits;

return getStringAscii(index, length);
}

public String getStringAscii(final int index, final int length)
{
final byte[] stringInBytes = new byte[length];
getBytes(index + SIZE_OF_INT, stringInBytes, 0, length);
System.arraycopy(byteArray, index + SIZE_OF_INT, stringInBytes, 0, length);

return new String(stringInBytes, US_ASCII);
}
Expand Down Expand Up @@ -584,7 +594,7 @@ public int putStringAscii(final int index, final String value, final ByteOrder b
public String getStringWithoutLengthAscii(final int index, final int length)
{
final byte[] stringInBytes = new byte[length];
getBytes(index, stringInBytes, 0, length);
System.arraycopy(byteArray, index, stringInBytes, 0, length);

return new String(stringInBytes, US_ASCII);
}
Expand Down Expand Up @@ -613,22 +623,32 @@ public int putStringWithoutLengthAscii(final int index, final String value)

public String getStringUtf8(final int index)
{
final int length = getInt(index);
boundsCheck0(index, SIZE_OF_INT);

final int length = UNSAFE.getInt(byteArray, ARRAY_BASE_OFFSET + index);

return getStringUtf8(index, length);
}

public String getStringUtf8(final int index, final ByteOrder byteOrder)
{
final int length = getInt(index, byteOrder);
boundsCheck0(index, SIZE_OF_INT);

int bits = UNSAFE.getInt(byteArray, ARRAY_BASE_OFFSET + index);
if (NATIVE_BYTE_ORDER != byteOrder)
{
bits = Integer.reverseBytes(bits);
}

final int length = bits;

return getStringUtf8(index, length);
}

public String getStringUtf8(final int index, final int length)
{
final byte[] stringInBytes = new byte[length];
getBytes(index + SIZE_OF_INT, stringInBytes, 0, length);
System.arraycopy(byteArray, index + SIZE_OF_INT, stringInBytes, 0, length);

return new String(stringInBytes, UTF_8);
}
Expand All @@ -651,8 +671,10 @@ public int putStringUtf8(final int index, final String value, final int maxEncod
throw new IllegalArgumentException("Encoded string larger than maximum size: " + maxEncodedSize);
}

putInt(index, bytes.length);
putBytes(index + SIZE_OF_INT, bytes, 0, bytes.length);
ensureCapacity(index, SIZE_OF_INT + bytes.length);

UNSAFE.putInt(byteArray, ARRAY_BASE_OFFSET + index, bytes.length);
System.arraycopy(bytes, 0, byteArray, index + SIZE_OF_INT, bytes.length);

return SIZE_OF_INT + bytes.length;
}
Expand All @@ -665,24 +687,33 @@ public int putStringUtf8(final int index, final String value, final ByteOrder by
throw new IllegalArgumentException("Encoded string larger than maximum size: " + maxEncodedSize);
}

putInt(index, bytes.length, byteOrder);
putBytes(index + SIZE_OF_INT, bytes, 0, bytes.length);
ensureCapacity(index, SIZE_OF_INT + bytes.length);

int bits = bytes.length;
if (NATIVE_BYTE_ORDER != byteOrder)
{
bits = Integer.reverseBytes(bits);
}

UNSAFE.putInt(byteArray, ARRAY_BASE_OFFSET + index, bits);
System.arraycopy(bytes, 0, byteArray, index + SIZE_OF_INT, bytes.length);

return SIZE_OF_INT + bytes.length;
}

public String getStringWithoutLengthUtf8(final int index, final int length)
{
final byte[] stringInBytes = new byte[length];
getBytes(index, stringInBytes, 0, length);
System.arraycopy(byteArray, index, stringInBytes, 0, length);

return new String(stringInBytes, UTF_8);
}

public int putStringWithoutLengthUtf8(final int index, final String value)
{
final byte[] bytes = value != null ? value.getBytes(UTF_8) : NULL_BYTES;
putBytes(index, bytes, 0, bytes.length);
ensureCapacity(index, bytes.length);
System.arraycopy(bytes, 0, byteArray, index, bytes.length);

return bytes.length;
}
Expand Down
107 changes: 90 additions & 17 deletions src/main/java/org/agrona/concurrent/UnsafeBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -1005,24 +1005,45 @@ public void putCharVolatile(final int index, final char value)

public String getStringAscii(final int index)
{
final int length = getInt(index);
if (SHOULD_BOUNDS_CHECK)
{
boundsCheck0(index, SIZE_OF_INT);
}

final int length = UNSAFE.getInt(byteArray, addressOffset + index);

return getStringAscii(index, length);
}

public String getStringAscii(final int index, final ByteOrder byteOrder)
{
final int length = getInt(index, byteOrder);
if (SHOULD_BOUNDS_CHECK)
{
boundsCheck0(index, SIZE_OF_INT);
}

int bits = UNSAFE.getInt(byteArray, addressOffset + index);
if (NATIVE_BYTE_ORDER != byteOrder)
{
bits = Integer.reverseBytes(bits);
}

final int length = bits;

return getStringAscii(index, length);
}

public String getStringAscii(final int index, final int length)
{
final byte[] stringInBytes = new byte[length];
getBytes(index + SIZE_OF_INT, stringInBytes, 0, length);
if (SHOULD_BOUNDS_CHECK)
{
boundsCheck0(index + SIZE_OF_INT, length);
}

final byte[] dst = new byte[length];
UNSAFE.copyMemory(byteArray, addressOffset + index + SIZE_OF_INT, dst, ARRAY_BASE_OFFSET, length);

return new String(stringInBytes, US_ASCII);
return new String(dst, US_ASCII);
}

public int putStringAscii(final int index, final String value)
Expand Down Expand Up @@ -1083,10 +1104,15 @@ public int putStringAscii(final int index, final String value, final ByteOrder b

public String getStringWithoutLengthAscii(final int index, final int length)
{
final byte[] stringInBytes = new byte[length];
getBytes(index, stringInBytes, 0, length);
if (SHOULD_BOUNDS_CHECK)
{
boundsCheck0(index, length);
}

final byte[] dst = new byte[length];
UNSAFE.copyMemory(byteArray, addressOffset + index, dst, ARRAY_BASE_OFFSET, length);

return new String(stringInBytes, US_ASCII);
return new String(dst, US_ASCII);
}

public int putStringWithoutLengthAscii(final int index, final String value)
Expand Down Expand Up @@ -1116,22 +1142,43 @@ public int putStringWithoutLengthAscii(final int index, final String value)

public String getStringUtf8(final int index)
{
final int length = getInt(index);
if (SHOULD_BOUNDS_CHECK)
{
boundsCheck0(index, SIZE_OF_INT);
}

final int length = UNSAFE.getInt(byteArray, addressOffset + index);

return getStringUtf8(index, length);
}

public String getStringUtf8(final int index, final ByteOrder byteOrder)
{
final int length = getInt(index, byteOrder);
if (SHOULD_BOUNDS_CHECK)
{
boundsCheck0(index, SIZE_OF_INT);
}

int bits = UNSAFE.getInt(byteArray, addressOffset + index);
if (NATIVE_BYTE_ORDER != byteOrder)
{
bits = Integer.reverseBytes(bits);
}

final int length = bits;

return getStringUtf8(index, length);
}

public String getStringUtf8(final int index, final int length)
{
if (SHOULD_BOUNDS_CHECK)
{
boundsCheck0(index + SIZE_OF_INT, length);
}

final byte[] stringInBytes = new byte[length];
getBytes(index + SIZE_OF_INT, stringInBytes, 0, length);
UNSAFE.copyMemory(byteArray, addressOffset + index + SIZE_OF_INT, stringInBytes, ARRAY_BASE_OFFSET, length);

return new String(stringInBytes, UTF_8);
}
Expand All @@ -1154,8 +1201,13 @@ public int putStringUtf8(final int index, final String value, final int maxEncod
throw new IllegalArgumentException("Encoded string larger than maximum size: " + maxEncodedSize);
}

putInt(index, bytes.length);
putBytes(index + SIZE_OF_INT, bytes, 0, bytes.length);
if (SHOULD_BOUNDS_CHECK)
{
boundsCheck0(index, SIZE_OF_INT + bytes.length);
}

UNSAFE.putInt(byteArray, addressOffset + index, bytes.length);
UNSAFE.copyMemory(bytes, ARRAY_BASE_OFFSET, byteArray, addressOffset + index + SIZE_OF_INT, bytes.length);

return SIZE_OF_INT + bytes.length;
}
Expand All @@ -1168,24 +1220,45 @@ public int putStringUtf8(final int index, final String value, final ByteOrder by
throw new IllegalArgumentException("Encoded string larger than maximum size: " + maxEncodedSize);
}

putInt(index, bytes.length, byteOrder);
putBytes(index + SIZE_OF_INT, bytes, 0, bytes.length);
if (SHOULD_BOUNDS_CHECK)
{
boundsCheck0(index, SIZE_OF_INT + bytes.length);
}

int bits = bytes.length;
if (NATIVE_BYTE_ORDER != byteOrder)
{
bits = Integer.reverseBytes(bits);
}

UNSAFE.putInt(byteArray, addressOffset + index, bits);
UNSAFE.copyMemory(bytes, ARRAY_BASE_OFFSET, byteArray, addressOffset + index + SIZE_OF_INT, bytes.length);

return SIZE_OF_INT + bytes.length;
}

public String getStringWithoutLengthUtf8(final int index, final int length)
{
if (SHOULD_BOUNDS_CHECK)
{
boundsCheck0(index, length);
}

final byte[] stringInBytes = new byte[length];
getBytes(index, stringInBytes, 0, length);
UNSAFE.copyMemory(byteArray, addressOffset + index, stringInBytes, ARRAY_BASE_OFFSET, length);

return new String(stringInBytes, UTF_8);
}

public int putStringWithoutLengthUtf8(final int index, final String value)
{
final byte[] bytes = value != null ? value.getBytes(UTF_8) : NULL_BYTES;
putBytes(index, bytes, 0, bytes.length);
if (SHOULD_BOUNDS_CHECK)
{
boundsCheck0(index, bytes.length);
}

UNSAFE.copyMemory(bytes, ARRAY_BASE_OFFSET, byteArray, addressOffset + index, bytes.length);

return bytes.length;
}
Expand Down

0 comments on commit d680f1d

Please sign in to comment.