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

se.l4.commons.serialization.collections.array.ShortArraySerializer Maven / Gradle / Ivy

package se.l4.commons.serialization.collections.array;

import java.io.IOException;
import java.util.Arrays;

import se.l4.commons.serialization.Serializer;
import se.l4.commons.serialization.collections.ArraySerializer;
import se.l4.commons.serialization.format.StreamingInput;
import se.l4.commons.serialization.format.StreamingOutput;
import se.l4.commons.serialization.format.Token;

/**
 * Custom serializer for arrays of shorts.
 */
public class ShortArraySerializer
	implements Serializer
{

	@Override
	public short[] read(StreamingInput in)
		throws IOException
	{
		in.next(Token.LIST_START);

		int length = 0;
		short[] current = new short[512];
		while(in.peek() != Token.LIST_END)
		{
			in.next(Token.VALUE);

			if(length == current.length)
			{
				int newSize = ArraySerializer.growArray(current.length);
				current = Arrays.copyOf(current, newSize);
			}

			current[length++] = in.getShort();
		}

		in.next(Token.LIST_END);
		return Arrays.copyOf(current, length);
	}

	@Override
	public void write(short[] object, String name, StreamingOutput out)
		throws IOException
	{
		out.writeListStart(name);
		for(short v : object)
		{
			out.write("item", v);
		}
		out.writeListEnd(name);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy