Class FileCreator

java.lang.Object
org.mockserver.file.FileCreator

public class FileCreator extends Object
  • Constructor Details

    • FileCreator

      public FileCreator()
  • Method Details

    • writeToFileAtomically

      public static void writeToFileAtomically(String type, File target, String content, boolean ownerOnly) throws IOException
      Atomically write content to target, applying restrictive owner-only permissions (0600) when ownerOnly is true (private keys, key stores) or world-readable (0644) permissions otherwise (public certificates), on POSIX filesystems.

      The bytes are written to a sibling temp file (created up-front with the target permissions, so a private key is never momentarily world-readable) then moved into place with ATOMIC_MOVE where supported, so a concurrent reader never observes a truncated or empty file. On non-POSIX filesystems (e.g. Windows) the permission step is skipped and the platform default is used — the atomic write still applies.

      Parameters:
      type - human-readable description of the artefact, used in the failure message
      target - the destination file
      content - the file content (UTF-8 encoded)
      ownerOnly - true to restrict to the owning user (private key material), false for public data
      Throws:
      IOException - if the parent directory is missing/unwritable, or the write/move fails — the caller MUST fail fast rather than swallow this, otherwise a later read surfaces a misleading "cannot read PEM" error (defect C13)
    • writeToFileAtomically

      public static void writeToFileAtomically(String type, File target, byte[] content, boolean ownerOnly) throws IOException
      Binary variant of writeToFileAtomically(String, File, String, boolean) — see that method for the atomicity and permission guarantees. Used for key stores (JKS) that carry private key material and must be owner-only (0600).
      Throws:
      IOException
    • createFileIfNotExists

      public static File createFileIfNotExists(String type, File file)
    • createParentDirs

      public static void createParentDirs(File file) throws IOException
      Throws:
      IOException