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

net.officefloor.server.http.netty.NettyBufferPool Maven / Gradle / Ivy

There is a newer version: 3.40.0
Show newest version
/*
 * OfficeFloor - http://www.officefloor.net
 * Copyright (C) 2005-2018 Daniel Sagenschneider
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see .
 */
package net.officefloor.server.http.netty;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.http.FullHttpResponse;
import net.officefloor.server.stream.StreamBufferPool;
import net.officefloor.server.stream.FileCompleteCallback;
import net.officefloor.server.stream.StreamBuffer;

/**
 * Netty {@link StreamBufferPool}.
 * 
 * @author Daniel Sagenschneider
 */
public class NettyBufferPool extends StreamBuffer implements StreamBufferPool {

	/**
	 * {@link FullHttpResponse}.
	 */
	private final FullHttpResponse response;

	/**
	 * Instantiate.
	 * 
	 * @param response
	 *            {@link FullHttpResponse}.
	 */
	public NettyBufferPool(FullHttpResponse response) {
		super(response.content(), null, null);
		this.response = response;
	}

	/*
	 * ============== StreamBufferPool =============
	 */

	@Override
	public StreamBuffer getPooledStreamBuffer() {
		return this;
	}

	@Override
	public StreamBuffer getUnpooledStreamBuffer(ByteBuffer buffer) {
		this.response.content().writeBytes(buffer);
		return this;
	}

	@Override
	public StreamBuffer getFileStreamBuffer(FileChannel file, long position, long count,
			FileCompleteCallback callback) throws IOException {
		int length = (int) (count < 0 ? file.size() - position : count);
		this.response.content().writeBytes(file, position, length);
		if (callback != null) {
			callback.complete(file, true);
		}
		return this;
	}

	/*
	 * ================== StreamBuffer ==============
	 */

	@Override
	public boolean write(byte datum) {
		this.response.content().writeByte(datum);
		return true;
	}

	@Override
	public int write(byte[] data, int offset, int length) {
		this.response.content().writeBytes(data, offset, length);
		return length;
	}

	@Override
	public void release() {
		// Called on response reset, so clear content
		this.response.content().clear();
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy