/*********************************************************************
* SojoAdapter.java
* created on 11.11.2006 by netseeker
* $Id: SojoAdapter.java,v 1.3 2007/05/27 22:13:09 netseeker Exp $
*
* ====================================================================
*
* 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.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import net.sf.sojo.interchange.object.ObjectSerializer;
import de.netseeker.ejoe.EJConstants;
import de.netseeker.ejoe.io.FastBufferedOutputStream;
import de.netseeker.ejoe.io.IOUtil;
/**
* An adapter for (de)serializing objects via SOJO - Simplify your Old Java Objects
*
* @see <a href="http://sojo.sourceforge.net/">SOJO</a>
* @author netseeker
* @since 0.3.9.1
*/
public class SojoAdapter extends BaseAdapter
{
/**
*
*/
private static final long serialVersionUID = 1L;
/**
*
*/
public SojoAdapter()
{
}
/*
* (non-Javadoc)
*
* @see de.netseeker.ejoe.adapter.SerializeAdapter#read(java.io.InputStream)
*/
public Object read( InputStream in ) throws Exception
{
ObjectSerializer deSerializer = new ObjectSerializer();
// return deSerializer.deserializeFromInputStream( new BufferedInputStream( in ));
byte[] buffer = new byte[EJConstants.BUFFERED_STREAM_SIZE];
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
int length = 0;
while ( (length = in.read( buffer )) != -1 )
{
if ( length > 0 ) bOut.write( buffer, 0, length );
}
ByteArrayInputStream bIn = new ByteArrayInputStream( bOut.toByteArray() );
Object obj = null;
try
{
obj = deSerializer.deserializeFromInputStream( bIn );
}
finally
{
IOUtil.closeQuiet( bIn );
}
return obj;
}
/*
* (non-Javadoc)
*
* @see de.netseeker.ejoe.adapter.SerializeAdapter#write(java.lang.Object, java.io.OutputStream)
*/
public void write( Object obj, OutputStream out ) throws Exception
{
ObjectSerializer serializer = new ObjectSerializer();
FastBufferedOutputStream bOut = new FastBufferedOutputStream( out );
try
{
serializer.serializeToOutputStream( obj, bOut );
}
finally
{
IOUtil.closeQuiet( bOut );
}
}
/*
* (non-Javadoc)
*
* @see de.netseeker.ejoe.adapter.BaseAdapter#isSelfBuffered()
*/
public boolean isSelfBuffered()
{
return true;
}
/*
* (non-Javadoc)
*
* @see de.netseeker.ejoe.adapter.BaseAdapter#requiresCustomEOFHandling()
*/
public boolean requiresCustomEOFHandling()
{
return true;
}
}