Skip to content

Commit

Permalink
(#28) Added getCharacterStream of substring
Browse files Browse the repository at this point in the history
  • Loading branch information
svettwer committed Mar 22, 2019
1 parent 5ff47a7 commit 4c8a5f8
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,21 @@ public void free() {

@Override
public Reader getCharacterStream(final long pos, final long length) throws SQLException {
if(pos < 1){
throw new SQLException("position is < 1 but CLOB starts at 1.");
}

if(pos + length > stringBuilder.length()){
throw new SQLException("CLOB references exceeds actual CLOB size");
}

final long posWithOffset = applyOffset(pos);
if(fitsInInt(posWithOffset + length)){
final int intPos = (int) posWithOffset;
final String substring = stringBuilder.substring(intPos, intPos + (int)length);
return new StringReader(substring);
}

return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.nio.charset.Charset;
import java.sql.Clob;
Expand Down Expand Up @@ -299,6 +300,41 @@ public void testFree() throws SQLException {
assertEquals(citrusClob.length(), 0);
}

@Test
public void testGetCharacterStream() throws Exception {

//GIVEN
citrusClob.setString(1, sampleText);
final String expectedStreamContent = "calm and";

//WHEN
final Reader characterStream = citrusClob.getCharacterStream(6, 8);

//THEN
final String streamContent = IOUtils.toString(characterStream);
assertEquals(streamContent, expectedStreamContent);
}

@Test(expectedExceptions = SQLException.class)
public void testGetCharacterStreamThrowsExceptionOnInvalidPosition() throws SQLException {

//WHEN
citrusClob.getCharacterStream(0, 5);

//THEN
//ExceptionIsThrown
}

@Test(expectedExceptions = SQLException.class)
public void testGetCharacterStreamThrowsExceptionWhenReferenceExceedsClobSize() throws SQLException {

//WHEN
citrusClob.getCharacterStream(5, 42);

//THEN
//ExceptionIsThrown
}

@Test
public void testEqualsContract(){
EqualsVerifier
Expand Down

0 comments on commit 4c8a5f8

Please sign in to comment.