package org.hibernate.engine.jdbc;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Clob;
import java.sql.SQLException;
import org.hibernate.engine.jdbc.internal.CharacterStreamImpl;
import org.hibernate.type.descriptor.java.DataHelper;
public class ClobProxy implements InvocationHandler {
private static final Class[] PROXY_INTERFACES = new Class[] { Clob.class, ClobImplementer.class };
private final CharacterStream characterStream;
private boolean needsReset;
protected ClobProxy(String string) {
this.characterStream = new CharacterStreamImpl( string );
}
protected ClobProxy(Reader reader, long length) {
this.characterStream = new CharacterStreamImpl( reader, length );
}
protected long getLength() {
return characterStream.getLength();
}
protected InputStream getAsciiStream() throws SQLException {
return new ReaderInputStream( getCharacterStream() );
}
protected Reader getCharacterStream() throws SQLException {
return getUnderlyingStream().asReader();
}
protected CharacterStream getUnderlyingStream() throws SQLException {
resetIfNeeded();
return characterStream;
}
protected String getSubString(long start, int length) {
final String string = characterStream.asString();
final int endIndex = Math.min( ( (int) start ) + length, string.length() );
return string.substring( (int) start, endIndex );
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
final String methodName = method.getName();
final int argCount = method.getParameterCount();
if ( "length".equals( methodName ) && argCount == 0 ) {
return getLength();
}
if ( "getUnderlyingStream".equals( methodName ) ) {
return getUnderlyingStream();
}
if ( "getAsciiStream".equals( methodName ) && argCount == 0 ) {
return getAsciiStream();
}
if ( "getCharacterStream".equals( methodName ) ) {
if ( argCount == 0 ) {
return getCharacterStream();
}
else if ( argCount == 2 ) {
final long start = (Long) args[0];
if ( start < 1 ) {
throw new SQLException( "Start position 1-based; must be 1 or more." );
}
if ( start > getLength() ) {
throw new SQLException( "Start position [" + start + "] cannot exceed overall CLOB length [" + getLength() + "]" );
}
final int length = (Integer) args[1];
if ( length < 0 ) {
throw new SQLException( "Length must be great-than-or-equal to zero." );
}
return DataHelper.subStream( getCharacterStream(), start-1, length );
}
}
if ( "getSubString".equals( methodName ) && argCount == 2 ) {
final long start = (Long) args[0];
if ( start < 1 ) {
throw new SQLException( "Start position 1-based; must be 1 or more." );
}
if ( start > getLength() ) {
throw new SQLException( "Start position [" + start + "] cannot exceed overall CLOB length [" + getLength() + "]" );
}
final int length = (Integer) args[1];
if ( length < 0 ) {
throw new SQLException( "Length must be great-than-or-equal to zero." );
}
return getSubString( start-1, length );
}
if ( "free".equals( methodName ) && argCount == 0 ) {
characterStream.release();
return null;
}
if ( "toString".equals( methodName ) && argCount == 0 ) {
return this.toString();
}
if ( "equals".equals( methodName ) && argCount == 1 ) {
return proxy == args[0];
}
if ( "hashCode".equals( methodName ) && argCount == 0 ) {
return this.hashCode();
}
throw new UnsupportedOperationException( "Clob may not be manipulated from creating session" );
}
protected void resetIfNeeded() throws SQLException {
try {
if ( needsReset ) {
characterStream.asReader().reset();
}
}
catch ( IOException ioe ) {
throw new SQLException( "could not reset reader", ioe );
}
needsReset = true;
}
public static Clob generateProxy(String string) {
return (Clob) Proxy.newProxyInstance( getProxyClassLoader(), PROXY_INTERFACES, new ClobProxy( string ) );
}
public static Clob generateProxy(Reader reader, long length) {
return (Clob) Proxy.newProxyInstance( getProxyClassLoader(), PROXY_INTERFACES, new ClobProxy( reader, length ) );
}
protected static ClassLoader getProxyClassLoader() {
return ClobImplementer.class.getClassLoader();
}
}