summaryrefslogtreecommitdiffstats
path: root/filedistribution/src/main/java/com/yahoo/vespa/filedistribution/FileReferenceData.java
blob: db6c4b70b83506a9f39b1276cbc3f10409f1514a (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
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright Yahoo. 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 java.nio.ByteBuffer;


/**
 * Utility class for a file reference with data and metadata
 *
 * @author hmusum
 */
public abstract class FileReferenceData {

    public enum Type {file, compressed}

    private final FileReference fileReference;
    private final String filename;
    private final Type type;

    public FileReferenceData(FileReference fileReference, String filename, Type type) {
        this.fileReference = fileReference;
        this.filename = filename;
        this.type = type;
    }

    public FileReference fileReference() {
        return fileReference;
    }

    public String filename() {
        return filename;
    }

    public Type type() {
        return type;
    }

    public ByteBuffer content() {
        ByteBuffer bb = ByteBuffer.allocate((int)size());
        while (bb.remaining() > 0) {
            nextContent(bb);
        }
        return bb;
    }
    /**
     * Will provide the next part of the content.
     *
     * @param bb with some available space
     * @return Number of bytes transferred.
     */
    public abstract int nextContent(ByteBuffer bb);

    /**
     * Only guaranteed to be valid after all content has been consumed.
     * @return xx64hash of content
     */
    public abstract long xxhash();

    /**
     * The size of the content in bytes
     *
     * @return number of bytes
     */
    public abstract long size();

    /**
     * Close underlying files
     *
     */
    public abstract void close();
}