// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
package test;

import java.io.IOException;

import org.alfresco.jlan.server.filesys.NetworkFile;

public class FakeFile extends NetworkFile {
    private byte[] cont;
    private int contlen;
    public FakeFile(String name) {
        super(name);
        cont = new byte[10000];
        contlen = 0;
        setFileSize(contlen);
        setCreationDate(System.currentTimeMillis());
        setModifyDate(System.currentTimeMillis());
        setAccessDate(System.currentTimeMillis());
    }
    public void closeFile() throws IOException {
        // ignore
    }
    public void flushFile() throws IOException {
        // ignore
    }
    public void openFile(boolean arg0) throws IOException {
        // ignore
    }
    public int readFile(byte[] buf, int ix, int len, long pos) throws IOException {
        setAccessDate(System.currentTimeMillis());
        int trulen = Math.min(len, contlen - (int)pos);
        System.arraycopy(cont, (int)pos, buf, ix, trulen);
        return trulen;
    }
    public long seekFile(long pos, int typ) throws IOException {
        return pos;
    }
    public void truncateFile(long pos) throws IOException {
        contlen = (int)pos;
        setModifyDate(System.currentTimeMillis());
        setAccessDate(System.currentTimeMillis());
    }
    public void writeFile(byte[] buf, int ix, int len, long pos) throws IOException {
        System.arraycopy(buf, ix, cont, (int)pos, len);
        contlen = Math.max(contlen, (int)pos + len);
        setFileSize(contlen);
        setModifyDate(System.currentTimeMillis());
        setAccessDate(System.currentTimeMillis());
    }
}
