Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
This file is part of Niowire. Niowire is free software: you can redistribute it and/or modify it under the terms of the Lesser GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Niowire is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Lesser GNU General Public License for more details. You should have received a copy of the Lesser GNU General Public License along with Niowire. If not, see <http://www.gnu.org/licenses/>.
 
 package io.niowire.serializer;
 
 import java.util.List;
 import java.util.Map;

This class serializes new line delimited binary blobs into strings, It then calls deserializeString on the string so that this class can be overloaded by other classes which use line based object serialization. It will also call serializeString which can be used to convert data into a string form to be sent to the client.

Author(s):
Trent Houliston
 
 public class LineSerializer extends DelimitedSerializer
 {

Our charset that we are going to use
 
 	private Charset CHARSET = null;
Our encoder (note not thread safe)
 
 	private CharsetDecoder DECODER = null;
Our decoder (not not thread safe)
 
 	private CharsetEncoder ENCODER = null;
 	private Map<String, ? extends Objectconfiguration;

This method overrides from the Delimited serializer and deserializes the passed blob into a string. It then passes this to the deserializeString method to be further parsed.

Parameters:
blob the byte buffer blob which has a window where our line is
Returns:
A NioPacket containing our string
Throws:
NioInvalidDataException if the encoding we are using does not exist or it is thrown by an implementing class
 
 	{
 		//Decode the string
 		String str;
 
 		try
 		{
 			str = .decode(blob).toString();
 		}
 		{
 			throw new NioInvalidDataException(ex);
 		}
 
 		//Remove any carriage returns
 		str = str.replaceAll("\r""");
 
 		//Deserialize this
 		Object data = deserializeString(str);
 
 		//Return a new one element list
 		return Collections.singletonList(new NioPacket(.getUid(), data));
 	}

This method is used to serialize a string into a ByteBuffer by using our charset to convert it into its binary form

Parameters:
packet the objects to be serialized
Returns:
a byte buffer containing our serialized objects
Throws:
NioInvalidDataException if the data that was given is invalid
	{
		try
		{
			return .encode(CharBuffer.wrap(serializeString(packet)));
		}
		{
			throw new NioInvalidDataException(ex);
		}
	}

This loads and configures this Line serializer with its character encoding.

Parameters:
configuration the configuration to load
Throws:
java.lang.Exception there is an exception while setting up the encoders
	public void configure(Map<String, ? extends Objectconfigurationthrows Exception
	{
		this. = configuration;
		//Get our charset property
		String charset = (Stringconfiguration.get("charset");
		//Build our charset, encoder and decoders
		 = Charset.forName(charset);
	}

Gets the delimiter for the line serializer, in this case we are using \n rather then \r\n or String.format("%n") as we are not sure which they will be using. Therefor if we use \n we will get all windows and linux encoded data so long as we strip out all \r from the output.

Returns:
the bytes for a new line character in our charset
	protected byte[] getDelimiter()
	{
		return "\n".getBytes();
	}

This method is meant to be overridden by subclasses which use a line based deserializeation method. The default implementation just returns the string as the object.

Parameters:
str the string to deserialize
Returns:
the string passed in
Throws:
NioInvalidDataException if the passed in data was invalid
	{
		return str;
	}

This method is meant to be overridden by subclasses which use a line based serialization method. The default implementation just returns obj.toString()

Parameters:
obj the object to serialize
Returns:
a string representation of the object
Throws:
NioInvalidDataException if the passed in data was invalid
	{
		return obj.getData().toString();
	}

	public Map<String, ? extends ObjectgetConfiguration()
	{
	}
New to GrepCode? Check out our FAQ X