RandomAccessFiles.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.IOException;
  19. import java.io.RandomAccessFile;
  20. import java.nio.channels.SeekableByteChannel;
  21. import java.util.Objects;

  22. import org.apache.commons.io.channels.FileChannels;

  23. /**
  24.  * Works with {@link RandomAccessFile}.
  25.  *
  26.  * @since 2.13.0
  27.  */
  28. public class RandomAccessFiles {

  29.     /**
  30.      * Tests if two RandomAccessFile contents are equal.
  31.      *
  32.      * @param raf1 A RandomAccessFile.
  33.      * @param raf2 Another RandomAccessFile.
  34.      * @return true if the contents of both RandomAccessFiles are equal, false otherwise.
  35.      * @throws IOException if an I/O error occurs.
  36.      * @since 2.15.0
  37.      */
  38.     @SuppressWarnings("resource") // See comments
  39.     public static boolean contentEquals(final RandomAccessFile raf1, final RandomAccessFile raf2) throws IOException {
  40.         // Short-circuit test
  41.         if (Objects.equals(raf1, raf2)) {
  42.             return true;
  43.         }
  44.         // Short-circuit test
  45.         final long length1 = length(raf1);
  46.         final long length2 = length(raf2);
  47.         if (length1 != length2) {
  48.             return false;
  49.         }
  50.         if (length1 == 0 && length2 == 0) {
  51.             return true;
  52.         }
  53.         // Dig in and to the work
  54.         // We do not close FileChannels because that would close the owning RandomAccessFile.
  55.         // Instead, the caller is assumed to manage the given RandomAccessFile objects.
  56.         return FileChannels.contentEquals((SeekableByteChannel) raf1.getChannel(), raf2.getChannel(), IOUtils.DEFAULT_BUFFER_SIZE);
  57.     }

  58.     private static long length(final RandomAccessFile raf) throws IOException {
  59.         return raf != null ? raf.length() : 0;
  60.     }

  61.     /**
  62.      * Reads a byte array starting at "position" for "length" bytes.
  63.      *
  64.      * @param input    The source RandomAccessFile.
  65.      * @param position The offset position, measured in bytes from the beginning of the file, at which to set the file pointer.
  66.      * @param length   How many bytes to read.
  67.      * @return a new byte array.
  68.      * @throws IOException If the first byte cannot be read for any reason other than end of file, or if the random access file has been closed, or if some
  69.      *                     other I/O error occurs.
  70.      */
  71.     public static byte[] read(final RandomAccessFile input, final long position, final int length) throws IOException {
  72.         input.seek(position);
  73.         return IOUtils.toByteArray(input::read, length);
  74.     }

  75.     /**
  76.      * Resets the given file to position 0.
  77.      *
  78.      * @param raf The RandomAccessFile to reset.
  79.      * @return The given RandomAccessFile.
  80.      * @throws IOException If {@code pos} is less than {@code 0} or if an I/O error occurs.
  81.      * @since 2.15.0
  82.      */
  83.     public static RandomAccessFile reset(final RandomAccessFile raf) throws IOException {
  84.         raf.seek(0);
  85.         return raf;
  86.     }

  87.     /**
  88.      * Make private in 3.0.
  89.      *
  90.      * @deprecated TODO Make private in 3.0.
  91.      */
  92.     @Deprecated
  93.     public RandomAccessFiles() {
  94.         // empty
  95.     }
  96. }