ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Amad Fida <amad.f...@gmail.com>
Subject Re: Custom Type Handler
Date Fri, 04 Feb 2005 02:11:33 GMT
This is something i have been using and works fine for me.

public class MyBlobTypeHandlerCallback implements TypeHandlerCallback {

    private static final byte BLOB_NULL = 127;
    private static byte[] blobNull = null;

    public Object getResult(ResultGetter getter) throws SQLException {
        byte[] bytes = null;
        InputStream is = null;
        ByteArrayOutputStream bos = null;
        try {
            is = getter.getBlob().getBinaryStream();
            if (is != null) {
                bos = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];
                int bytesRead = is.read(buffer);
                while (bytesRead >= 0) {
                    if (bytesRead > 0) {
                        bos.write(buffer, 0, bytesRead);
                    }
                    bytesRead = is.read(buffer);
                }
                bos.flush();
                bytes = bos.toByteArray();
                if (isBlobNull(bytes)) {
                    bytes = null;
                }
            }
        }
        catch (Exception ex) {
            throw new SQLException("Unable to read the blob: " + ex.toString());
        }
        finally {
            FileUtil.closeStream(is);
            FileUtil.closeStream(bos);
        }
        return bytes;
    }

    public void setParameter(ParameterSetter setter, Object parameter)
throws SQLException {
        byte[] bytes = (byte[])parameter;
        if (bytes == null) {
            bytes = getBlobNull();
        }
        setter.setBinaryStream(new ByteArrayInputStream(bytes), bytes.length);
    }

    public Object valueOf(String s) {
        return s;
    }

    /**
     * Determines if the blob actually represent a null.
     */
    private boolean isBlobNull(byte[] bytes) {
        return bytes != null && bytes.length == 1 && bytes[0] == BLOB_NULL;
    }

    /**
     * Gets the blob that represent a null.
     */
    private byte[] getBlobNull() {
        if (blobNull == null) {
            byte[] bn = new byte[1];
            bn[0] = BLOB_NULL;
            blobNull = bn;
        }
        return blobNull;
    }

}


On Thu, 3 Feb 2005 17:42:54 -0800, Tony Li <tony@digitalcentral.net> wrote:
>  
>  
> 
> Hi all – 
> 
>   
> 
> I'm having trouble using the default BLOB/CLOB handlers in 2.0.9 w/ DB2 v
> 8.1.  Using SqlMap, I can read the BLOBs into a byte[] by specifying
> jdbcType="BLOB", but I can't write that byte[] back out in a mapped INSERT
> statement. 
> 
>   
> 
> I keep reading in the archives about a solution through the use of custom
> type handlers.  Sounds like something I should try, but I can't access that
> .zip example (it was removed by Clinton I think) or find any other
> documentation or better yet, an example of a custom type handler.  Can
> anyone point me in the right direction? 
> 
>   
> 
> Thanks, 
> 
> Tony 


-- 
Amad Fida

Mime
View raw message