public AbstractFile getLastFile(final long logIndex, final int waitToWroteSize, final boolean createIfNecessary) {
        AbstractFile lastFile = null;
        while (true) {
            int fileCount = 0;
            this.readLock.lock();
            try {
                if (!this.files.isEmpty()) {
                    fileCount = this.files.size();
                    final AbstractFile file = this.files.get(fileCount - 1);
                    if (waitToWroteSize <= 0 || !file.reachesFileEndBy(waitToWroteSize)) {
                        lastFile = file;
                    } else if (file.reachesFileEndBy(waitToWroteSize)) {
                        // Reach file end , need to fill blank bytes in file end
                        file.fillEmptyBytesInFileEnd();
                    }
                }
            } finally {
                this.readLock.unlock();
            }
            // Try to get a new file
            if (lastFile == null && createIfNecessary) {
                this.writeLock.lock();
                try {
                    if (this.files.size() != fileCount) {
                        // That means already create a new file , just continue and try again
                        continue;
                    }
                    lastFile = this.allocateService.takeEmptyFile();
                    if (lastFile != null) {
                        final long newFileOffset = (long) this.files.size() * (long) this.fileSize;
                        lastFile.setFileFromOffset(newFileOffset);
                        this.files.add(lastFile);
                        this.swapOutFilesIfNecessary();
                        return lastFile;
                    } else {
                        continue;
                    }
                } catch (final Exception e) {
                    LOG.error("Error on create new abstract file , current logIndex:{}", logIndex);
                } finally {
                    this.writeLock.unlock();
                }
            }
            return lastFile;
        }
    }
@Test
    public void writeDataToFirstFile() {
        // Append 10 index to first file , and come to the file end (size:130)
        {
            for (int i = 0; i < 10; i++) {
                final AbstractFile lastFile = this.fileManager.getLastFile(i, 10, true);
                assert (lastFile instanceof IndexFile);
                final IndexFile indexFile = (IndexFile) lastFile;
                indexFile.appendIndex(i, i, segmentIndex);
            }
        }
    }