Mina TextEncoder

package com.prosodie.ws.tuxedo.net.codec;

import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.AttributeKey;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolEncoderAdapter;
import org.apache.mina.filter.codec.ProtocolEncoderOutput;

public class TextEncoder extends ProtocolEncoderAdapter
{

private final AttributeKey ENCODER = new AttributeKey(getClass(), "encoder");

private final Charset charset;


public TextEncoder(Charset charset)
{
if (charset == null)
{
throw new IllegalArgumentException("charset parameter should not be null");
}


this.charset = charset;

}

public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception
{
CharsetEncoder encoder = (CharsetEncoder) session.getAttribute(ENCODER);

if (encoder == null)
{
encoder = charset.newEncoder();
session.setAttribute(ENCODER, encoder);
}

String value = (message == null ? "" : message.toString());
IoBuffer buffer = IoBuffer.allocate(value.getBytes().length).setAutoExpand(true);
buffer.putString(value, encoder);


buffer.flip();
out.write(buffer);
}

}



package com.prosodie.ws.tuxedo.net.codec;

import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.AttributeKey;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolDecoderAdapter;
import org.apache.mina.filter.codec.ProtocolDecoderOutput;

public class TextDecoder extends ProtocolDecoderAdapter
{


private final AttributeKey DECODER = new AttributeKey(getClass(), "decoder");

private final Charset charset;


public TextDecoder(Charset charset)
{
if (charset == null)
{
throw new IllegalArgumentException("charset parameter should not be null");
}

this.charset = charset;


}


@Override
public void decode(IoSession session, IoBuffer in, ProtocolDecoderOutput out)
throws Exception
{

CharsetDecoder decoder = (CharsetDecoder) session.getAttribute(DECODER);

if (decoder == null)
{
decoder = charset.newDecoder();
session.setAttribute(DECODER, decoder);
}


if (in != null)
{
out.write(in.getString(decoder));
}


}


}



package com.prosodie.ws.tuxedo.net.codec;

import java.nio.charset.Charset;

import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFactory;
import org.apache.mina.filter.codec.ProtocolDecoder;
import org.apache.mina.filter.codec.ProtocolEncoder;


public class TextCodecFactory implements ProtocolCodecFactory
{

private final TextEncoder encoder;

private final TextDecoder decoder;


public TextCodecFactory(Charset charset)
{
encoder = new TextEncoder(charset);
decoder = new TextDecoder(charset);
}


public ProtocolEncoder getEncoder(IoSession session)
{
return encoder;
}

public ProtocolDecoder getDecoder(IoSession session)
{
return decoder;
}
}


你可能感兴趣的:(java)