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

org.xsocket.connection.http.BoundBodyDataSource 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.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 BoundBodyDataSource extends NonBlockingBodyDataSource implements IDataHandler, IDisconnectHandler {

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

	
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	BodyType getBodyType() {
		return BodyType.BOUND;
	}
	
	
	/**
	 * {@inheritDoc}
	 */
	public boolean onData(INonBlockingConnection connection) throws IOException, BufferUnderflowException {
			
		try {
			int available = connection.available();
			
			if (available == 0) {
				return true;
			}
			

					
			// not enough data
			if (available < remaining) {
				ByteBuffer[] data = connection.readByteBufferByLength(available);
				remaining -= available;
				
				append(data);

				if (LOG.isLoggable(Level.FINE)) {
					LOG.fine("[" + connection.getId() + "] " + available + " bytes read remaining=" + remaining);
				}

				
			// enough data 
			} else {
				if (LOG.isLoggable(Level.FINE)) {
					LOG.fine("[" + connection.getId() + "] reading remaining data " + remaining);
				}
				
				ByteBuffer[] data = connection.readByteBufferByLength(remaining);
				remaining = 0;
				
				append(data);
			} 

			
			
			getHttpConnection().onBodyDataReceived();

			
			if (remaining == 0) {
				if (LOG.isLoggable(Level.FINE)) {
					LOG.fine("[" + connection.getId() + "] complete body received (size=" + size + ")");
				}
				
				removeBodyParser();
				setComplete(true);
				
			} else {
				if (LOG.isLoggable(Level.FINER)) {
					LOG.finer("[" + connection.getId() + "] body data read. wating for more data");
				}

			}
			
		} catch (BufferUnderflowException bue) {
			throw bue;

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

		return true;
	}
	
	
	public boolean onDisconnect(INonBlockingConnection connection) throws IOException {
		
		if (remaining != 0) {
			setIOException(new IOException("protocol error occured (connection is closed, but more data (" + remaining + " bytes) is expected)"));
		}
		
		super.onDisconnect(connection);
		
		
		
		onUnderlyingHttpConnectionClosed();
		return true;
	}
	
	

	
	int getSize() {
		return size;
	}
	
	int getRemaining() {
		return remaining;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy