Skip to content

Commit 61c2a29

Browse files
committed
[Android] code cleanup and added some useful javadocs.
1 parent 79a43f2 commit 61c2a29

File tree

2 files changed

+38
-29
lines changed

2 files changed

+38
-29
lines changed

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ private BitUtil()
102102
{
103103
}
104104

105+
/**
106+
* Returns the memory address of a direct buffer.
107+
*
108+
* @param buffer the direct buffer
109+
* @return the memory address of the buffer
110+
*/
105111
static long getEffectiveDirectAddress(final ByteBuffer buffer)
106112
{
107113
try
@@ -119,6 +125,12 @@ static long getEffectiveDirectAddress(final ByteBuffer buffer)
119125
}
120126
}
121127

128+
/**
129+
* Returns the memory address of a {@link MemoryFile}
130+
*
131+
* @param memoryFile the {@link MemoryFile}
132+
* @return the memory address of the {@link MemoryFile}
133+
*/
122134
static long getMemoryFileAddress(final MemoryFile memoryFile)
123135
{
124136
try
@@ -154,7 +166,7 @@ static MemoryAccess getMemoryAccess()
154166
* @return the field object.
155167
* @throws PrivilegedActionException
156168
*/
157-
static Field getField(final Class<?> clazz, final String name) throws PrivilegedActionException
169+
private static Field getField(final Class<?> clazz, final String name) throws PrivilegedActionException
158170
{
159171
final PrivilegedExceptionAction<Field> action = new PrivilegedExceptionAction<Field>()
160172
{
@@ -170,11 +182,13 @@ public Field run() throws Exception
170182
}
171183

172184
/**
173-
* Builds a DirectByteBuffer out of an adddress and a size
185+
* Builds a DirectByteBuffer out of an address and a size. Uses the same
186+
* constructor as the buffers that are created from JNI, thus it does not
187+
* deallocate the memory when the {@link ByteBuffer} object is garbage collected.
174188
*
175-
* @param address
176-
* @param size
177-
* @return
189+
* @param address where the memory begins off-heap
190+
* @param size of the buffer from the given address
191+
* @return the {@link ByteBuffer} associated with the address and size
178192
*/
179193
static ByteBuffer newDirectByteBuffer(long address, int size)
180194
{

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

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,14 @@ public final class DirectBuffer
3232
private static final MemoryAccess MEMORY_ACCESS = BitUtil.getMemoryAccess();
3333

3434
private byte[] byteArray;
35-
private boolean hasArray;
35+
private long effectiveDirectAddress;
36+
private int offset;
37+
private int capacity;
3638

3739
private ByteBuffer byteBuffer;
3840
//we keep this reference to avoid being cleaned by GC
3941
@SuppressWarnings("unused")
4042
private MemoryFile memoryFile;
41-
private long effectiveDirectAddress;
42-
private int offset;
43-
private int capacity;
4443

4544
/**
4645
* Attach a view to a byte[] for providing direct access.
@@ -97,7 +96,6 @@ public void wrap(final byte[] buffer)
9796
effectiveDirectAddress = 0;
9897
capacity = buffer.length;
9998
byteArray = buffer;
100-
hasArray = true;
10199
byteBuffer = null;
102100
memoryFile = null;
103101
}
@@ -115,7 +113,6 @@ public void wrap(final ByteBuffer buffer)
115113
if (buffer.hasArray())
116114
{
117115
byteArray = buffer.array();
118-
hasArray = true;
119116
offset = buffer.arrayOffset();
120117
effectiveDirectAddress = 0;
121118
}
@@ -125,7 +122,6 @@ public void wrap(final ByteBuffer buffer)
125122
// and buffers allocated inside JNI.
126123
// Performance seems to be lower for this situation
127124
byteArray = null;
128-
hasArray = false;
129125
offset = 0;
130126
effectiveDirectAddress = BitUtil.getEffectiveDirectAddress(buffer);
131127
}
@@ -146,11 +142,10 @@ public void wrap(final long address, final int capacity)
146142
effectiveDirectAddress = address;
147143
this.capacity = capacity;
148144
byteArray = null;
149-
hasArray = false;
150-
memoryFile = null;
151145
//Memory.memmove needs either a bytebuffer or a bytearray
152146
//it could only work with memory addresses, but it doesn't
153147
byteBuffer = BitUtil.newDirectByteBuffer(effectiveDirectAddress, this.capacity);
148+
memoryFile = null;
154149
}
155150

156151
/**
@@ -250,7 +245,7 @@ public ByteBuffer duplicateByteBuffer()
250245
*/
251246
public long getLong(final int index, final ByteOrder byteOrder)
252247
{
253-
if (hasArray)
248+
if (byteArray != null)
254249
{
255250
return Memory.peekLong(byteArray, offset + index, byteOrder);
256251
}
@@ -271,7 +266,7 @@ public long getLong(final int index, final ByteOrder byteOrder)
271266
*/
272267
public void putLong(final int index, final long value, final ByteOrder byteOrder)
273268
{
274-
if (hasArray)
269+
if (byteArray != null)
275270
{
276271
Memory.pokeLong(byteArray, offset + index, value, byteOrder);
277272
}
@@ -292,7 +287,7 @@ public void putLong(final int index, final long value, final ByteOrder byteOrder
292287
*/
293288
public int getInt(final int index, final ByteOrder byteOrder)
294289
{
295-
if (hasArray)
290+
if (byteArray != null)
296291
{
297292
return Memory.peekInt(byteArray, offset + index, byteOrder);
298293
}
@@ -313,7 +308,7 @@ public int getInt(final int index, final ByteOrder byteOrder)
313308
*/
314309
public void putInt(final int index, final int value, final ByteOrder byteOrder)
315310
{
316-
if (hasArray)
311+
if (byteArray != null)
317312
{
318313
Memory.pokeInt(byteArray, offset + index, value, byteOrder);
319314
}
@@ -382,7 +377,7 @@ public void putFloat(final int index, final float value, final ByteOrder byteOrd
382377
*/
383378
public short getShort(final int index, final ByteOrder byteOrder)
384379
{
385-
if (hasArray)
380+
if (byteArray != null)
386381
{
387382
return Memory.peekShort(byteArray, offset + index, byteOrder);
388383
}
@@ -403,7 +398,7 @@ public short getShort(final int index, final ByteOrder byteOrder)
403398
*/
404399
public void putShort(final int index, final short value, final ByteOrder byteOrder)
405400
{
406-
if (hasArray)
401+
if (byteArray != null)
407402
{
408403
Memory.pokeShort(byteArray, offset + index, value, byteOrder);
409404
}
@@ -423,7 +418,7 @@ public void putShort(final int index, final short value, final ByteOrder byteOrd
423418
*/
424419
public byte getByte(final int index)
425420
{
426-
if (hasArray)
421+
if (byteArray != null)
427422
{
428423
return byteArray[offset + index];
429424
}
@@ -442,7 +437,7 @@ public byte getByte(final int index)
442437
*/
443438
public void putByte(final int index, final byte value)
444439
{
445-
if (hasArray)
440+
if (byteArray != null)
446441
{
447442
byteArray[offset + index] = value;
448443
}
@@ -480,7 +475,7 @@ public int getBytes(final int index, final byte[] dst, final int offset, final i
480475
int count = Math.min(length, capacity - index);
481476
count = Math.min(count, dst.length - offset);
482477

483-
if (hasArray)
478+
if (byteArray != null)
484479
{
485480
System.arraycopy(byteArray, this.offset + index, dst, offset, count);
486481
}
@@ -507,9 +502,9 @@ public int getBytes(final int index, final DirectBuffer dst, final int offset, f
507502
int count = Math.min(length, capacity - index);
508503
count = Math.min(count, dst.capacity - offset);
509504

510-
if (hasArray)
505+
if (byteArray != null)
511506
{
512-
if (dst.hasArray)
507+
if (dst.byteArray != null)
513508
{
514509
System.arraycopy(byteArray, this.offset + index, dst.byteArray, dst.offset + offset, count);
515510
}
@@ -521,7 +516,7 @@ public int getBytes(final int index, final DirectBuffer dst, final int offset, f
521516
}
522517
else
523518
{
524-
if (dst.hasArray)
519+
if (dst.byteArray != null)
525520
{
526521
final long address = effectiveDirectAddress + index;
527522
MEMORY_ACCESS.peekByteArray(address, dst.byteArray, dst.offset + offset, count);
@@ -548,7 +543,7 @@ public int getBytes(final int index, final ByteBuffer dstBuffer, final int lengt
548543
int count = Math.min(length, capacity - index);
549544
count = Math.min(count, dstBuffer.remaining());
550545

551-
if (hasArray)
546+
if (byteArray != null)
552547
{
553548
getBytesFromByteArray(index, dstBuffer, count);
554549
}
@@ -588,7 +583,7 @@ public int putBytes(final int index, final byte[] src, final int offset, final i
588583
int count = Math.min(length, capacity - index);
589584
count = Math.min(count, src.length - offset);
590585

591-
if (hasArray)
586+
if (byteArray != null)
592587
{
593588
System.arraycopy(src, offset, byteArray, this.offset + index, count);
594589
}
@@ -629,7 +624,7 @@ public int putBytes(final int index, final ByteBuffer srcBuffer, final int lengt
629624
int count = Math.min(length, capacity - index);
630625
count = Math.min(count, srcBuffer.remaining());
631626

632-
if (hasArray)
627+
if (byteArray != null)
633628
{
634629
putBytesToByteArray(index, srcBuffer, count);
635630
}

0 commit comments

Comments
 (0)