Skip to content

Commit

Permalink
(#28) Added setCharacterStream
Browse files Browse the repository at this point in the history
  • Loading branch information
svettwer committed Mar 11, 2019
1 parent a5bf371 commit 917dd0f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public int setString(final long pos, final String str) {
public int setString(final long pos, final String str, final int offset, final int len) {
final long positionWithOffset = applyOffset(pos);
if(fitsInInt(positionWithOffset)){
return setContent((int) positionWithOffset, str, offset, len);
return setContent(stringBuilder, (int) positionWithOffset, str, offset, len);
}else {
return 0;
}
Expand All @@ -96,8 +96,26 @@ public void write(final int b) {
}

@Override
public Writer setCharacterStream(final long pos) throws SQLException {
return null;
public Writer setCharacterStream(final long pos) {
return new Writer() {

private StringBuilder buffer = new StringBuilder();

@Override
public void write(final char[] cbuf, final int off, final int len) {
setContent(buffer, off, new String(cbuf), 0, len);
}

@Override
public void flush() {
buffer = new StringBuilder();
}

@Override
public void close() {
setString(pos, buffer.toString());
}
};
}

@Override
Expand Down Expand Up @@ -147,13 +165,14 @@ private long applyOffset(final long pos) {
* Alters the {@link StringBuilder} of this @{@link CitrusClob} to contain the given string.
* If the size of the altered string exceeds the current StringBuilder size, it is automatically extended to the
* required capacity.
* @param stringBuilder The string builder to alter
* @param position The start position for altering the content. Starts at 0.
* @param stringToSet The String to set the content from.
* @param offset the index of the first character of {@code stringToSet} to be inserted. Starting at 0.
* @param length The length of the string to set.
* @return
*/
private int setContent(final int position, final String stringToSet, final int offset, final int length) {
private int setContent(final StringBuilder stringBuilder, final int position, final String stringToSet, final int offset, final int length) {
final boolean expandsString = position + length > stringBuilder.length();

if(expandsString){
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.Writer;
import java.nio.charset.Charset;
import java.sql.Clob;
import java.sql.SQLException;
Expand Down Expand Up @@ -244,6 +245,27 @@ public void testSetAsciiStream() throws Exception{
assertEquals(clobContent, expectedClobContent);
}

@Test
public void testSetCharacterStream() throws Exception{

//GIVEN
citrusClob.setString(1, sampleText);

final int position = 15;
final String textToAdd = "fire photon torpedoes";

final String expectedClobContent = "Keep calm and fire photon torpedoes";

//WHEN
final Writer writer = citrusClob.setCharacterStream(position);
writer.write(textToAdd);
writer.close();

//THEN
final String clobContent = IOUtils.toString(citrusClob.getCharacterStream());
assertEquals(clobContent, expectedClobContent);
}

@Test
public void testEqualsContract(){
final StringBuilder one = new StringBuilder();
Expand Down

0 comments on commit 917dd0f

Please sign in to comment.