001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.io;
019
020import java.io.IOException;
021import java.io.RandomAccessFile;
022import java.nio.channels.SeekableByteChannel;
023import java.util.Objects;
024
025import org.apache.commons.io.channels.FileChannels;
026
027/**
028 * Works with {@link RandomAccessFile}.
029 *
030 * @since 2.13.0
031 */
032public class RandomAccessFiles {
033
034    /**
035     * Tests if two RandomAccessFile contents are equal.
036     *
037     * @param raf1 A RandomAccessFile.
038     * @param raf2 Another RandomAccessFile.
039     * @return true if the contents of both RandomAccessFiles are equal, false otherwise.
040     * @throws IOException if an I/O error occurs.
041     * @since 2.15.0
042     */
043    @SuppressWarnings("resource") // See comments
044    public static boolean contentEquals(final RandomAccessFile raf1, final RandomAccessFile raf2) throws IOException {
045        // Short-circuit test
046        if (Objects.equals(raf1, raf2)) {
047            return true;
048        }
049        // Short-circuit test
050        final long length1 = length(raf1);
051        final long length2 = length(raf2);
052        if (length1 != length2) {
053            return false;
054        }
055        if (length1 == 0 && length2 == 0) {
056            return true;
057        }
058        // Dig in and to the work
059        // We do not close FileChannels because that would close the owning RandomAccessFile.
060        // Instead, the caller is assumed to manage the given RandomAccessFile objects.
061        return FileChannels.contentEquals((SeekableByteChannel) raf1.getChannel(), raf2.getChannel(), IOUtils.DEFAULT_BUFFER_SIZE);
062    }
063
064    private static long length(final RandomAccessFile raf) throws IOException {
065        return raf != null ? raf.length() : 0;
066    }
067
068    /**
069     * Reads a byte array starting at "position" for "length" bytes.
070     *
071     * @param input    The source RandomAccessFile.
072     * @param position The offset position, measured in bytes from the beginning of the file, at which to set the file pointer.
073     * @param length   How many bytes to read.
074     * @return a new byte array.
075     * @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
076     *                     other I/O error occurs.
077     */
078    public static byte[] read(final RandomAccessFile input, final long position, final int length) throws IOException {
079        input.seek(position);
080        return IOUtils.toByteArray(input::read, length);
081    }
082
083    /**
084     * Resets the given file to position 0.
085     *
086     * @param raf The RandomAccessFile to reset.
087     * @return The given RandomAccessFile.
088     * @throws IOException If {@code pos} is less than {@code 0} or if an I/O error occurs.
089     * @since 2.15.0
090     */
091    public static RandomAccessFile reset(final RandomAccessFile raf) throws IOException {
092        raf.seek(0);
093        return raf;
094    }
095
096    /**
097     * Make private in 3.0.
098     *
099     * @deprecated TODO Make private in 3.0.
100     */
101    @Deprecated
102    public RandomAccessFiles() {
103        // empty
104    }
105}