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

se.l4.commons.serialization.collections.array.CharArraySerializer 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 chars.
 */
public class CharArraySerializer
	implements Serializer
{

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

		int length = 0;
		char[] current = new char[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.getChar();
		}

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

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

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy