package org.glassfish.grizzly.streams;
import java.io.IOException;
import org.glassfish.grizzly.Buffer;
import org.glassfish.grizzly.CompletionHandler;
import org.glassfish.grizzly.Connection;
import org.glassfish.grizzly.Grizzly;
import org.glassfish.grizzly.GrizzlyFuture;
import org.glassfish.grizzly.TransformationResult;
import org.glassfish.grizzly.TransformationResult.Status;
import org.glassfish.grizzly.Transformer;
import org.glassfish.grizzly.attributes.Attribute;
import org.glassfish.grizzly.attributes.AttributeStorage;
import org.glassfish.grizzly.impl.ReadyFutureImpl;
import org.glassfish.grizzly.memory.CompositeBuffer;
import org.glassfish.grizzly.memory.MemoryManager;
@SuppressWarnings("unchecked")
public class TransformerOutput extends BufferedOutput {
private final Attribute<CompositeBuffer> outputBufferAttr;
protected final Transformer<Buffer, Buffer> transformer;
protected final Output underlyingOutput;
protected final MemoryManager memoryManager;
protected final AttributeStorage attributeStorage;
public TransformerOutput(Transformer<Buffer, Buffer> transformer, Output underlyingOutput, Connection connection) {
this(transformer, underlyingOutput, connection.getMemoryManager(), connection);
}
public TransformerOutput(Transformer<Buffer, Buffer> transformer, Output underlyingOutput, MemoryManager memoryManager, AttributeStorage attributeStorage) {
this.transformer = transformer;
this.underlyingOutput = underlyingOutput;
this.memoryManager = memoryManager;
this.attributeStorage = attributeStorage;
outputBufferAttr = Grizzly.DEFAULT_ATTRIBUTE_BUILDER.createAttribute("TransformerOutput-" + transformer.getName());
}
@Override
protected GrizzlyFuture<Integer> flush0(Buffer buffer, final CompletionHandler<Integer> completionHandler) throws IOException {
if (buffer != null) {
CompositeBuffer savedBuffer = outputBufferAttr.get(attributeStorage);
if (savedBuffer != null) {
savedBuffer.append(buffer);
buffer = savedBuffer;
}
do {
final TransformationResult<Buffer, Buffer> result = transformer.transform(attributeStorage, buffer);
final Status status = result.getStatus();
if (status == Status.COMPLETE) {
final Buffer outputBuffer = result.getMessage();
underlyingOutput.write(outputBuffer);
transformer.release(attributeStorage);
} else if (status == Status.INCOMPLETE) {
buffer.compact();
if (!buffer.isComposite()) {
buffer = CompositeBuffer.newBuffer(memoryManager, buffer);
}
outputBufferAttr.set(attributeStorage, (CompositeBuffer) buffer);
return ReadyFutureImpl.create(new IllegalStateException("Can not flush data: " + "Insufficient input data for transformer"));
} else if (status == Status.ERROR) {
transformer.release(attributeStorage);
throw new IOException("Transformation exception: " + result.getErrorDescription());
}
} while (buffer.hasRemaining());
return underlyingOutput.flush(completionHandler);
}
return ZERO_READY_FUTURE;
}
@Override
protected Buffer newBuffer(int size) {
return memoryManager.allocate(size);
}
@Override
protected Buffer reallocateBuffer(Buffer oldBuffer, int size) {
return memoryManager.reallocate(oldBuffer, size);
}
@Override
protected void onClosed() throws IOException {
}
}