aboutsummaryrefslogtreecommitdiffstats
path: root/predicate-search/src/main/java/com/yahoo/search/predicate/serialization/SerializationHelper.java
blob: b7e1f5322898fa94fe5cbb5057be56441f5864c2 (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
74
75
76
77
78
79
80
81
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.predicate.serialization;

import com.yahoo.search.predicate.PredicateIndex;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

/**
 * Misc utility functions to help serialization of {@link PredicateIndex}.
 *
 * @author bjorncs
 */
public class SerializationHelper {

    public static void writeIntArray(int[] array, DataOutputStream out) throws IOException {
        out.writeInt(array.length);
        for (int v : array) {
            out.writeInt(v);
        }
    }

    public static int[] readIntArray(DataInputStream in) throws IOException {
        int length = in.readInt();
        int[] array = new int[length];
        for (int i = 0; i < length; i++) {
            array[i] = in.readInt();
        }
        return array;
    }

    public static void writeByteArray(byte[] array, DataOutputStream out) throws IOException {
        out.writeInt(array.length);
        for (int v : array) {
            out.writeByte(v);
        }
    }

    public static byte[] readByteArray(DataInputStream in) throws IOException {
        int length = in.readInt();
        byte[] array = new byte[length];
        for (int i = 0; i < length; i++) {
            array[i] = in.readByte();
        }
        return array;
    }

    public static void writeLongArray(long[] array, DataOutputStream out) throws IOException {
        out.writeInt(array.length);
        for (long v : array) {
            out.writeLong(v);
        }
    }

    public static long[] readLongArray(DataInputStream in) throws IOException {
        int length = in.readInt();
        long[] array = new long[length];
        for (int i = 0; i < length; i++) {
            array[i] = in.readLong();
        }
        return array;
    }

    public static void writeShortArray(short[] array, DataOutputStream out) throws IOException {
        out.writeInt(array.length);
        for (short v : array) {
            out.writeShort(v);
        }
    }

    public static short[] readShortArray(DataInputStream in) throws IOException {
        int length = in.readInt();
        short[] array = new short[length];
        for (int i = 0; i < length; i++) {
            array[i] = in.readShort();
        }
        return array;
    }

}