AODataSyncFile { public int read(ByteBuffer readBuffer, long position) throws IOException { if (position < FileHeader.HEADER_SIZE) { throw new IllegalArgumentException("position must >= file header size:" + FileHeader.HEADER_SIZE + " current pos:" + position); } readBufferLock.lock(); try { long allFileSize = size(); if (allFileSize == 0) { return -1; } if (position >= allFileSize) { throw new IllegalArgumentException("position must < file size[" + allFileSize + "]!"); } int readSize = readBuffer.remaining(); int count = 0; long[] blockIndexes = getBlockIndexes(position, readSize); for (int i = 0; i < blockIndexes.length; ++i) { ByteDynamicArray blockDataDArray = getBlockData(blockIndexes[i]); int readPos = 0; if (i == 0) { long removedHeaderPos = position - FileHeader.HEADER_SIZE; readPos = (int) (removedHeaderPos % blockSize); } count += blockDataDArray.read(readBuffer, readPos); } return count; } finally { readBufferLock.unlock(); } } AODataSyncFile(File file, int syncStrategy);  AODataSyncFile(File file, int blockSize, boolean blockCache, int maxCacheCapacity, int maxDataBufferSize, int syncStrategy); long size(); File getFile(); int getBlockSize(); boolean isBlockCache(); int getMaxCacheCapacity(); int getMaxDataBufferSize(); long appendData(ByteBuffer newHeaderBuffer, ByteBuffer newDataBuffer); int read(ByteBuffer readBuffer, long position); void close(); static final int DEFAULT_MAX_CACHE_CAPACITY; static final int DEFAULT_BLOCK_SIZE; static final int DEFAULT_MAX_DATA_BUFFER_SIZE; static final int BUFFER_STRATEGY; static final int ALWAYS_STRATEGY; static final int EVERY_SEC_STRATEGY; }
@Test public void testReadData() throws Exception { testWriteData(); int bufferSize = 1024; for (int i = 0; i < 6; ++i) { ByteBuffer readBuffer = ByteBuffer.allocate(bufferSize); aoDataSyncFile.read(readBuffer, FileHeader.HEADER_SIZE + i * bufferSize); readBuffer.flip(); Assert.assertEquals((byte) i, readBuffer.get()); } }