package org.glassfish.grizzly.utils;
import org.glassfish.grizzly.attributes.AttributeStorage;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import org.glassfish.grizzly.AbstractTransformer;
import org.glassfish.grizzly.Buffer;
import org.glassfish.grizzly.Grizzly;
import org.glassfish.grizzly.TransformationException;
import org.glassfish.grizzly.TransformationResult;
import org.glassfish.grizzly.attributes.Attribute;
import java.util.logging.Level;
import java.util.logging.Logger;
public class StringDecoder extends AbstractTransformer<Buffer, String> {
private static final Logger logger = Grizzly.logger(StringDecoder.class);
protected Charset charset;
protected final Attribute<Integer> lengthAttribute;
protected byte[] stringTerminateBytes;
@SuppressWarnings("unused")
public StringDecoder() {
this(null, null);
}
@SuppressWarnings("unused")
public StringDecoder(final String stringTerminator) {
this(Charset.forName("UTF-8"), stringTerminator);
}
public StringDecoder(final Charset charset) {
this(charset, null);
}
public StringDecoder(final Charset charset, final String stringTerminator) {
this.charset = charset != null ? charset : Charset.defaultCharset();
if (stringTerminator != null) {
try {
this.stringTerminateBytes = stringTerminator.getBytes(
this.charset.name());
} catch (final UnsupportedEncodingException ignored) {
}
}
lengthAttribute = attributeBuilder.createAttribute(
"StringDecoder.StringSize");
}
@Override
public String getName() {
return "StringDecoder";
}
@Override
protected TransformationResult<Buffer, String> transformImpl(
final AttributeStorage storage, final Buffer input)
throws TransformationException {
if (input == null) {
throw new TransformationException("Input could not be null");
}
final TransformationResult<Buffer, String> result;
result = stringTerminateBytes == null
? parseWithLengthPrefix(storage, input)
: parseWithTerminatingSeq(storage, input);
return result;
}
protected TransformationResult<Buffer, String> parseWithLengthPrefix(
final AttributeStorage storage, final Buffer input) {
Integer stringSize = lengthAttribute.get(storage);
if (logger.isLoggable(Level.FINE)) {
logger.log(Level.FINE, "StringDecoder decode stringSize={0} buffer={1} content={2}",
new Object[]{stringSize, input, input.toStringContent()});
}
if (stringSize == null) {
if (input.remaining() < 4) {
return TransformationResult.createIncompletedResult(input);
}
stringSize = input.getInt();
lengthAttribute.set(storage, stringSize);
}
if (input.remaining() < stringSize) {
return TransformationResult.createIncompletedResult(input);
}
final int tmpLimit = input.limit();
input.limit(input.position() + stringSize);
final String stringMessage = input.toStringContent(charset);
input.position(input.limit());
input.limit(tmpLimit);
return TransformationResult.createCompletedResult(
stringMessage, input);
}
protected TransformationResult<Buffer, String> parseWithTerminatingSeq(
final AttributeStorage storage, final Buffer input) {
final int terminationBytesLength = stringTerminateBytes.length;
int checkIndex = 0;
int termIndex = -1;
final Integer offsetInt = lengthAttribute.get(storage);
int offset = 0;
if (offsetInt != null) {
offset = offsetInt;
}
for (int i = input.position() + offset, lim = input.limit(); i < lim; i++) {
if (input.get(i) == stringTerminateBytes[checkIndex]) {
checkIndex++;
if (checkIndex >= terminationBytesLength) {
termIndex = i - terminationBytesLength + 1;
break;
}
}
}
if (termIndex >= 0) {
final int tmpLimit = input.limit();
input.limit(termIndex);
final String stringMessage = input.toStringContent(charset);
input.limit(tmpLimit);
input.position(termIndex + terminationBytesLength);
return TransformationResult.createCompletedResult(
stringMessage, input);
} else {
offset = input.remaining() - terminationBytesLength;
if (offset < 0) {
offset = 0;
}
lengthAttribute.set(storage, offset);
return TransformationResult.createIncompletedResult(
input);
}
}
@Override
public void release(final AttributeStorage storage) {
lengthAttribute.remove(storage);
super.release(storage);
}
@Override
public boolean hasInputRemaining(final AttributeStorage storage,
final Buffer input) {
return input != null && input.hasRemaining();
}
public Charset getCharset() {
return charset;
}
public void setCharset(final Charset charset) {
this.charset = charset;
}
}