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.util.logging.Level;
import java.util.logging.Logger;
import org.xsocket.ClosedException;
import org.xsocket.connection.INonBlockingConnection;
import org.xsocket.connection.IConnection.FlushMode;
/**
*
* 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 boolean isCloseHttpConnectionAfterWritten = false;
/**
* constructor
*
* @param httpConnection the http connection
* @param header the message header
* @param encoding the encoding to use
*/
ChunkedBodyDataSink(AbstractHttpConnection httpConnection, AbstractMessageHeader header, String encoding) {
this.httpConnection = httpConnection;
this.tcpConnection = httpConnection.getUnderlyingConnection();
this.header = header;
setEncoding(encoding);
}
/**
* {@inheritDoc}
*/
void setCloseHttpConnectionAfterWritten(boolean isCloseHttpConnectionAfterWritten) {
this.isCloseHttpConnectionAfterWritten = isCloseHttpConnectionAfterWritten;
}
/**
* {@inheritDoc}
*/
@Override
protected void onWriteDataInserted() throws IOException, ClosedException {
if (isAutoflush()) {
flush();
}
}
private void writeChunk() throws IOException {
removeWriteMark();
if (!isHeaderWritten) {
isHeaderWritten = true;
if (LOG.isLoggable(Level.FINE)) {
LOG.fine("[" + httpConnection.getId() + "] sending " + header.toString());
}
header.writeTo(tcpConnection);
}
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() + "] writeing chunk (size=" + length + ")");
}
// write length field
tcpConnection.write(Integer.toHexString(length));
tcpConnection.write("\r\n");
// write data
tcpConnection.write(body);
tcpConnection.write("\r\n");
}
}
/**
* {@inheritDoc}
*/
public void flush() throws IOException {
writeChunk();
try {
tcpConnection.setFlushmode(this.getFlushmode());
tcpConnection.flush();
} finally {
tcpConnection.setFlushmode(FlushMode.ASYNC);
}
}
/**
* {@inheritDoc}
*/
public void close() throws IOException {
writeChunk();
// write length field
tcpConnection.write("0");
tcpConnection.write("\r\n");
tcpConnection.write("\r\n");
tcpConnection.setFlushmode(this.getFlushmode());
tcpConnection.flush();
tcpConnection.setFlushmode(FlushMode.ASYNC);
super.close();
if (isCloseHttpConnectionAfterWritten) {
httpConnection.close();
}
}
/**
* {@inheritDoc}
*/
public String toString() {
return printWriteBuffer(getEncoding());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy