RandomAccessFileMode.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.io;

  18. import java.io.File;
  19. import java.io.FileNotFoundException;
  20. import java.io.IOException;
  21. import java.io.RandomAccessFile;
  22. import java.nio.file.OpenOption;
  23. import java.nio.file.Path;
  24. import java.nio.file.StandardOpenOption;
  25. import java.util.Objects;

  26. import org.apache.commons.io.function.IOConsumer;
  27. import org.apache.commons.io.function.IOFunction;

  28. /**
  29.  * Enumerates access modes for {@link RandomAccessFile} with factory methods.
  30.  *
  31.  * @see RandomAccessFile#RandomAccessFile(File, String)
  32.  * @see RandomAccessFile#RandomAccessFile(String, String)
  33.  * @see Enum
  34.  * @since 2.12.0
  35.  */
  36. public enum RandomAccessFileMode {

  37.     /**
  38.      * Defines mode {@value #R} to open a {@link RandomAccessFile} for reading only.
  39.      *
  40.      * @see RandomAccessFile#RandomAccessFile(File, String)
  41.      * @see RandomAccessFile#RandomAccessFile(String, String)
  42.      */
  43.     READ_ONLY(RandomAccessFileMode.R, 1), // NOPMD bug https://github.com/pmd/pmd/issues/5263

  44.     /**
  45.      * Defines mode {@value #RW} to open a {@link RandomAccessFile} for reading and writing.
  46.      *
  47.      * @see RandomAccessFile#RandomAccessFile(File, String)
  48.      * @see RandomAccessFile#RandomAccessFile(String, String)
  49.      */
  50.     READ_WRITE(RandomAccessFileMode.RW, 2), // NOPMD bug https://github.com/pmd/pmd/issues/5263

  51.     /**
  52.      * Defines mode {@value #RWS} to open a {@link RandomAccessFile} for reading and writing, as with {@value #RW}, and also require that every update to the
  53.      * file's content or metadata be written synchronously to the underlying storage device.
  54.      *
  55.      * @see RandomAccessFile#RandomAccessFile(File, String)
  56.      * @see RandomAccessFile#RandomAccessFile(String, String)
  57.      * @see StandardOpenOption#SYNC
  58.      */
  59.     READ_WRITE_SYNC_ALL(RandomAccessFileMode.RWS, 4), // NOPMD bug https://github.com/pmd/pmd/issues/5263

  60.     /**
  61.      * Defines mode {@value #RWD} to open a {@link RandomAccessFile} for reading and writing, as with {@value #RW}, and also require that every update to the
  62.      * file's content be written synchronously to the underlying storage device.
  63.      *
  64.      * @see RandomAccessFile#RandomAccessFile(File, String)
  65.      * @see RandomAccessFile#RandomAccessFile(String, String)
  66.      * @see StandardOpenOption#DSYNC
  67.      */
  68.     READ_WRITE_SYNC_CONTENT(RandomAccessFileMode.RWD, 3); // NOPMD bug https://github.com/pmd/pmd/issues/5263

  69.     private static final String R = "r";
  70.     private static final String RW = "rw";
  71.     private static final String RWD = "rwd";
  72.     private static final String RWS = "rws";

  73.     /**
  74.      * Gets the enum value that best fits the given {@link OpenOption}s.
  75.      * <p>
  76.      * The input must be a legal and working combination for NIO.
  77.      * </p>
  78.      *
  79.      * @param openOption options like {@link StandardOpenOption}.
  80.      * @return best fit, by default {@link #READ_ONLY}.
  81.      * @see StandardOpenOption
  82.      * @since 2.18.0
  83.      */
  84.     public static RandomAccessFileMode valueOf(final OpenOption... openOption) {
  85.         RandomAccessFileMode bestFit = READ_ONLY;
  86.         for (final OpenOption option : openOption) {
  87.             if (option instanceof StandardOpenOption) {
  88.                 switch ((StandardOpenOption) option) {
  89.                 case WRITE:
  90.                     if (!bestFit.implies(READ_WRITE)) {
  91.                         bestFit = READ_WRITE;
  92.                     }
  93.                     break;
  94.                 case DSYNC:
  95.                     if (!bestFit.implies(READ_WRITE_SYNC_CONTENT)) {
  96.                         bestFit = READ_WRITE_SYNC_CONTENT;
  97.                     }
  98.                     break;
  99.                 case SYNC:
  100.                     if (!bestFit.implies(READ_WRITE_SYNC_ALL)) {
  101.                         bestFit = READ_WRITE_SYNC_ALL;
  102.                     }
  103.                     break;
  104.                 default:
  105.                     // explicit case skip (spotbugs)
  106.                     continue;
  107.                 }
  108.             }
  109.         }
  110.         return bestFit;
  111.     }

  112.     /**
  113.      * Gets the {@link RandomAccessFileMode} value for the given mode, one of {@value #R}, {@value #RW}, {@value #RWD}, or {@value #RWS}.
  114.      *
  115.      * @param mode one of {@value #R}, {@value #RW}, {@value #RWD}, or {@value #RWS}.
  116.      * @return A RandomAccessFileMode.
  117.      * @throws IllegalArgumentException Thrown when mode is not one of {@value #R}, {@value #RW}, {@value #RWD}, or {@value #RWS}.
  118.      * @since 2.18.0
  119.      */
  120.     public static RandomAccessFileMode valueOfMode(final String mode) {
  121.         switch (mode) {
  122.         case R:
  123.             return READ_ONLY;
  124.         case RW:
  125.             return READ_WRITE;
  126.         case RWD:
  127.             return READ_WRITE_SYNC_CONTENT;
  128.         case RWS:
  129.             return READ_WRITE_SYNC_ALL;
  130.         }
  131.         throw new IllegalArgumentException(mode);
  132.     }

  133.     private final int level;

  134.     private final String mode;

  135.     RandomAccessFileMode(final String mode, final int level) {
  136.         this.mode = mode;
  137.         this.level = level;
  138.     }

  139.     /**
  140.      * Performs an operation on the {@link RandomAccessFile} specified at the given {@link Path}.
  141.      * <p>
  142.      * This method allocates and releases the {@link RandomAccessFile} given to the consumer.
  143.      * </p>
  144.      *
  145.      * @param file the file specifying the {@link RandomAccessFile} to open.
  146.      * @param consumer the function to apply.
  147.      * @throws FileNotFoundException See {@link IORandomAccessFile#IORandomAccessFile(File, String)}.
  148.      * @throws IOException Thrown by the given function.
  149.      * @since 2.18.0
  150.      */
  151.     public void accept(final Path file, final IOConsumer<RandomAccessFile> consumer) throws IOException {
  152.         try (RandomAccessFile raf = create(file)) {
  153.             consumer.accept(raf);
  154.         }
  155.     }

  156.     /**
  157.      * Applies the given function for a {@link RandomAccessFile} specified at the given {@link Path}.
  158.      * <p>
  159.      * This method allocates and releases the {@link RandomAccessFile} given to the function.
  160.      * </p>
  161.      *
  162.      * @param <T> the return type of the function.
  163.      * @param file the file specifying the {@link RandomAccessFile} to open.
  164.      * @param function the function to apply.
  165.      * @return the function's result value.
  166.      * @throws FileNotFoundException See {@link IORandomAccessFile#IORandomAccessFile(File, String)}.
  167.      * @throws IOException Thrown by the given function.
  168.      * @since 2.18.0
  169.      */
  170.     public <T> T apply(final Path file, final IOFunction<RandomAccessFile, T> function) throws IOException {
  171.         try (RandomAccessFile raf = create(file)) {
  172.             return function.apply(raf);
  173.         }
  174.     }

  175.     /**
  176.      * Constructs a random access file to read from, and optionally to write to, the file specified by the {@link File} argument.
  177.      * <p>
  178.      * Prefer {@link #create(Path)} over this.
  179.      * </p>
  180.      *
  181.      * @param file the file object
  182.      * @return a random access file
  183.      * @throws FileNotFoundException See {@link IORandomAccessFile#IORandomAccessFile(File, String)}.
  184.      */
  185.     public RandomAccessFile create(final File file) throws FileNotFoundException {
  186.         return new IORandomAccessFile(file, mode);
  187.     }

  188.     /**
  189.      * Constructs a random access file to read from, and optionally to write to, the file specified by the {@link File} argument.
  190.      *
  191.      * @param file the file object
  192.      * @return a random access file
  193.      * @throws FileNotFoundException See {@link IORandomAccessFile#IORandomAccessFile(File, String)}.
  194.      */
  195.     public RandomAccessFile create(final Path file) throws FileNotFoundException {
  196.         return create(Objects.requireNonNull(file.toFile(), "file"));
  197.     }

  198.     /**
  199.      * Constructs a random access file to read from, and optionally to write to, the file specified by the {@link File} argument.
  200.      * <p>
  201.      * Prefer {@link #create(Path)} over this.
  202.      * </p>
  203.      *
  204.      * @param name the file object
  205.      * @return a random access file
  206.      * @throws FileNotFoundException See {@link IORandomAccessFile#IORandomAccessFile(File, String)}.
  207.      */
  208.     public RandomAccessFile create(final String name) throws FileNotFoundException {
  209.         return new IORandomAccessFile(name, mode);
  210.     }

  211.     /**
  212.      * A level for relative comparison of access mode rights, the larger, the more access.
  213.      * <p>
  214.      * The relative order from lowest to highest access rights is:
  215.      * </p>
  216.      * <ol>
  217.      * <li>{@link #READ_ONLY}</li>
  218.      * <li>{@link #READ_WRITE}</li>
  219.      * <li>{@link #READ_WRITE_SYNC_CONTENT}</li>
  220.      * <li>{@link #READ_WRITE_SYNC_ALL}</li>
  221.      * </ol>
  222.      * <p>
  223.      * This is unrelated to {@link #ordinal()}.
  224.      * </p>
  225.      *
  226.      * @return A level for relative comparison.
  227.      */
  228.     private int getLevel() {
  229.         return level;
  230.     }

  231.     /**
  232.      * Gets the access mode, one of {@value #R}, {@value #RW}, {@value #RWD}, or {@value #RWS}.
  233.      *
  234.      * @return one of {@value #R}, {@value #RW}, {@value #RWD}, or {@value #RWS}.
  235.      * @since 2.18.0
  236.      */
  237.     public String getMode() {
  238.         return mode;
  239.     }

  240.     /**
  241.      * Tests whether this mode implies the given {@code other} mode.
  242.      * <p>
  243.      * For example:
  244.      * </p>
  245.      * <ol>
  246.      * <li>{@link RandomAccessFileMode#READ_WRITE_SYNC_ALL} implies {{@link RandomAccessFileMode#READ_WRITE_SYNC_CONTENT}}.</li>
  247.      * <li>{@link RandomAccessFileMode#READ_WRITE_SYNC_CONTENT} implies {{@link RandomAccessFileMode#READ_WRITE}}.</li>
  248.      * <li>{@link RandomAccessFileMode#READ_WRITE} implies {{@link RandomAccessFileMode#READ_ONLY}}.</li>
  249.      * </ol>
  250.      *
  251.      * @param other the non-null mode to test against.
  252.      * @return whether this mode implies the given {@code other} mode.
  253.      * @since 2.18.0
  254.      */
  255.     public boolean implies(final RandomAccessFileMode other) {
  256.         // Note: The method name "implies" is inspired by java.security.Permission.implies(Permission)
  257.         return getLevel() >= other.getLevel();
  258.     }

  259.     /**
  260.      * Constructs a random access file to read from, and optionally to write to, the file specified by the {@link File} argument.
  261.      *
  262.      * @param name the file object
  263.      * @return a random access file
  264.      * @throws FileNotFoundException See {@link IORandomAccessFile#IORandomAccessFile(File, String)}.
  265.      * @since 2.18.0
  266.      */
  267.     public IORandomAccessFile io(final String name) throws FileNotFoundException {
  268.         return new IORandomAccessFile(name, mode);
  269.     }

  270. }