Skip to content

Commit 4c4d348

Browse files
committed
[Android] DirectBuffer cleanup and code tidy
1 parent 90c92f7 commit 4c4d348

File tree

6 files changed

+130
-45
lines changed

6 files changed

+130
-45
lines changed

main/android/uk/co/real_logic/sbe/codec/java/BitUtil.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
* Copyright 2013 Real Logic Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package uk.co.real_logic.sbe.codec.java;
217

318
import java.lang.reflect.Field;
@@ -41,6 +56,7 @@ final class BitUtil
4156
Field positionField = getField(Buffer.class, "position");
4257
POSITION_FIELD_OFFSET = UNSAFE.objectFieldOffset(positionField);
4358
Field effectiveDirectAddressField = getField(Buffer.class, "effectiveDirectAddress");
59+
4460
EFFECTIVE_DIRECT_ADDRESS_FIELD_OFFSET = UNSAFE.objectFieldOffset(effectiveDirectAddressField);
4561
USE_LONG_ADDRESS = effectiveDirectAddressField.getType() == long.class;
4662
MEMORY_ACCESS = USE_LONG_ADDRESS ? new MemoryAccessLongAddress() : new MemoryAccessIntAddress();
@@ -78,11 +94,24 @@ static Unsafe getUnsafe()
7894
return UNSAFE;
7995
}
8096

97+
/**
98+
* Get the instance of {@link MemoryAccess}.
99+
*
100+
* @return the instance of MemoryAccess
101+
*/
81102
public static MemoryAccess getMemoryAccess()
82103
{
83104
return MEMORY_ACCESS;
84105
}
85106

107+
/**
108+
* Gets the value of a static field.
109+
*
110+
* @param clazz from which to get the field value
111+
* @param name the name of the field
112+
* @return the value of the field.
113+
* @throws PrivilegedActionException
114+
*/
86115
static <T> T getStaticFieldValue(final Class<?> clazz, final String name) throws PrivilegedActionException
87116
{
88117
final PrivilegedExceptionAction<T> action = new PrivilegedExceptionAction<T>()
@@ -98,6 +127,14 @@ public T run() throws Exception
98127
return AccessController.doPrivileged(action);
99128
}
100129

130+
/**
131+
* Extracts a field from a class using reflection.
132+
*
133+
* @param clazz from which to get the field object
134+
* @param name the name of the field object
135+
* @return the field object.
136+
* @throws PrivilegedActionException
137+
*/
101138
static Field getField(final Class<?> clazz, final String name) throws PrivilegedActionException
102139
{
103140
final PrivilegedExceptionAction<Field> action = new PrivilegedExceptionAction<Field>()

main/android/uk/co/real_logic/sbe/codec/java/DirectBuffer.java

Lines changed: 35 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,7 @@ public int getBytes(final int index, final byte[] dst, final int offset, final i
419419
{
420420
int count = Math.min(length, capacity - index);
421421
count = Math.min(count, dst.length - offset);
422+
422423
if (hasArray)
423424
{
424425
System.arraycopy(byteArray, this.offset + index, dst, offset, count);
@@ -428,6 +429,7 @@ public int getBytes(final int index, final byte[] dst, final int offset, final i
428429
final long address = effectiveDirectAddress + index;
429430
MEMORY_ACCESS.peekByteArray(address, dst, offset, count);
430431
}
432+
431433
return count;
432434
}
433435

@@ -442,44 +444,34 @@ public int getBytes(final int index, final byte[] dst, final int offset, final i
442444
*/
443445
public int getBytes(final int index, final DirectBuffer dst, final int offset, final int length)
444446
{
447+
int count = Math.min(length, capacity - index);
448+
count = Math.min(count, dst.capacity - offset);
449+
445450
if (hasArray)
446451
{
447-
int srcOffset = this.offset + index;
448-
int count = Math.min(length, dst.capacity - offset);
449-
count = Math.min(count, capacity - index);
450-
451452
if (dst.hasArray)
452453
{
453-
System.arraycopy(byteArray, srcOffset, dst.byteArray, dst.offset + offset, count);
454+
System.arraycopy(byteArray, this.offset + index, dst.byteArray, dst.offset + offset, count);
454455
}
455456
else
456457
{
457458
final long address = dst.effectiveDirectAddress + offset;
458-
DirectBuffer.MEMORY_ACCESS.pokeByteArray(address, byteArray, srcOffset, count);
459+
DirectBuffer.MEMORY_ACCESS.pokeByteArray(address, byteArray, this.offset + index, count);
459460
}
460-
461-
return count;
462461
}
463-
else if (dst.hasArray)
464-
{
465-
byte[] dstArray = dst.byteArray;
466-
int dstOffset = dst.offset + offset;
467-
468-
int count = Math.min(length, capacity - index);
469-
count = Math.min(count, dst.capacity - offset);
470-
final long address = effectiveDirectAddress + index;
471-
MEMORY_ACCESS.peekByteArray(address, dstArray, dstOffset, count);
472-
473-
return count;
462+
else {
463+
if (dst.hasArray)
464+
{
465+
final long address = effectiveDirectAddress + index;
466+
MEMORY_ACCESS.peekByteArray(address, dst.byteArray, dst.offset + offset, count);
467+
}
468+
else
469+
{
470+
Memory.memmove(dst.byteBuffer, dst.offset + offset, byteBuffer, this.offset + index, count);
471+
}
474472
}
475-
else
476-
{
477-
int count = Math.min(length, capacity - index);
478-
count = Math.min(count, dst.capacity - offset);
479-
Memory.memmove(dst.byteBuffer, dst.offset + offset, byteBuffer, this.offset + index, count);
480473

481-
return count;
482-
}
474+
return count;
483475
}
484476

485477
/**
@@ -492,8 +484,9 @@ else if (dst.hasArray)
492484
*/
493485
public int getBytes(final int index, final ByteBuffer dstBuffer, final int length)
494486
{
495-
int count = Math.min(dstBuffer.remaining(), capacity - index);
496-
count = Math.min(count, length);
487+
int count = Math.min(length, capacity - index);
488+
count = Math.min(count, dstBuffer.remaining());
489+
497490
if (hasArray)
498491
{
499492
getBytesFromByteArray(index, dstBuffer, count);
@@ -502,7 +495,9 @@ public int getBytes(final int index, final ByteBuffer dstBuffer, final int lengt
502495
{
503496
getBytesFromMemory(index, dstBuffer, count);
504497
}
498+
505499
BitUtil.setBufferPosition(dstBuffer, dstBuffer.position() + count);
500+
506501
return count;
507502
}
508503

@@ -531,6 +526,7 @@ public int putBytes(final int index, final byte[] src, final int offset, final i
531526
{
532527
int count = Math.min(length, capacity - index);
533528
count = Math.min(count, src.length - offset);
529+
534530
if (hasArray)
535531
{
536532
System.arraycopy(src, offset, byteArray, this.offset + index, count);
@@ -569,8 +565,8 @@ public int putBytes(final int index, final DirectBuffer src, final int offset, f
569565
*/
570566
public int putBytes(final int index, final ByteBuffer srcBuffer, final int length)
571567
{
572-
int count = Math.min(srcBuffer.remaining(), capacity - index);
573-
count = Math.min(count, length);
568+
int count = Math.min(length, capacity - index);
569+
count = Math.min(count, srcBuffer.remaining());
574570

575571
if (hasArray)
576572
{
@@ -580,71 +576,68 @@ public int putBytes(final int index, final ByteBuffer srcBuffer, final int lengt
580576
{
581577
putBytesToMemory(index, srcBuffer, count);
582578
}
579+
583580
BitUtil.setBufferPosition(srcBuffer, srcBuffer.position() + count);
584581

585582
return count;
586583
}
587584

588585
private void getBytesFromMemory(final int index, final ByteBuffer dstBuffer, final int count)
589586
{
590-
final int dstBufferPosition = dstBuffer.position();
591587
if (dstBuffer.hasArray())
592588
{
593589
final byte[] dst = dstBuffer.array();
594-
final int dstOffset = dstBuffer.arrayOffset() + dstBufferPosition;
590+
final int dstOffset = dstBuffer.arrayOffset() + dstBuffer.position();
595591
final long address = effectiveDirectAddress + index;
596592
MEMORY_ACCESS.peekByteArray(address, dst, dstOffset, count);
597593
}
598594
else
599595
{
600-
Memory.memmove(dstBuffer, dstBufferPosition, byteBuffer, index, count);
596+
Memory.memmove(dstBuffer, dstBuffer.position(), byteBuffer, index, count);
601597
}
602598
}
603599

604600
private void putBytesToMemory(final int index, final ByteBuffer srcBuffer, final int count)
605601
{
606-
final int srcBufferPosition = srcBuffer.position();
607602
if (srcBuffer.hasArray())
608603
{
609604
final byte[] src = srcBuffer.array();
610-
final int srcOffset = srcBuffer.arrayOffset() + srcBufferPosition;
605+
final int srcOffset = srcBuffer.arrayOffset() + srcBuffer.position();
611606
final long address = effectiveDirectAddress + index;
612607
MEMORY_ACCESS.pokeByteArray(address, src, srcOffset, count);
613608
}
614609
else
615610
{
616-
Memory.memmove(byteBuffer, index, srcBuffer, srcBufferPosition, count);
611+
Memory.memmove(byteBuffer, index, srcBuffer, srcBuffer.position(), count);
617612
}
618613
}
619614

620615
private void getBytesFromByteArray(final int index, final ByteBuffer dstBuffer, final int count)
621616
{
622-
final int dstBufferPosition = dstBuffer.position();
623617
if (dstBuffer.hasArray())
624618
{
625619
final byte[] dst = dstBuffer.array();
626-
final int dstOffset = dstBuffer.arrayOffset() + dstBufferPosition;
620+
final int dstOffset = dstBuffer.arrayOffset() + dstBuffer.position();
627621
System.arraycopy(byteArray, this.offset + index, dst, dstOffset, count);
628622
}
629623
else
630624
{
631-
final long address = BitUtil.getEffectiveDirectAddress(dstBuffer) + dstBufferPosition;
625+
final long address = BitUtil.getEffectiveDirectAddress(dstBuffer) + dstBuffer.position();
632626
MEMORY_ACCESS.pokeByteArray(address, byteArray, offset + index, count);
633627
}
634628
}
635629

636630
private void putBytesToByteArray(final int index, final ByteBuffer srcBuffer, final int count)
637631
{
638-
final int srcBufferPosition = srcBuffer.position();
639632
if (srcBuffer.hasArray())
640633
{
641634
final byte[] src = srcBuffer.array();
642-
final int srcOffset = srcBuffer.arrayOffset() + srcBufferPosition;
635+
final int srcOffset = srcBuffer.arrayOffset() + srcBuffer.position();
643636
System.arraycopy(src, srcOffset, byteArray, this.offset + index, count);
644637
}
645638
else
646639
{
647-
final long address = BitUtil.getEffectiveDirectAddress(srcBuffer) + srcBufferPosition;
640+
final long address = BitUtil.getEffectiveDirectAddress(srcBuffer) + srcBuffer.position();
648641
MEMORY_ACCESS.peekByteArray(address, byteArray, offset + index, count);
649642
}
650643
}

main/android/uk/co/real_logic/sbe/codec/java/MemoryAccess.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
1+
/*
2+
* Copyright 2013 Real Logic Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package uk.co.real_logic.sbe.codec.java;
217

18+
/**
19+
* This class is used to access memory addresses.
20+
* The 2 implementations that exists are proxies for the libcore.io.Memory class
21+
* which was changed in Android 4.3 (API 18) to use longs as addresses instead of integers.
22+
* The libcore.io.Memory class is available starting with Android 4.0 (API 14).
23+
*/
324
interface MemoryAccess {
425

526
byte peekByte(long address);

main/android/uk/co/real_logic/sbe/codec/java/MemoryAccessIntAddress.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
* Copyright 2013 Real Logic Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package uk.co.real_logic.sbe.codec.java;
217

318
import libcore.io.Memory;

main/android/uk/co/real_logic/sbe/codec/java/MemoryAccessLongAddress.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
* Copyright 2013 Real Logic Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package uk.co.real_logic.sbe.codec.java;
217

318
import libcore.io.Memory;

test/android/uk/co/real_logic/sbe/codec/java/DirectBufferTest.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import org.junit.rules.ExpectedException;
2424
import org.junit.runner.RunWith;
2525

26+
import android.util.Log;
27+
2628
import java.io.File;
2729
import java.io.IOException;
2830
import java.io.RandomAccessFile;
@@ -43,10 +45,10 @@ public class DirectBufferTest
4345
private static final int INDEX = 8;
4446

4547
private static final byte BYTE_VALUE = 1;
46-
private static final short SHORT_VALUE = 2;
47-
private static final int INT_VALUE = 4;
48+
private static final short SHORT_VALUE = 22345;
49+
private static final int INT_VALUE = 41244325;
4850
private static final float FLOAT_VALUE = 5.0f;
49-
private static final long LONG_VALUE = 6;
51+
private static final long LONG_VALUE = 1234567890123L;
5052
private static final double DOUBLE_VALUE = 7.0d;
5153

5254
@Rule
@@ -533,6 +535,7 @@ private static ByteBuffer createMemoryMappedBuffer()
533535
}
534536
catch (IOException e)
535537
{
538+
Log.e(DirectBufferTest.class.getName(), "Exception encountered", e);
536539
e.printStackTrace();
537540
}
538541
return null;
@@ -554,6 +557,7 @@ public static void cleanup()
554557
}
555558
catch (Exception e)
556559
{
560+
Log.e(DirectBufferTest.class.getName(), "Exception encountered", e);
557561
e.printStackTrace();
558562
}
559563
}

0 commit comments

Comments
 (0)