diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2017-12-08 18:52:27 +0100 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2017-12-08 18:52:27 +0100 |
commit | f83c2be974ce171d7d2d32fdf642f5b8bf69fcfa (patch) | |
tree | 2c8978fa67e755e2f6200c74c6a994e705aad5e9 /filedistribution/src | |
parent | 6bffc191b8689e0dac623d0db90790719a896634 (diff) |
Use a ByteChannel interface instead.
Diffstat (limited to 'filedistribution/src')
4 files changed, 21 insertions, 24 deletions
diff --git a/filedistribution/src/main/java/com/yahoo/vespa/filedistribution/FileReceiver.java b/filedistribution/src/main/java/com/yahoo/vespa/filedistribution/FileReceiver.java index aa2f0310754..d783ecf97ea 100644 --- a/filedistribution/src/main/java/com/yahoo/vespa/filedistribution/FileReceiver.java +++ b/filedistribution/src/main/java/com/yahoo/vespa/filedistribution/FileReceiver.java @@ -197,7 +197,7 @@ public class FileReceiver { } void receiveFile(FileReferenceData fileReferenceData) { - long xxHashFromContent = hasher.hash(ByteBuffer.wrap(fileReferenceData.content()), 0); + long xxHashFromContent = fileReferenceData.xxhash(); if (xxHashFromContent != fileReferenceData.xxhash()) { throw new RuntimeException("xxhash from content (" + xxHashFromContent + ") is not equal to xxhash in request (" + fileReferenceData.xxhash() + ")"); } @@ -207,7 +207,7 @@ public class FileReceiver { File file = new File(fileReferenceDir, fileReferenceData.filename()); try { File tempFile = new File(Files.createTempDirectory("downloaded").toFile(), fileReferenceData.filename()); - Files.write(tempFile.toPath(), fileReferenceData.content()); + Files.write(tempFile.toPath(), fileReferenceData.content().array()); // Unpack if necessary if (fileReferenceData.type() == FileReferenceData.Type.compressed) { diff --git a/filedistribution/src/main/java/com/yahoo/vespa/filedistribution/FileReferenceData.java b/filedistribution/src/main/java/com/yahoo/vespa/filedistribution/FileReferenceData.java index 171a3d7fb39..dabdba2bfc0 100644 --- a/filedistribution/src/main/java/com/yahoo/vespa/filedistribution/FileReferenceData.java +++ b/filedistribution/src/main/java/com/yahoo/vespa/filedistribution/FileReferenceData.java @@ -37,20 +37,20 @@ public abstract class FileReferenceData { return type; } - public byte [] content() { + public ByteBuffer content() { ByteBuffer bb = ByteBuffer.allocate((int)size()); - for (byte [] part = nextContent(0); part != null && part.length > 0; part = nextContent(0)) { - bb.put(part); + while (bb.remaining() > 0) { + nextContent(bb); } - return bb.array(); + return bb; } /** * Will provide the next part of the content. * - * @param desiredSize of the part - * @return The next part of the content. Empty when done. + * @param bb with some available space + * @return Number of bytes transferred. */ - public abstract byte[] nextContent(int desiredSize); + public abstract int nextContent(ByteBuffer bb); /** * Only guaranteed to be valid after all content has been consumed. diff --git a/filedistribution/src/main/java/com/yahoo/vespa/filedistribution/FileReferenceDataBlob.java b/filedistribution/src/main/java/com/yahoo/vespa/filedistribution/FileReferenceDataBlob.java index f31f9941d71..3759cbe2ef7 100644 --- a/filedistribution/src/main/java/com/yahoo/vespa/filedistribution/FileReferenceDataBlob.java +++ b/filedistribution/src/main/java/com/yahoo/vespa/filedistribution/FileReferenceDataBlob.java @@ -23,12 +23,13 @@ public class FileReferenceDataBlob extends FileReferenceData { return new FileReferenceDataBlob(fileReference, filename, FileReferenceData.Type.file, new byte[0], 0); } - public byte [] content() { - return content; + public ByteBuffer content() { + return ByteBuffer.wrap(content); } @Override - public byte[] nextContent(int desiredSize) { - return content; + public int nextContent(ByteBuffer bb) { + bb.put(content); + return content.length; } @Override diff --git a/filedistribution/src/main/java/com/yahoo/vespa/filedistribution/LazyFileReferenceData.java b/filedistribution/src/main/java/com/yahoo/vespa/filedistribution/LazyFileReferenceData.java index 1de72eaefd2..5e7fd08e52f 100644 --- a/filedistribution/src/main/java/com/yahoo/vespa/filedistribution/LazyFileReferenceData.java +++ b/filedistribution/src/main/java/com/yahoo/vespa/filedistribution/LazyFileReferenceData.java @@ -22,20 +22,16 @@ public class LazyFileReferenceData extends FileReferenceData { } @Override - public byte[] nextContent(int desiredSize) { - ByteBuffer bb = ByteBuffer.allocate(Math.min(desiredSize, 0x100000)); + public int nextContent(ByteBuffer bb) { + int read = 0; + int pos = bb.position(); try { - channel.read(bb); + read = channel.read(bb); } catch (IOException e) { - return null; + return -1; } - byte [] retval = bb.array(); - if (bb.position() != bb.array().length) { - retval = new byte [bb.position()]; - bb.get(retval); - } - hasher.update(retval, 0, retval.length); - return retval; + hasher.update(bb.array(), pos, read); + return read; } @Override |