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.INonBlockingConnection;
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 AbstractHttpConnection httpConnection = null;
private INonBlockingConnection tcpConnection = null;
private AbstractMessageHeader header = null;
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);
this.httpConnection = httpConnection;
this.tcpConnection = httpConnection.getUnderlyingConnection();
this.header = header;
setEncoding(encoding);
}
@Override
BodyType getBodyType() {
return BodyType.CHUNKED;
}
@Override
protected int getWriteTransferChunkeSize() {
try {
return (Integer) tcpConnection.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;
if (LOG.isLoggable(Level.FINE)) {
LOG.fine("[" + httpConnection.getId() + "] sending header ");
}
written += header.writeTo(tcpConnection);
}
// 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("[" + httpConnection.getId() + "] writing chunk (size=" + length + ")");
}
// write length field
written += tcpConnection.write(Integer.toHexString(length));
written += tcpConnection.write("\r\n");
// write data
written += tcpConnection.write(body);
written += tcpConnection.write("\r\n");
writtenData += length;
}
return written;
}
/**
* {@inheritDoc}
*/
public void flush() throws IOException {
int written = writeChunk();
if (written > 0) {
try {
tcpConnection.setFlushmode(this.getFlushmode());
tcpConnection.flush();
} finally {
tcpConnection.setFlushmode(FlushMode.ASYNC);
}
}
}
@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
tcpConnection.write("0");
tcpConnection.write("\r\n");
tcpConnection.write("\r\n");
// ... and flush it
tcpConnection.setFlushmode(this.getFlushmode());
tcpConnection.flush();
tcpConnection.setFlushmode(FlushMode.ASYNC);
}
/**
* {@inheritDoc}
*/
public String toString() {
return printWriteBuffer(getEncoding());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy