CREATE OR REPLACE AND RESOLVE JAVA SOURCE NAMED "LobCompressor" AS
import java.lang.*;
import oracle.sql.*;
import java.io.*;
import java.util.zip.InflaterInputStream;
import java.util.zip.DeflaterOutputStream;

/**
 * A simple class for LOB compression and decompression in Oracle Database. Will work in 8i and
better.
 *
 * @author <a href="mailto:pjarmuz@poczta.onet.pl">Piotr Jarmuz</a>
 */
public class LobCompressor {
/**
 * Compresses the CLOB into BLOB
 *
 * @param clob the source CLOB (plain text)
 * @param blob the target BLOB (will hold compressed binary data) it should be an empty BLOB
retrieved for example with dbms_lob.createtemporary(l_blob,true);
 * @throws Exception mostly I/O exception if ever
 */
    public static void compress(CLOB clob, BLOB blob) throws Exception {
 Reader reader = new BufferedReader(clob.getCharacterStream());
    OutputStreamWriter writer = new OutputStreamWriter(
      new DeflaterOutputStream(blob.getBinaryOutputStream()), "UTF8"
    );
    int length;
    char[] buf = new char[clob.getChunkSize()];
    while ((length = reader.read(buf, 0, clob.getChunkSize())) != -1) {
      writer.write(buf, 0, length);
    }
    reader.close();
    writer.close();
    }

/**
 * Decompresses the BLOB into CLOB
 *
 * @param blob the source BLOB (compressed binary data)
 * @param clob the target CLOB (will hold plain text) it should be an empty CLOB retrieved for
example with dbms_lob.createtemporary(l_clob,true);
 * @throws Exception mostly I/O exception if ever
 */
    public static void decompress(BLOB blob, CLOB clob) throws Exception {
    Reader reader = new InputStreamReader(
      new InflaterInputStream(blob.getBinaryStream()), "UTF8"
    );
    Writer writer = clob.getCharacterOutputStream();

    char[] chars = new char[clob.getChunkSize()];
    int iChar;
    while ((iChar = reader.read(chars)) != -1) {
      writer.write(chars, 0, iChar);
    }
    reader.close();
    writer.close();
    }

/**
 * Compresses the BLOB into BLOB
 *
 * @param slob the source BLOB (plain binary data)
 * @param blob the target BLOB (will hold compressed binary data) it should be an empty BLOB
retrieved for example with dbms_lob.createtemporary(l_blob,true);
 * @throws Exception mostly I/O exception if ever
 */
    public static void compress(BLOB slob, BLOB blob) throws Exception {
        InputStream in=slob.getBinaryStream();
        DeflaterOutputStream z=new DeflaterOutputStream(blob.getBinaryOutputStream());
        byte[] buffer=new byte[slob.getBufferSize()];

        int cnt;
        while ((cnt=in.read(buffer))!=-1) {
            z.write(buffer,0,cnt);
        }
        in.close();
        z.close();
    }

/**
 * Decompresses the BLOB into CLOB
 *
 * @param blob the source BLOB (compressed binary data)
 * @param slob the target CLOB (will hold plain binary data) it should be an empty CLOB retrieved
for example with dbms_lob.createtemporary(l_blob,true);
 * @throws Exception mostly I/O exception if ever
 */
    public static void decompress(BLOB blob, BLOB slob) throws Exception {
        OutputStream out=slob.getBinaryOutputStream();
        InflaterInputStream z=new InflaterInputStream(blob.getBinaryStream());
        byte[] buffer=new byte[blob.getBufferSize()];

        int cnt;
        while ((cnt=z.read(buffer))!=-1) {
            out.write(buffer,0,cnt);
        }
        z.close();
        out.close();
    }
};
/

