Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 1998-2020 University Corporation for Atmospheric Research/Unidata
* See LICENSE for license information.
*/
package ucar.unidata.io;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.WritableByteChannel;
import java.time.Duration;
import java.util.concurrent.ExecutionException;
import javax.annotation.Nonnull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** An abstract superclass for remote RandomAccessFile */
// not immutable because RandomAccessFile is not immutable.
public abstract class RemoteRandomAccessFile extends ucar.unidata.io.RandomAccessFile implements ReadableRemoteFile {
private static final Logger logger = LoggerFactory.getLogger(RemoteRandomAccessFile.class);
// 10 MiB default maximum loading cache size
protected static final long defaultMaxReadCacheSize = 10485760;
// 256 KiB default remote file buffer size
protected static final int defaultRemoteFileBufferSize = 262144;
// default connection timeout in milliseconds (10 seconds)
protected static final int defaultRemoteFileTimeout = 10 * 1000;
// default cache time to live in milliseconds
private static final long defaultReadCacheTimeToLive = 30 * 1000;
protected final String url;
private final boolean readCacheEnabled;
private final int readCacheBlockSize;
private final LoadingCache readCache;
protected RemoteRandomAccessFile(String url, int bufferSize, long maxRemoteCacheSize) {
super(bufferSize);
this.url = url;
file = null;
location = url;
// Only enable cache if its maximum size is at least 2x the buffer size, both of which are configurable
// at runtime
int minimumCacheActivationSize = 2 * bufferSize;
if (maxRemoteCacheSize >= minimumCacheActivationSize) {
// have each cache block hold a 1 buffer sized chunk
this.readCacheBlockSize = bufferSize;
// user set max cache size in bytes
// guava cache set as number of objects
// The question here is, how many readCacheBlockSize objects would there be in maxRemoteCacheSize bytes?
// total max cache size in bytes / size of one cache block, rounded up.
long numberOfCacheBlocks = (maxRemoteCacheSize / readCacheBlockSize) + 1;
this.readCache = initCache(numberOfCacheBlocks, Duration.ofMillis(defaultReadCacheTimeToLive));
readCacheEnabled = true;
} else {
this.readCacheBlockSize = -1;
readCacheEnabled = false;
readCache = null;
}
}
private LoadingCache initCache(long maximumNumberOfCacheBlocks, java.time.Duration timeToLive) {
CacheBuilder