Skip to content

Commit d4b2d0f

Browse files
committed
fix a bug in ClobProxy
Signed-off-by: Gavin King <[email protected]>
1 parent 2d9f58a commit d4b2d0f

File tree

1 file changed

+68
-55
lines changed

1 file changed

+68
-55
lines changed

hibernate-core/src/main/java/org/hibernate/engine/jdbc/ClobProxy.java

Lines changed: 68 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -97,65 +97,78 @@ protected String getSubString(long start, int length) {
9797
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
9898
final String methodName = method.getName();
9999
final int argCount = method.getParameterCount();
100-
101-
if ( "length".equals( methodName ) && argCount == 0 ) {
102-
return getLength();
103-
}
104-
if ( "getUnderlyingStream".equals( methodName ) ) {
105-
return getUnderlyingStream(); // Reset stream if needed.
106-
}
107-
if ( "getAsciiStream".equals( methodName ) && argCount == 0 ) {
108-
return getAsciiStream();
109-
}
110-
if ( "getCharacterStream".equals( methodName ) ) {
111-
if ( argCount == 0 ) {
112-
return getCharacterStream();
113-
}
114-
else if ( argCount == 2 ) {
115-
final long start = (Long) args[0];
116-
if ( start < 1 ) {
117-
throw new SQLException( "Start position 1-based; must be 1 or more." );
100+
switch ( methodName ) {
101+
case "length":
102+
if ( argCount == 0 ) {
103+
return getLength();
118104
}
119-
if ( start > getLength() ) {
120-
throw new SQLException( "Start position [" + start + "] cannot exceed overall CLOB length [" + getLength() + "]" );
105+
break;
106+
case "getUnderlyingStream":
107+
return getUnderlyingStream(); // Reset stream if needed
108+
case "getCharacterStream":
109+
if ( argCount == 0 ) {
110+
return getCharacterStream();
121111
}
122-
final int length = (Integer) args[1];
123-
if ( length < 0 ) {
124-
// java docs specifically say for getCharacterStream(long,int) that the start+length must not exceed the
125-
// total length, however that is at odds with the getSubString(long,int) behavior.
126-
throw new SQLException( "Length must be great-than-or-equal to zero." );
112+
else if ( argCount == 2 ) {
113+
final long start = (Long) args[0];
114+
if ( start < 1 ) {
115+
throw new SQLException( "Start position 1-based; must be 1 or more." );
116+
}
117+
if ( start > getLength() + 1 ) {
118+
throw new SQLException( "Start position [" + start + "] cannot exceed overall CLOB length [" + getLength() + "]" );
119+
}
120+
final int length = (Integer) args[1];
121+
if ( length < 0 ) {
122+
// javadoc for getCharacterStream(long,int) specify that the start+length must not exceed the
123+
// total length (this is at odds with the behavior of getSubString(long,int))
124+
throw new SQLException( "Length must be greater than or equal to zero" );
125+
}
126+
return DataHelper.subStream( getCharacterStream(), start-1, length );
127127
}
128-
return DataHelper.subStream( getCharacterStream(), start-1, length );
129-
}
130-
}
131-
if ( "getSubString".equals( methodName ) && argCount == 2 ) {
132-
final long start = (Long) args[0];
133-
if ( start < 1 ) {
134-
throw new SQLException( "Start position 1-based; must be 1 or more." );
135-
}
136-
if ( start > getLength() ) {
137-
throw new SQLException( "Start position [" + start + "] cannot exceed overall CLOB length [" + getLength() + "]" );
138-
}
139-
final int length = (Integer) args[1];
140-
if ( length < 0 ) {
141-
throw new SQLException( "Length must be great-than-or-equal to zero." );
142-
}
143-
return getSubString( start-1, length );
144-
}
145-
if ( "free".equals( methodName ) && argCount == 0 ) {
146-
characterStream.release();
147-
return null;
148-
}
149-
if ( "toString".equals( methodName ) && argCount == 0 ) {
150-
return this.toString();
151-
}
152-
if ( "equals".equals( methodName ) && argCount == 1 ) {
153-
return proxy == args[0];
154-
}
155-
if ( "hashCode".equals( methodName ) && argCount == 0 ) {
156-
return this.hashCode();
128+
break;
129+
case "getAsciiStream":
130+
if ( argCount == 0 ) {
131+
return getAsciiStream();
132+
}
133+
break;
134+
case "getSubString":
135+
if ( argCount == 2 ) {
136+
final long start = (Long) args[0];
137+
if ( start < 1 ) {
138+
throw new SQLException( "Start position 1-based; must be 1 or more." );
139+
}
140+
if ( start > getLength() + 1 ) {
141+
throw new SQLException( "Start position [" + start + "] cannot exceed overall CLOB length [" + getLength() + "]" );
142+
}
143+
final int length = (Integer) args[1];
144+
if ( length < 0 ) {
145+
throw new SQLException( "Length must be great-than-or-equal to zero." );
146+
}
147+
return getSubString( start-1, length );
148+
}
149+
break;
150+
case "free":
151+
if ( argCount == 0 ) {
152+
characterStream.release();
153+
return null;
154+
}
155+
break;
156+
case "toString":
157+
if ( argCount == 0 ) {
158+
return this.toString();
159+
}
160+
break;
161+
case "equals":
162+
if ( argCount == 1 ) {
163+
return proxy == args[0];
164+
}
165+
break;
166+
case "hashCode":
167+
if ( argCount == 0 ) {
168+
return this.hashCode();
169+
}
170+
break;
157171
}
158-
159172
throw new UnsupportedOperationException( "Clob may not be manipulated from creating session" );
160173
}
161174

0 commit comments

Comments
 (0)