BlobFactoryImpl.java 1.57 KB
/*
 * Decompiled with CFR 0_118.
 */
package com.day.crx.delite.impl.support;

import com.day.crx.delite.impl.support.Blob;
import com.day.crx.delite.impl.support.BlobFactory;
import com.day.crx.delite.impl.support.BlobImpl;
import com.day.crx.delite.impl.support.BlobOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class BlobFactoryImpl
implements BlobFactory {
    private final Set<BlobImpl> blobs = new HashSet<BlobImpl>();

    public Blob create(byte[] bytes, int off, int len) throws IOException {
        byte[] data = new byte[len];
        System.arraycopy(bytes, off, data, 0, len);
        return this.register(new BlobImpl(this, data));
    }

    public Blob create(byte[] bytes) throws IOException {
        return this.create(bytes, 0, bytes.length);
    }

    public BlobOutputStream createOutputStream() throws IOException {
        return new BlobOutputStream(this);
    }

    BlobImpl register(BlobImpl blob) {
        this.blobs.add(blob);
        return blob;
    }

    void discard(BlobImpl blob) throws IOException {
        blob.discard();
    }

    File allocateTmpFile() throws IOException {
        return File.createTempFile("__crx", ".dat");
    }

    void freeTmpFile(File file) throws IOException {
        file.deleteOnExit();
        file.delete();
    }

    public void close() throws IOException {
        Iterator<BlobImpl> iter = this.blobs.iterator();
        while (iter.hasNext()) {
            iter.next().close();
            iter.remove();
        }
    }
}