Skip to content

Commit

Permalink
Merge pull request brianfrankcooper#999 from manolama/iterators_reset
Browse files Browse the repository at this point in the history
[core] Add a reset() method to the ByteIterator abstract and implemen…
  • Loading branch information
manolama authored Aug 5, 2017
2 parents eaff913 + a56a00e commit 169150b
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 0 deletions.
8 changes: 8 additions & 0 deletions core/src/main/java/com/yahoo/ycsb/ByteArrayByteIterator.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* A ByteIterator that iterates through a byte array.
*/
public class ByteArrayByteIterator extends ByteIterator {
private final int originalOffset;
private byte[] str;
private int off;
private final int len;
Expand All @@ -28,12 +29,14 @@ public ByteArrayByteIterator(byte[] s) {
this.str = s;
this.off = 0;
this.len = s.length;
originalOffset = 0;
}

public ByteArrayByteIterator(byte[] s, int off, int len) {
this.str = s;
this.off = off;
this.len = off + len;
originalOffset = off;
}

@Override
Expand All @@ -53,4 +56,9 @@ public long bytesLeft() {
return len - off;
}

@Override
public void reset() {
off = originalOffset;
}

}
9 changes: 9 additions & 0 deletions core/src/main/java/com/yahoo/ycsb/ByteIterator.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ public void remove() {
throw new UnsupportedOperationException();
}

/** Resets the iterator so that it can be consumed again. Not all
* implementations support this call.
* @throws UnsupportedOperationException if the implementation hasn't implemented
* the method.
*/
public void reset() {
throw new UnsupportedOperationException();
}

/** Consumes remaining contents of this object, and returns them as a string. */
public String toString() {
Charset cset = Charset.forName("UTF-8");
Expand Down
19 changes: 19 additions & 0 deletions core/src/main/java/com/yahoo/ycsb/InputStreamByteIterator.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package com.yahoo.ycsb;

import java.io.IOException;
import java.io.InputStream;

/**
Expand All @@ -25,11 +26,16 @@ public class InputStreamByteIterator extends ByteIterator {
private long len;
private InputStream ins;
private long off;
private final boolean resetable;

public InputStreamByteIterator(InputStream ins, long len) {
this.len = len;
this.ins = ins;
off = 0;
resetable = ins.markSupported();
if (resetable) {
ins.mark((int) len);
}
}

@Override
Expand Down Expand Up @@ -57,4 +63,17 @@ public long bytesLeft() {
return len - off;
}

@Override
public void reset() {
if (resetable) {
try {
ins.reset();
ins.mark((int) len);
} catch (IOException e) {
throw new IllegalStateException("Failed to reset the input stream", e);
}
}
throw new UnsupportedOperationException();
}

}
6 changes: 6 additions & 0 deletions core/src/main/java/com/yahoo/ycsb/RandomByteIterator.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,10 @@ public int nextBuf(byte[] buffer, int bufOffset) {
public long bytesLeft() {
return len - off - bufOff;
}

@Override
public void reset() {
off = 0;
}

}
5 changes: 5 additions & 0 deletions core/src/main/java/com/yahoo/ycsb/StringByteIterator.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ public long bytesLeft() {
return str.length() - off;
}

@Override
public void reset() {
off = 0;
}

/**
* Specialization of general purpose toString() to avoid unnecessary
* copies.
Expand Down

0 comments on commit 169150b

Please sign in to comment.