* @return a {@link ExtensionByteBuffer} instance as a wrapper of original buffer with extra buffers;
* {@code null} if no extra buffers needed
* @throws IOException
*/
protected ExtensionByteBuffer applyExtensionsFlush(final ByteBuffer buffer, final int position, final int length) throws IOException {
ExtensionByteBuffer extBuffer = new ExtensionByteBuffer(getWebSocketChannel(), buffer, position);
int newLength = length;
if (extensions != null) {
for (ExtensionFunction ext : extensions) {
ext.beforeFlush(this, extBuffer, position, newLength);
if (extBuffer.getFilled() == 0) {
buffer.position(position);
newLength = 0;
} else if (extBuffer.getFilled() != newLength) {
newLength = extBuffer.getFilled();
}
}
}
buffer.flip();
if (extBuffer.hasExtra()) {
extBuffer.flipExtra();
return extBuffer;
} else {
return null;
}
}