Skip to content

Commit

Permalink
Added ability to tune offset of ByteChunkProvider (Fixes #22)
Browse files Browse the repository at this point in the history
  • Loading branch information
hierynomus committed Nov 7, 2016
1 parent 2332cea commit 4ccd73f
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 5 deletions.
33 changes: 33 additions & 0 deletions .gitlab-ci.yml
@@ -0,0 +1,33 @@
image: java:8-jdk

stages:
- build
- test
- deploy

before_script:
# - echo `pwd` # debug
# - echo "$CI_BUILD_NAME, $CI_BUILD_REF_NAME $CI_BUILD_STAGE" # debug
- export GRADLE_USER_HOME=`pwd`/.gradle

cache:
paths:
- .gradle/wrapper
- .gradle/caches

build:
stage: build
script:
- ./gradlew assemble
artifacts:
paths:
- build/libs/*.jar
expire_in: 1 week
only:
- master

test:
stage: test
script:
- ./gradlew check

11 changes: 6 additions & 5 deletions src/main/java/com/hierynomus/smbj/io/ByteChunkProvider.java
Expand Up @@ -22,14 +22,15 @@
import java.io.OutputStream;

public abstract class ByteChunkProvider {
static final int CHUNK_SIZE = 64 * 1024;
protected static final int CHUNK_SIZE = 64 * 1024;

private long offset;
protected long offset;
protected int chunkSize = CHUNK_SIZE;

public abstract boolean isAvailable();

public void writeChunk(OutputStream os) {
byte[] chunk = new byte[CHUNK_SIZE];
byte[] chunk = new byte[chunkSize];
try {
int size = getChunk(chunk);
os.write(chunk, 0, size);
Expand All @@ -40,7 +41,7 @@ public void writeChunk(OutputStream os) {
}

public void writeChunks(Buffer<?> buffer, int nrChunks) {
byte[] chunk = new byte[CHUNK_SIZE];
byte[] chunk = new byte[chunkSize];
for (int i = 0; i < nrChunks; i++) {
try {
int size = getChunk(chunk);
Expand All @@ -53,7 +54,7 @@ public void writeChunks(Buffer<?> buffer, int nrChunks) {
}

public void writeChunk(Buffer<?> buffer) {
byte[] chunk = new byte[CHUNK_SIZE];
byte[] chunk = new byte[chunkSize];
try {
int size = getChunk(chunk);
buffer.putRawBytes(chunk, 0, size);
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/com/hierynomus/smbj/io/FileByteChunkProvider.java
Expand Up @@ -29,6 +29,24 @@ public FileByteChunkProvider(File file) throws FileNotFoundException {
fis = new BufferedInputStream(new FileInputStream(file), CHUNK_SIZE);
}

public FileByteChunkProvider(File file, long offset) throws IOException {
this.file = file;
fis = new BufferedInputStream(new FileInputStream(file), CHUNK_SIZE);
ensureSkipped(fis, offset);
this.offset = offset;
}

private void ensureSkipped(final BufferedInputStream fis, final long offset) throws IOException {
long skipped = 0;
while (skipped < offset && fis.available() > 0) {
skipped += fis.skip(offset);
}

if (skipped < offset) {
throw new IOException("Was unable to go to the requested offset of " + offset + " of file " + file);
}
}

@Override
protected int getChunk(byte[] chunk) throws IOException {
int count = 0;
Expand Down
Expand Up @@ -67,6 +67,24 @@ class FileByteChunkProviderTest extends Specification {
provider.isAvailable()
}

def "should start at provided offset"() {
given:
def file = getFileWithRandomData(ByteChunkProvider.CHUNK_SIZE)
def provider = new FileByteChunkProvider(file, 100)
def baos = new ByteArrayOutputStream()

when:
provider.writeChunk(baos)

then:
def tmpBytes = new byte[ByteChunkProvider.CHUNK_SIZE - 100]
System.arraycopy(file.bytes, 100, tmpBytes, 0, tmpBytes.length)
baos.toByteArray() == tmpBytes
provider.offset == ByteChunkProvider.CHUNK_SIZE
!provider.isAvailable()

}

private def getFileWithRandomData(int size) {
def bytes = new byte[size]
new Random().nextBytes(bytes)
Expand Down

0 comments on commit 4ccd73f

Please sign in to comment.