FileChannelStorage implements IStorage { public FileChannelStorage(String dir, int index, int capacity) throws IOException { File dirFile = new File(dir); if (!dirFile.exists()) { dirFile.mkdirs(); } String fullFileName = dir + index + "-" + System.currentTimeMillis() + DATA_FILE_SUFFIX; raf = new RandomAccessFile(fullFileName, "rw"); raf.setLength(capacity); fileChannel = raf.getChannel(); } FileChannelStorage(String dir, int index, int capacity); @Override void get(int position, byte[] dest); @Override void put(int position, byte[] source); @Override void free(); @Override void close();  }
@Test public void testFileChannelStorage() throws IOException { fileChannelStorage = new FileChannelStorage(testDir, 1, 16 * 1024 * 1024); String testString = "Test String"; fileChannelStorage.put(0, testString.getBytes()); byte[] dest = new byte[testString.getBytes().length]; fileChannelStorage.get(0, dest); assertEquals(testString, new String(dest)); int limit = 1000; int[] positionArray = new int[limit]; int[] lengthArray = new int[limit]; int position = 0; for(int i = 0; i < limit; i++) { byte[] src = (testString + i).getBytes(); positionArray[i] = position; lengthArray[i] = src.length; fileChannelStorage.put(position, src); position += src.length; } for(int i = 0; i < limit; i++) { dest = new byte[lengthArray[i]]; fileChannelStorage.get(positionArray[i], dest); assertEquals(testString + i, new String(dest)); } }