
org.glassfish.grizzly.streams.TransformerInput Maven / Gradle / Ivy
The newest version!
/*
* Copyright (c) 2010, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.grizzly.streams;
import java.io.IOException;
import org.glassfish.grizzly.Buffer;
import org.glassfish.grizzly.Connection;
import org.glassfish.grizzly.EmptyCompletionHandler;
import org.glassfish.grizzly.Grizzly;
import org.glassfish.grizzly.TransformationException;
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.memory.CompositeBuffer;
import org.glassfish.grizzly.memory.MemoryManager;
import org.glassfish.grizzly.utils.conditions.Condition;
/**
*
* @author Alexey Stashok
*/
public final class TransformerInput extends BufferedInput {
private final Attribute inputBufferAttr;
protected final Transformer transformer;
protected final Input underlyingInput;
protected final MemoryManager memoryManager;
protected final AttributeStorage attributeStorage;
public TransformerInput(Transformer transformer, Input underlyingInput, Connection connection) {
this(transformer, underlyingInput, connection.getMemoryManager(), connection);
}
public TransformerInput(Transformer transformer, Input underlyingInput, MemoryManager memoryManager, AttributeStorage attributeStorage) {
this.transformer = transformer;
this.underlyingInput = underlyingInput;
this.memoryManager = memoryManager;
this.attributeStorage = attributeStorage;
inputBufferAttr = Grizzly.DEFAULT_ATTRIBUTE_BUILDER.createAttribute("TransformerInput-" + transformer.getName());
}
@Override
protected void onOpenInputSource() throws IOException {
underlyingInput.notifyCondition(new TransformerCondition(), new TransformerCompletionHandler());
}
@Override
protected void onCloseInputSource() throws IOException {
}
public final class TransformerCompletionHandler extends EmptyCompletionHandler {
@Override
public void failed(Throwable throwable) {
notifyFailure(completionHandler, throwable);
future.failure(throwable);
}
}
public final class TransformerCondition implements Condition {
@Override
public boolean check() {
try {
CompositeBuffer savedBuffer = inputBufferAttr.get(attributeStorage);
Buffer bufferToTransform = savedBuffer;
Buffer chunkBuffer;
final boolean hasSavedBuffer = savedBuffer != null;
if (underlyingInput.isBuffered()) {
chunkBuffer = underlyingInput.takeBuffer();
} else {
int size = underlyingInput.size();
chunkBuffer = memoryManager.allocate(size);
while (size-- >= 0) {
chunkBuffer.put(underlyingInput.read());
}
chunkBuffer.flip();
}
if (hasSavedBuffer) {
savedBuffer.append(chunkBuffer);
} else {
bufferToTransform = chunkBuffer;
}
while (bufferToTransform.hasRemaining()) {
final TransformationResult result = transformer.transform(attributeStorage, bufferToTransform);
final Status status = result.getStatus();
if (status == Status.COMPLETE) {
final Buffer outputBuffer = result.getMessage();
lock.writeLock().lock();
try {
append(outputBuffer);
if (!isCompletionHandlerRegistered) {
// if !isCompletionHandlerRegistered - it means StreamReader has enough data to continue processing
return true;
}
} finally {
lock.writeLock().unlock();
}
} else if (status == Status.INCOMPLETE) {
if (!hasSavedBuffer) {
if (bufferToTransform.isComposite()) {
inputBufferAttr.set(attributeStorage, (CompositeBuffer) bufferToTransform);
} else {
savedBuffer = CompositeBuffer.newBuffer(memoryManager);
savedBuffer.append(bufferToTransform);
inputBufferAttr.set(attributeStorage, savedBuffer);
}
}
return false;
} else if (status == Status.ERROR) {
throw new TransformationException(result.getErrorDescription());
}
}
return false;
} catch (IOException e) {
throw new TransformationException(e);
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy