aboutsummaryrefslogtreecommitdiffstats
path: root/filedistribution/src/main/java/com/yahoo/vespa/filedistribution/EmptyFileReferenceData.java
blob: ea8461b42f32071fe09b8410fe398c56ac1c4cab (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
// 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;

public class EmptyFileReferenceData extends FileReferenceData {

    private final byte[] content;
    private final long xxhash;
    private int contentRead = 0;

    private EmptyFileReferenceData(FileReference fileReference, String filename, Type type, byte[] content, long xxhash) {
        super(fileReference, filename, type, CompressionType.gzip);
        this.content = content;
        this.xxhash = xxhash;
    }

    public static FileReferenceData empty(FileReference fileReference, String filename) {
        return new EmptyFileReferenceData(fileReference, filename, FileReferenceData.Type.file, new byte[0], 0);
    }

    public ByteBuffer content() {
        return ByteBuffer.wrap(content);
    }

    @Override
    public int nextContent(ByteBuffer bb) {
        if (contentRead >= content.length) {
            return -1;
        } else {
            int left = content.length - contentRead;
            int size = Math.min(bb.remaining(), left);
            bb.put(content, contentRead, size);
            contentRead += size;
            return size;
        }
    }

    @Override
    public long xxhash() {
        return xxhash;
    }

    @Override
    public long size() {
        return content.length;
    }

    @Override
    public void close() {
        // no-op
    }
}