org.agrona.IoUtil类的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(8.6k)|赞(0)|评价(0)|浏览(151)

本文整理了Java中org.agrona.IoUtil类的一些代码示例,展示了IoUtil类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。IoUtil类的具体详情如下:
包路径:org.agrona.IoUtil
类名称:IoUtil

IoUtil介绍

[英]Collection of IO utilities.
[中]IO实用程序的集合。

代码示例

代码示例来源:origin: real-logic/aeron

/**
 * Delete the cluster container directory.
 */
public void deleteDirectory()
{
  if (null != clusterDir)
  {
    IoUtil.delete(clusterDir, false);
  }
}

代码示例来源:origin: real-logic/aeron

private void closeRecordingSegment()
{
  IoUtil.unmap(mappedSegmentBuffer);
  mappedSegmentBuffer = null;
}

代码示例来源:origin: real-logic/agrona

public static MappedByteBuffer mapExistingFile(
  final File markFile, final Consumer<String> logger, final long offset, final long length)
{
  if (markFile.exists())
  {
    if (null != logger)
    {
      logger.accept("INFO: Mark file exists: " + markFile);
    }
    return IoUtil.mapExistingFile(markFile, markFile.toString(), offset, length);
  }
  return null;
}

代码示例来源:origin: real-logic/aeron

/**
 * Is a media driver active in the given directory?
 *
 * @param directory       to check
 * @param driverTimeoutMs for the driver liveness check.
 * @param logger          for feedback as liveness checked.
 * @return true if a driver is active or false if not.
 */
public static boolean isDriverActive(
  final File directory, final long driverTimeoutMs, final Consumer<String> logger)
{
  final File cncFile = new File(directory, CncFileDescriptor.CNC_FILE);
  if (cncFile.exists() && cncFile.length() > 0)
  {
    logger.accept("INFO: Aeron CnC file exists: " + cncFile);
    final MappedByteBuffer cncByteBuffer = IoUtil.mapExistingFile(cncFile, "CnC file");
    try
    {
      return isDriverActive(driverTimeoutMs, logger, cncByteBuffer);
    }
    finally
    {
      IoUtil.unmap(cncByteBuffer);
    }
  }
  return false;
}

代码示例来源:origin: real-logic/agrona

/**
 * Create an empty file, and optionally fill with 0s, and return the {@link FileChannel}
 *
 * @param file          to create
 * @param length        of the file to create
 * @param fillWithZeros to the length of the file to force allocation.
 * @return {@link java.nio.channels.FileChannel} for the file
 */
public static FileChannel createEmptyFile(final File file, final long length, final boolean fillWithZeros)
{
  ensureDirectoryExists(file.getParentFile(), file.getParent());
  FileChannel templateFile = null;
  try
  {
    final RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
    randomAccessFile.setLength(length);
    templateFile = randomAccessFile.getChannel();
    if (fillWithZeros)
    {
      fill(templateFile, 0, length, (byte)0);
    }
  }
  catch (final IOException ex)
  {
    LangUtil.rethrowUnchecked(ex);
  }
  return templateFile;
}

代码示例来源:origin: real-logic/aeron

public static File makeTestDirectory()
{
  final File archiveDir = new File(IoUtil.tmpDirName(), "archive-test");
  if (archiveDir.exists())
  {
    System.err.println("Warning archive directory exists, deleting: " + archiveDir.getAbsolutePath());
    IoUtil.delete(archiveDir, false);
  }
  if (!archiveDir.mkdirs())
  {
    throw new IllegalStateException("failed to make archive test directory: " + archiveDir.getAbsolutePath());
  }
  return archiveDir;
}

代码示例来源:origin: real-logic/agrona

IoUtil.unmap(byteBuffer);
  IoUtil.delete(directory, false);
IoUtil.ensureDirectoryExists(directory, directory.toString());

代码示例来源:origin: real-logic/aeron

private static void ensureDirectoryIsRecreated(final Context ctx)
{
  if (ctx.aeronDirectory().isDirectory())
  {
    if (ctx.warnIfDirectoryExists())
    {
      System.err.println("WARNING: " + ctx.aeronDirectory() + " already exists.");
    }
    if (!ctx.dirDeleteOnStart())
    {
      final Consumer<String> logger = ctx.warnIfDirectoryExists() ? System.err::println : (s) -> {};
      final MappedByteBuffer cncByteBuffer = ctx.mapExistingCncFile(logger);
      try
      {
        if (CommonContext.isDriverActive(ctx.driverTimeoutMs(), logger, cncByteBuffer))
        {
          throw new ActiveDriverException("active driver detected");
        }
        reportExistingErrors(ctx, cncByteBuffer);
      }
      finally
      {
        IoUtil.unmap(cncByteBuffer);
      }
    }
    ctx.deleteAeronDirectory();
  }
  IoUtil.ensureDirectoryExists(ctx.aeronDirectory(), "aeron");
}

代码示例来源:origin: real-logic/aeron

@After
public void after()
{
  IoUtil.unmap(mockLogBufferMapped.byteBuffer());
  CloseHelper.close(mockLogBufferChannel);
  IoUtil.delete(archiveDir, false);
  IoUtil.delete(termFile, false);
}

代码示例来源:origin: real-logic/agrona

/**
 * Create a new file, fill with 0s, and return a {@link java.nio.MappedByteBuffer} for the file.
 * <p>
 * The file itself will be closed, but the mapping will persist.
 *
 * @param location of the file to create and map
 * @param length   of the file to create and map
 * @return {@link java.nio.MappedByteBuffer} for the file
 */
public static MappedByteBuffer mapNewFile(final File location, final long length)
{
  return mapNewFile(location, length, true);
}

代码示例来源:origin: real-logic/artio

@Test(timeout = 20_000L)
public void shouldReadRecordsFromBeforeARestart() throws IOException
{
  indexExampleMessage();
  // Fake restarting the gateway
  final File logFile = logFile(SESSION_ID);
  IoUtil.ensureDirectoryExists(new File(DEFAULT_LOG_FILE_DIR), DEFAULT_LOG_FILE_DIR);
  logFile.createNewFile();
  try
  {
    newReplayIndex();
    final int msgCount = query();
    verifyMappedFile(SESSION_ID, 1);
    verifyMessagesRead(1);
    assertEquals(1, msgCount);
  }
  finally
  {
    IoUtil.delete(new File(DEFAULT_LOG_FILE_DIR), false);
  }
}

代码示例来源:origin: real-logic/aeron

public RawLogFactory(
  final String dataDirectoryName,
  final int filePageSize,
  final boolean checkStorage,
  final ErrorHandler errorHandler)
{
  this.filePageSize = filePageSize;
  this.checkStorage = checkStorage;
  this.errorHandler = errorHandler;
  final File dataDir = new File(dataDirectoryName);
  publicationsDir = new File(dataDir, PUBLICATIONS);
  imagesDir = new File(dataDir, IMAGES);
  IoUtil.ensureDirectoryExists(publicationsDir, PUBLICATIONS);
  IoUtil.ensureDirectoryExists(imagesDir, IMAGES);
  FileStore fs = null;
  try
  {
    if (checkStorage)
    {
      fs = Files.getFileStore(dataDir.toPath());
    }
  }
  catch (final IOException ex)
  {
    LangUtil.rethrowUnchecked(ex);
  }
  fileStore = fs;
}

代码示例来源:origin: real-logic/aeron

storageDir = new File(IoUtil.tmpDirName());

代码示例来源:origin: real-logic/artio

public static MappedByteBuffer mapNewFile(final File file, final int size)
{
  final File parentDir = file.getParentFile();
  IoUtil.ensureDirectoryExists(parentDir, parentDir.getAbsolutePath());
  return IoUtil.mapNewFile(file, size);
}

代码示例来源:origin: real-logic/agrona

/**
 * Check that file exists, open file, and return MappedByteBuffer for entire file
 * <p>
 * The file itself will be closed, but the mapping will persist.
 *
 * @param location         of the file to map
 * @param descriptionLabel to be associated for any exceptions
 * @return {@link java.nio.MappedByteBuffer} for the file
 */
public static MappedByteBuffer mapExistingFile(final File location, final String descriptionLabel)
{
  checkFileExists(location, descriptionLabel);
  MappedByteBuffer mappedByteBuffer = null;
  try (RandomAccessFile file = new RandomAccessFile(location, "rw");
    FileChannel channel = file.getChannel())
  {
    mappedByteBuffer = channel.map(READ_WRITE, 0, channel.size());
  }
  catch (final IOException ex)
  {
    LangUtil.rethrowUnchecked(ex);
  }
  return mappedByteBuffer;
}

代码示例来源:origin: org.agrona/agrona

IoUtil.unmap(byteBuffer);
  IoUtil.delete(directory, false);
IoUtil.ensureDirectoryExists(directory, directory.toString());

代码示例来源:origin: io.aeron/aeron-all

private static void ensureDirectoryIsRecreated(final Context ctx)
{
  if (ctx.aeronDirectory().isDirectory())
  {
    if (ctx.warnIfDirectoryExists())
    {
      System.err.println("WARNING: " + ctx.aeronDirectory() + " already exists.");
    }
    if (!ctx.dirDeleteOnStart())
    {
      final Consumer<String> logger = ctx.warnIfDirectoryExists() ? System.err::println : (s) -> {};
      final MappedByteBuffer cncByteBuffer = ctx.mapExistingCncFile(logger);
      try
      {
        if (CommonContext.isDriverActive(ctx.driverTimeoutMs(), logger, cncByteBuffer))
        {
          throw new ActiveDriverException("active driver detected");
        }
        reportExistingErrors(ctx, cncByteBuffer);
      }
      finally
      {
        IoUtil.unmap(cncByteBuffer);
      }
    }
    ctx.deleteAeronDirectory();
  }
  IoUtil.ensureDirectoryExists(ctx.aeronDirectory(), "aeron");
}

代码示例来源:origin: real-logic/aeron

/**
   * Map a new loss report in the Aeron directory for a given length.
   *
   * @param aeronDirectoryName in which to create the file.
   * @param reportFileLength   for the file.
   * @return the newly mapped buffer for the file.
   */
  public static MappedByteBuffer mapLossReport(final String aeronDirectoryName, final int reportFileLength)
  {
    return mapNewFile(file(aeronDirectoryName), reportFileLength, false);
  }
}

代码示例来源:origin: org.agrona/Agrona

/**
 * Create an empty file, fill with 0s, and return the {@link FileChannel}
 *
 * @param file   to create
 * @param length of the file to create
 * @return {@link java.nio.channels.FileChannel} for the file
 */
public static FileChannel createEmptyFile(final File file, final long length)
{
  ensureDirectoryExists(file.getParentFile(), file.getParent());
  FileChannel templateFile = null;
  try
  {
    final RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
    randomAccessFile.setLength(length);
    templateFile = randomAccessFile.getChannel();
    fill(templateFile, 0, length, (byte)0);
  }
  catch (final IOException ex)
  {
    LangUtil.rethrowUnchecked(ex);
  }
  return templateFile;
}

代码示例来源:origin: real-logic/aeron

@Before
public void createDataDir()
{
  IoUtil.ensureDirectoryExists(DATA_DIR, "data");
  rawLogFactory = new RawLogFactory(
    DATA_DIR.getAbsolutePath(), PAGE_SIZE, PERFORM_STORAGE_CHECKS, mock(ErrorHandler.class));
}

相关文章