/*********************************************************************
 * UTF8StringAdapter.java
 * created on 18.03.2005 by netseeker
 * $Source: /cvsroot/ejoe/EJOE/src/de/netseeker/ejoe/adapter/UTF8StringAdapter.java,v $
 * $Date: 2006/11/05 16:33:37 $
 * $Revision: 1.9 $
 *
 * ====================================================================
 *
 *  Copyright 2005-2006 netseeker aka Michael Manske
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * ====================================================================
 *
 * This file is part of the ejoe framework.
 * For more information on the author, please see
 * <http://www.manskes.de/>.
 *
 */
package de.netseeker.ejoe.adapter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import de.netseeker.ejoe.EJConstants;
/**
 * Simple raw string (de)Serializer for serializing/deserializing string messages. This adapter will always use UTF8
 * encoding.
 * 
 * @author netseeker
 * @since 0.3.4
 */
public class UTF8StringAdapter extends BaseAdapter
{
    private static final long serialVersionUID = 1L;
    /*
     * (non-Javadoc)
     * 
     * @see de.netseeker.ejoe.adapter.SerializeAdapter#read(java.io.InputStream)
     */
    public Object read( InputStream in ) throws Exception
    {
        Reader reader = new InputStreamReader( in, "UTF-8" );
        char[] buffer = new char[EJConstants.BUFFERED_STREAM_SIZE];
        char[] result = null;
        char[] tmp = null;
        int n = 0;
        int pos = 1;
        while ( -1 != (n = reader.read( buffer )) )
        {
            if ( result == null )
            {
                result = new char[n];
            }
            else
            {
                pos = result.length;
                tmp = new char[pos + n];
                System.arraycopy( result, 0, tmp, 0, pos - 1 );
                result = tmp;
            }
            System.arraycopy( buffer, 0, result, pos - 1, n );
        }
        reader.close();
        if ( result != null )
        {
            return new String( result );
        }
        else
        {
            return null;
        }
    }
    /*
     * (non-Javadoc)
     * 
     * @see de.netseeker.ejoe.adapter.SerializeAdapter#write(java.lang.Object, java.io.OutputStream)
     */
    public void write( Object obj, OutputStream out ) throws Exception
    {
        Writer writer = new OutputStreamWriter( out, "UTF-8" );
        writer.write( obj.toString() );
        writer.close();
    }
    /*
     * (non-Javadoc)
     * 
     * @see de.netseeker.ejoe.adapter.BaseAdapter#getContentType()
     */
    public String getContentType()
    {
        return "text/plain";
    }
}