Skip to content

Commit

Permalink
(#28) Added getBytes, setBytes and length implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
svettwer committed Apr 11, 2019
1 parent 9703a43 commit 74ca876
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,22 @@

public class CitrusBlob implements Blob {

private byte[] content = new byte[0];
private byte[] content = ArrayUtils.EMPTY_BYTE_ARRAY;
private final LobUtils lobUtils = new LobUtils();

@Override
public long length() {
return 0;
return content.length;
}

@Override
public byte[] getBytes(final long pos, final int length) {
return new byte[0];
final long positionWithOffset = lobUtils.applyOffset(pos);
if(lobUtils.fitsInInt(positionWithOffset)){
final int offsetPositionInInt = (int) positionWithOffset;
return ArrayUtils.subarray(content, offsetPositionInInt, offsetPositionInInt + length);
}
return new byte[length];
}

@Override
Expand All @@ -42,14 +47,19 @@ public long position(final Blob pattern, final long start) {
public int setBytes(final long pos, final byte[] bytes) {
final long positionWithOffset = lobUtils.applyOffset(pos);
if(lobUtils.fitsInInt(positionWithOffset)){
return setContent(content, (int)positionWithOffset, bytes, 0, bytes.length);
return setContent((int)positionWithOffset, bytes, 0, bytes.length);
}

return 0;
}

@Override
public int setBytes(final long pos, final byte[] bytes, final int offset, final int len) {
final long positionWithOffset = lobUtils.applyOffset(pos);
if(lobUtils.fitsInInt(positionWithOffset)){
return setContent((int)positionWithOffset, bytes, offset, len);
}

return 0;
}

Expand Down Expand Up @@ -95,25 +105,26 @@ public String toString() {
* Alters the content of this @{@link CitrusBlob} to contain the given bytes.
* If the size of the altered bytes exceeds the current capacity, the capacity is automatically extended to the
* required size.
* @param byteContent The byte content to alter
* @param position The start position for altering the content. Starts at 0.
* @param bytesToSet The bytes to set the content from.
* @param offset the index of the first character of {@code bytesToSet} to be inserted. Starting at 0.
* @param length The number of bytes to set.
* @return The number of the bytes that have been set
*/
private int setContent(final byte[] byteContent,
final int position,
private int setContent(final int position,
final byte[] bytesToSet,
final int offset,
final int length) {
final boolean expandsCurrentContent = position + length > byteContent.length;
final boolean expandsCurrentContent = position + length > content.length;
final byte[] limitedBytesToSet = ArrayUtils.subarray(bytesToSet, offset, offset+length);
final byte[] bytesBeforeAddingContent = Arrays.copyOfRange(content, 0, position);

if(expandsCurrentContent){
final byte[] untouchedContent = Arrays.copyOfRange(content,0, position);
content = ArrayUtils.addAll(untouchedContent, bytesToSet);
if(expandsCurrentContent) {
content = ArrayUtils.addAll(bytesBeforeAddingContent, limitedBytesToSet);
}else{
content = ArrayUtils.insert(position, content, bytesToSet);
final byte[] bytesAfterReplacement = Arrays.copyOfRange(content, position + length, content.length);
final byte[] bytesIncludingReplacement = ArrayUtils.addAll(bytesBeforeAddingContent, limitedBytesToSet);
content = ArrayUtils.addAll(bytesIncludingReplacement, bytesAfterReplacement);
}

return length;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@

public class CitrusBlobTest {

private final CitrusBlob citrusBlob = new CitrusBlob();

private final byte[] sampleBytes = "Beam Me Up, Scotty".getBytes();

@Test
public void testEqualsContract(){
EqualsVerifier
Expand All @@ -23,15 +27,59 @@ public void testEqualsContract(){
public void testToString(){

//GIVEN
final byte[] expectedClobContent = "Beam Me Up, Scotty".getBytes();
final CitrusBlob citrusBlob = new CitrusBlob();
citrusBlob.setBytes(1, expectedClobContent);
citrusBlob.setBytes(1, sampleBytes);

//WHEN
final String clobContent = citrusBlob.toString();

//THEN
assertEquals(clobContent, Arrays.toString(expectedClobContent));
assertEquals(clobContent, Arrays.toString(sampleBytes));
}

@Test
public void testGetBytes() {

//GIVEN
citrusBlob.setBytes(1, sampleBytes);
final byte[] expectedBlobContent = "Me Up".getBytes();

//WHEN
final byte[] blobContent = citrusBlob.getBytes(6, 5);

//THEN
final String blobString = new String(blobContent);
assertEquals(blobString, new String(expectedBlobContent));
}

@Test
public void testLength() {

//GIVEN
citrusBlob.setBytes(1, sampleBytes);

//WHEN
final long length = citrusBlob.length();
//THEN
assertEquals(length, sampleBytes.length);
}

@Test
public void testSetStringWithOffsetAndLengthReplacingContent() {

//GIVEN
citrusBlob.setBytes(1, sampleBytes);

final byte[] bytesToAdd = "pause".getBytes();

final byte[] expectedBlobContent = "Beam us Up, Scotty".getBytes();

//WHEN
final long writtenBytes = citrusBlob.setBytes(6, bytesToAdd, 2, 2);

//THEN
final String blobString = new String(citrusBlob.getBytes(1, (int)citrusBlob.length()));
assertEquals(blobString, new String(expectedBlobContent));
assertEquals(writtenBytes, 2);
}

}

0 comments on commit 74ca876

Please sign in to comment.