/** @throws Exception */
  protected void openFile() throws Exception {
    flush();

    ByteBuffer bufferWrite = fileFactory.newBuffer(journal.getFileSize());

    writingChannel = HornetQBuffers.wrappedBuffer(bufferWrite);

    currentFile = filesRepository.takeFile(false, false, false, true);

    sequentialFile = currentFile.getFile();

    sequentialFile.open(1, false);

    currentFile = new JournalFileImpl(sequentialFile, nextOrderingID++, JournalImpl.FORMAT_VERSION);

    JournalImpl.writeHeader(writingChannel, journal.getUserVersion(), currentFile.getFileID());
  }
  /** Write the record to the current file. */
  private void writeRecord(
      JournalInternalRecord encoder, final boolean sync, final IOCompletion callback)
      throws Exception {

    lockAppend.lock();
    try {
      if (callback != null) {
        callback.storeLineUp();
      }
      currentFile = journal.switchFileIfNecessary(encoder.getEncodeSize());
      encoder.setFileID(currentFile.getRecordID());

      if (callback != null) {
        currentFile.getFile().write(encoder, sync, callback);
      } else {
        currentFile.getFile().write(encoder, sync);
      }
    } finally {
      lockAppend.unlock();
    }
  }
  public static SequentialFile writeControlFile(
      final SequentialFileFactory fileFactory,
      final List<JournalFile> files,
      final List<JournalFile> newFiles,
      final List<Pair<String, String>> renames)
      throws Exception {

    SequentialFile controlFile =
        fileFactory.createSequentialFile(AbstractJournalUpdateTask.FILE_COMPACT_CONTROL, 1);

    try {
      controlFile.open(1, false);

      JournalImpl.initFileHeader(fileFactory, controlFile, 0, 0);

      HornetQBuffer filesToRename = HornetQBuffers.dynamicBuffer(1);

      // DataFiles first

      if (files == null) {
        filesToRename.writeInt(0);
      } else {
        filesToRename.writeInt(files.size());

        for (JournalFile file : files) {
          filesToRename.writeUTF(file.getFile().getFileName());
        }
      }

      // New Files second

      if (newFiles == null) {
        filesToRename.writeInt(0);
      } else {
        filesToRename.writeInt(newFiles.size());

        for (JournalFile file : newFiles) {
          filesToRename.writeUTF(file.getFile().getFileName());
        }
      }

      // Renames from clean up third
      if (renames == null) {
        filesToRename.writeInt(0);
      } else {
        filesToRename.writeInt(renames.size());
        for (Pair<String, String> rename : renames) {
          filesToRename.writeUTF(rename.getA());
          filesToRename.writeUTF(rename.getB());
        }
      }

      JournalInternalRecord controlRecord =
          new JournalAddRecord(
              true, 1, (byte) 0, new ByteArrayEncoding(filesToRename.toByteBuffer().array()));

      HornetQBuffer renameBuffer = HornetQBuffers.dynamicBuffer(filesToRename.writerIndex());

      controlRecord.setFileID(0);

      controlRecord.encode(renameBuffer);

      ByteBuffer writeBuffer = fileFactory.newBuffer(renameBuffer.writerIndex());

      writeBuffer.put(renameBuffer.toByteBuffer().array(), 0, renameBuffer.writerIndex());

      writeBuffer.rewind();

      controlFile.writeDirect(writeBuffer, true);

      return controlFile;
    } finally {
      controlFile.close();
    }
  }