blob: 0bc8f3b162ae4d27517aaf55be331edc1e485727 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.filedistribution;
import com.yahoo.config.FileReference;
import net.jpountz.xxhash.StreamingXXHash64;
import net.jpountz.xxhash.XXHashFactory;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;
import java.nio.file.Files;
public class LazyFileReferenceData extends FileReferenceData {
private final File file;
private final ReadableByteChannel channel;
private final StreamingXXHash64 hasher;
public LazyFileReferenceData(FileReference fileReference, String filename, Type type, File file) throws IOException {
super(fileReference, filename, type);
this.file = file;
channel = Files.newByteChannel(file.toPath());
this.hasher = XXHashFactory.fastestInstance().newStreamingHash64(0);
}
@Override
public int nextContent(ByteBuffer bb) {
int read = 0;
int pos = bb.position();
try {
read = channel.read(bb);
} catch (IOException e) {
return -1;
}
if (read > 0) {
hasher.update(bb.array(), pos, read);
}
return read;
}
@Override
public long xxhash() {
return hasher.getValue();
}
@Override
public long size() {
try {
return Files.size(file.toPath());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public void close() {
try {
channel.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
|