All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.permazen.encoding.ShortArrayEncoding Maven / Gradle / Ivy

The newest version!

/*
 * Copyright (C) 2015 Archie L. Cobbs. All rights reserved.
 */

package io.permazen.encoding;

import com.google.common.primitives.Shorts;

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

/**
 * {@code short[]} primitive array type. Does not support null arrays.
 */
public class ShortArrayEncoding extends IntegralArrayEncoding {

    private static final long serialVersionUID = 2001467018347663363L;

    public ShortArrayEncoding() {
       super(new ShortEncoding(null), short[].class);
    }

    @Override
    protected int getArrayLength(short[] array) {
        return array.length;
    }

    @Override
    protected Short getArrayElement(short[] array, int index) {
        return array[index];
    }

    @Override
    protected short[] createArray(List elements) {
        return Shorts.toArray(elements);
    }

    @Override
    protected void encode(short[] array, DataOutputStream output) throws IOException {
        for (short value : array)
            output.writeShort(value);
    }

    @Override
    protected short[] decode(DataInputStream input, int numBytes) throws IOException {
        final short[] array = this.checkDecodeLength(numBytes);
        for (int i = 0; i < array.length; i++)
            array[i] = input.readShort();
        return array;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy