All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.dstadler.audio.download.RangeDownloadFile Maven / Gradle / Ivy

There is a newer version: 1.2.1
Show newest version
package org.dstadler.audio.download;

import com.google.common.base.Preconditions;
import org.dstadler.commons.logging.jdk.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.logging.Logger;

/**
 * Implementation of the RangeDownload interface for reading
 * data from local files
 */
public class RangeDownloadFile implements RangeDownload {
    private final static Logger log = LoggerFactory.make();

    private final File file;
    private final long length;
    private boolean closed = false;

    public RangeDownloadFile(File file) {
        this.file = file;
        this.length = file.length();
    }

    @Override
    public long getLength() {
        return length;
    }

    @Override
    public byte[] readRange(long start, int size) throws IOException {
        if(closed) {
            throw new IllegalStateException("Already closed");
        }
        long length = getLength();

        // don't try to read beyond the end
        Preconditions.checkArgument(start <= length,
                "Tried to start reading beyond the end of the file. " +
                        "Size of stream: %s, position to read: %s, size to read: %s",
                length, start, size);

        if(start + size > length) {
            log.info("Reducing number of bytes to read for " + file + " at position " + start +
                    " from " + size + " bytes to " + (length - start) +
                    " bytes because it would exceed the length of the stream of " + length + " bytes");
            size = (int)(length - start);
        }

        Preconditions.checkArgument(start >= 0,
                "Had an invalid download-start %s for size %s and length: %s",
                start, size, length);

        Preconditions.checkArgument(size >= 1,
                "Had an invalid download-size %s for start %s and length: %s",
                size, start, length);

        byte[] bytes = new byte[size];
        try (RandomAccessFile raf = new RandomAccessFile(file, "r")) {
            raf.seek(start);

            raf.read(bytes);
        }

        return bytes;
    }

    @Override
    public void close() {
        // just remember closed state for throwing exception on reading
        closed = true;
    }

    @Override
    public String toString() {
        return "RangeDownloadHTTP{" +
                "file='" + file + '\'' +
                ", length=" + getLength() +
                '}';
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy