Regalamiunsorriso/decompiled-libs/www/acxent-videoj-1.0.0/org/jcodec/common/AutoFileChannelWrapper.java

99 lines
2.4 KiB
Java
Raw Normal View History

2026-04-22 18:41:37 +02:00
package org.jcodec.common;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import org.jcodec.common.io.AutoPool;
import org.jcodec.common.io.AutoResource;
import org.jcodec.common.io.SeekableByteChannel;
public class AutoFileChannelWrapper implements SeekableByteChannel, AutoResource {
private static final long THRESHOLD = 5000L;
private FileChannel ch;
private File file;
private long savedPos;
private long curTime;
private long accessTime;
public AutoFileChannelWrapper(File file) throws IOException {
this.file = file;
this.curTime = System.currentTimeMillis();
AutoPool.getInstance().add(this);
ensureOpen();
}
private void ensureOpen() throws IOException {
this.accessTime = this.curTime;
if (this.ch == null || !this.ch.isOpen()) {
this.ch = new FileInputStream(this.file).getChannel();
this.ch.position(this.savedPos);
}
}
public int read(ByteBuffer arg0) throws IOException {
ensureOpen();
int r = this.ch.read(arg0);
this.savedPos = this.ch.position();
return r;
}
public void close() throws IOException {
if (this.ch != null && this.ch.isOpen()) {
this.savedPos = this.ch.position();
this.ch.close();
this.ch = null;
}
}
public boolean isOpen() {
return (this.ch != null && this.ch.isOpen());
}
public int write(ByteBuffer arg0) throws IOException {
ensureOpen();
int w = this.ch.write(arg0);
this.savedPos = this.ch.position();
return w;
}
public long position() throws IOException {
ensureOpen();
return this.ch.position();
}
public SeekableByteChannel setPosition(long newPosition) throws IOException {
ensureOpen();
this.ch.position(newPosition);
this.savedPos = newPosition;
return this;
}
public long size() throws IOException {
ensureOpen();
return this.ch.size();
}
public SeekableByteChannel truncate(long size) throws IOException {
ensureOpen();
this.ch.truncate(size);
this.savedPos = this.ch.position();
return this;
}
public void setCurTime(long curTime) {
this.curTime = curTime;
if (this.ch != null && this.ch.isOpen() && curTime - this.accessTime > 5000L)
try {
close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}