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

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

/*
 *  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.ByteBuffer;
import java.nio.channels.ClosedChannelException;
import java.util.logging.Level;
import java.util.logging.Logger;


import org.xsocket.connection.IConnection;
import org.xsocket.connection.IConnection.FlushMode;
import org.xsocket.connection.http.AbstractHttpMessage.BodyType;




/**
 * 
 * Chunked implementation of a body data sink
 * 
 * 
 * @author [email protected]
 */
final class ChunkedBodyDataSink extends BodyDataSink {
		
	private static final Logger LOG = Logger.getLogger(ChunkedBodyDataSink.class.getName());
	
	private boolean isHeaderWritten = false;

	private int writtenData = 0;
	
	
	/**
	 * constructor
	 * 
	 * @param httpConnection   the http connection
	 * @param header           the message header
	 * @param encoding         the encoding to use
	 */
	ChunkedBodyDataSink(AbstractHttpConnection httpConnection, AbstractMessageHeader header, String encoding) {
		super(httpConnection, header);

		setEncoding(encoding);
	}
	
	@Override
	BodyType getBodyType() {
		return BodyType.CHUNKED;
	}
	

	@Override
	protected int getWriteTransferChunkeSize() {
		try {
			return (Integer) getHttpConnection().getOption(IConnection.SO_SNDBUF);
		} catch (IOException ignore) { }
		
		return super.getWriteTransferChunkeSize();
	}
	
	

	
	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void onWriteDataInserted() throws IOException, ClosedChannelException {
		if (isAutoflush()) {
			flush();
		}
	}
	
	
	
	private int writeChunk() throws IOException {
		int written = 0;
		
		removeWriteMark();
		
		
		// write header (if not already written)
		if (!isHeaderWritten) {
			isHeaderWritten = true;
			
			onStartWriting();
			
			if (LOG.isLoggable(Level.FINE)) {
				LOG.fine("[" + getHttpConnection().getId() + "] sending header ");
			}
			
			written += getHttpConnection().writeHeader(getHeader());
		}

		
		
		// write body (if content size larger than 0)
		ByteBuffer[] body = drainWriteQueue();
		int length = 0;
		
		if (body != null) {
			for (ByteBuffer buffer : body) {
				length += buffer.remaining();
			}
		}
		
		if (length > 0) {
			if (LOG.isLoggable(Level.FINE)) {
				LOG.fine("[" + getHttpConnection().getId() + "] writing chunk (size=" + length + ")");
			}
			// write length field
			written += getHttpConnection().write(Integer.toHexString(length) +
                                                 "\r\n");
			
			// write data 
			written += getHttpConnection().write(body);
			written += getHttpConnection().write("\r\n");
			
			writtenData += length;
		}
		
		return written;
	}

	
	/**
	 * {@inheritDoc}
	 */
	public void flush() throws IOException {
		
		
		int written = writeChunk();
		
		if (written > 0) {
			getHttpConnection().flush();
		}
	}
	
	
	@Override
	void finalizeBody() throws IOException {
		
		
		// write pending chunk
		writeChunk();

		if (LOG.isLoggable(Level.FINE)) {
			LOG.fine("[" + getHttpConnection().getId()+ "] closing chunked body by writing termination chunk (body size=" + writtenData + ")");
		}

		// write termination chunk
		getHttpConnection().write("0" + 
				                  "\r\n" +
								  "\r\n");

		// ... and flush it 
		getHttpConnection().flush();
	}


	/**
	 * {@inheritDoc}
	 */
	public String toString() {
		return printWriteBuffer(getEncoding());
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy