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

org.xsocket.connection.http.PlainBodyDataSource 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.nio.ByteBuffer;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.xsocket.Execution;
import org.xsocket.MaxReadSizeExceededException;
import org.xsocket.Execution.Mode;
import org.xsocket.connection.IDataHandler;
import org.xsocket.connection.INonBlockingConnection;




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

	
	private static final Logger LOG = Logger.getLogger(PlainBodyDataSource.class.getName());
	
	private int remaining = 0;
	private AbstractHttpConnection httpConnection = null;
	
	
	
	/**
	 * constructor
	 * 
	 * @param httpConnection   the http connection
	 * @param header           the message header
	 * @param encoding         the encoding to use
	 */
	public PlainBodyDataSource(AbstractHttpConnection httpConnection, AbstractMessageHeader header, String encoding) {
		super(encoding);
		setComplete(false);
		
		this.httpConnection = httpConnection;
		this.remaining = header.getContentLength();
	}

	
	/**
	 * {@inheritDoc}
	 */
	@Override
	void doCallBodyHandler(HandlerCaller handlerCaller, Mode handlerExecutionMode) {
		if (handlerExecutionMode == Execution.Mode.NONTHREADED) {
			httpConnection.processNonThreaded(handlerCaller);
		} else {
			httpConnection.processMultiThreaded(handlerCaller);
		}
	}

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

		try {
			int available = connection.available();
			
			// not enough data
			if (available < remaining) {
				ByteBuffer[] data = connection.readByteBufferByLength(available);
				append(data);
				remaining -= available;
				
				
			// enough data 
			} else {
				ByteBuffer[] data = connection.readByteBufferByLength(remaining);
				append(data);
				remaining = 0;
			} 
			
			if (remaining == 0) {
				if (LOG.isLoggable(Level.FINE)) {
					LOG.fine("[" + connection.getId() + "] body received.");
				}
				
				httpConnection.removeBodyParser();
				setComplete(true);
				
			} else {
				if (LOG.isLoggable(Level.FINE)) {
					LOG.fine("[" + connection.getId() + "] body data read. wating for more data");
				}

			}

		} catch (MaxReadSizeExceededException mre) {
			throw mre;

			
		} catch (BufferUnderflowException bue) {
			throw bue;

		} catch (IOException ioe) {
			ioe.printStackTrace();
			throw ioe;
		}
		

		return true;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy