@Test public void testIsReusable() throws Exception { NativeMemoryChunk chunk = mPool.get(1); Assert.assertTrue(mPool.isReusable(chunk)); chunk.close(); Assert.assertFalse(mPool.isReusable(chunk)); }
@Test public void testFree() throws Exception { NativeMemoryChunk c = mPool.alloc(1); Assert.assertFalse(c.isClosed()); mPool.free(c); Assert.assertTrue(c.isClosed()); mPool.free(c); Assert.assertTrue(c.isClosed()); }
// Test out the alloc method @Test public void testAlloc() throws Exception { NativeMemoryChunk c = mPool.alloc(1); Assert.assertNotNull(c); Assert.assertEquals(1, c.getSize()); Assert.assertEquals(1, mPool.alloc(1).getSize()); Assert.assertEquals(33, mPool.alloc(33).getSize()); Assert.assertEquals(32, mPool.alloc(32).getSize()); }
/** * Creates a new inputstream instance over the specific memory chunk. * * @param nativeMemoryChunk the native memory chunk * @param startOffset start offset within the memory chunk * @param length length of subchunk */ public NativeMemoryChunkInputStream( NativeMemoryChunk nativeMemoryChunk, int startOffset, int length) { super(); Preconditions.checkState(startOffset >= 0); Preconditions.checkState(length >= 0); mMemoryChunk = Preconditions.checkNotNull(nativeMemoryChunk); mStartOffset = startOffset; mEndOffset = startOffset + length > nativeMemoryChunk.getSize() ? nativeMemoryChunk.getSize() : startOffset + length; mOffset = startOffset; mMark = startOffset; }
/** * Reads at most {@code length} bytes from this stream and stores them in byte array {@code * buffer} starting at {@code offset}. * * @param buffer the buffer to read data into * @param offset start offset in the buffer * @param length max number of bytes to read * @return number of bytes read */ @Override public int read(byte[] buffer, int offset, int length) { if (offset < 0 || length < 0 || offset + length > buffer.length) { throw new ArrayIndexOutOfBoundsException( "length=" + buffer.length + "; regionStart=" + offset + "; regionLength=" + length); } if (mOffset >= mEndOffset) { return -1; } if (length == 0) { return 0; } int numToRead = Math.min(available(), length); int numRead = mMemoryChunk.read(mOffset, buffer, offset, numToRead); mOffset += numRead; return numRead; }