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

org.xsocket.connection.http.ConnectionTerminatedBodyDataSource Maven / Gradle / Ivy

There is a newer version: 2.0-beta-1
Show newest version
/*
 *  Copyright (c) xsocket.org, 2006 - 2008. All rights reserved.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library 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 GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Please refer to the LGPL license at: http://www.gnu.org/copyleft/lesser.txt
 * The latest copy of this software may be found on http://www.xsocket.org/
 */
package org.xsocket.connection.http;

import java.io.IOException;
import java.nio.BufferUnderflowException;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.xsocket.connection.IDataHandler;
import org.xsocket.connection.IDisconnectHandler;
import org.xsocket.connection.INonBlockingConnection;
import org.xsocket.connection.http.AbstractHttpMessage.BodyType;




/**
 * 
 * plain implementation of a body data source
 * 
 * 
 * @author [email protected]
 */
class ConnectionTerminatedBodyDataSource extends NonBlockingBodyDataSource implements IDataHandler, IDisconnectHandler {

	
	private static final Logger LOG = Logger.getLogger(ConnectionTerminatedBodyDataSource.class.getName());
	
	
	/**
	 * constructor
	 * 
	 * @param httpConnection   the http connection
	 * @param header           the message header
	 * @param encoding         the encoding to use
	 */
	public ConnectionTerminatedBodyDataSource(AbstractHttpConnection httpConnection, AbstractMessageHeader header, String encoding) throws IOException {
		super(encoding, httpConnection);
	}

	
	/**
	 * {@inheritDoc}
	 */
	@Override
	BodyType getBodyType() {
		return BodyType.CONNECTION_TERMINATED;
	}
	
	
	/**
	 * {@inheritDoc}
	 */
	public boolean onData(INonBlockingConnection connection) throws IOException, BufferUnderflowException {
		if (LOG.isLoggable(Level.FINE)) {
			LOG.fine("[" + connection.getId() + "] reading received data into body");
		}

		try {
			
			int available = connection.available();
			
			if (available > 0) {
				append(connection.readByteBufferByLength(available));
				
			} else if (available == -1) {
				getHttpConnection().removeBodyParser();
				setComplete(true);
			}
			
		} catch (BufferUnderflowException bue) {
			throw bue;

		} catch (IOException ioe) {
			if (LOG.isLoggable(Level.FINE)) {
				LOG.fine("error occured by reading plain body " + ioe.toString());
			}
			
			getHttpConnection().destroy();
			setComplete(true);
			return true;
		}
		

		return true;
	}
	
	
	public boolean onDisconnect(INonBlockingConnection connection) throws IOException {
		
		setComplete(true);
		onUnderlyingHttpConnectionClosed();
		return true;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy