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

org.nutz.dao.util.blob.SimpleBlob Maven / Gradle / Ivy

Go to download

Nutz, which is a collections of lightweight frameworks, each of them can be used independently

There is a newer version: 1.r.72
Show newest version
package org.nutz.dao.util.blob;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.sql.Blob;
import java.sql.SQLException;

import org.nutz.dao.jdbc.Jdbcs;
import org.nutz.lang.Files;
import org.nutz.lang.Lang;
import org.nutz.lang.Streams;

/**
 * 如果数据已经是byte[],请使用javax.sql.rowset.serial.SerialBlob
 * @author wendal([email protected])
 *
 */
public class SimpleBlob implements Blob, Serializable {

    private static final long serialVersionUID = 4192412466410263969L;
    
    protected File file;

    public SimpleBlob() {}
    
    /**
     * 如果数据已经是byte[],请使用javax.sql.rowset.serial.SerialBlob
     */
    public SimpleBlob(File f) {
        this.file = f;
    }

    public long length() throws SQLException {
        return file.length();
    }

    public byte[] getBytes(long pos, int length) throws SQLException {
        if (pos == 1 && length == length()) {
            return Streams.readBytesAndClose(getBinaryStream());
        }
        throw Lang.noImplement();
    }

    public InputStream getBinaryStream() throws SQLException {
        return Streams.buff(Streams.fileIn(file));
    }

    public long position(byte[] pattern, long start) throws SQLException {
        throw Lang.noImplement();
    }

    public long position(Blob pattern, long start) throws SQLException {
        throw Lang.noImplement();
    }

    public int setBytes(long pos, byte[] bytes) throws SQLException {
        throw Lang.noImplement();
    }

    public int setBytes(long pos, byte[] bytes, int offset, int len) throws SQLException {
        throw Lang.noImplement();
    }

    public OutputStream setBinaryStream(long pos) throws SQLException {
        throw Lang.noImplement();
    }

    public void truncate(long len) throws SQLException {
        Files.write(file, new Byte[]{});
    }

    public void free() throws SQLException {
        Files.deleteFile(file);
    }

    public InputStream getBinaryStream(long pos, long length) throws SQLException {
        throw Lang.noImplement();
    }

    private void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException {
        Streams.writeAndClose(out, new FileInputStream(file));
    }
    private void readObject(java.io.ObjectInputStream in) throws java.io.IOException, ClassNotFoundException{
        file = Jdbcs.getFilePool().createFile(".blob");
        Files.write(file, in);
    }
    
    public void setFile(File file) {
        this.file = file;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy