com.brightsparklabs.asanti.decoder.builtin.Utf8StringDecoder Maven / Gradle / Ivy
/*
* Maintained by brightSPARK Labs.
* www.brightsparklabs.com
*
* Refer to LICENSE at repository root for license details.
*/
package com.brightsparklabs.asanti.decoder.builtin;
import com.brightsparklabs.asanti.common.DecodeExceptions;
import com.brightsparklabs.asanti.exception.DecodeException;
import com.brightsparklabs.asanti.schema.AsnBuiltinType;
import com.brightsparklabs.asanti.validator.AsnByteValidator;
import com.brightsparklabs.asanti.validator.failure.ByteValidationFailure;
import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableSet;
/**
* Decoder for data of type {@link AsnBuiltinType#Utf8String}
*
* @author brightSPARK Labs
*/
public class Utf8StringDecoder extends AbstractBuiltinTypeDecoder {
// -------------------------------------------------------------------------
// INSTANCE VARIABLES
// -------------------------------------------------------------------------
/** singleton instance */
private static Utf8StringDecoder instance;
// -------------------------------------------------------------------------
// CONSTRUCTION
// -------------------------------------------------------------------------
/**
* Default constructor.
*
* This is private, use {@link #getInstance()} to obtain an instance
*/
private Utf8StringDecoder() {}
/**
* Returns a singleton instance of this class
*
* @return a singleton instance of this class
*/
public static Utf8StringDecoder getInstance() {
if (instance == null) {
instance = new Utf8StringDecoder();
}
return instance;
}
// -------------------------------------------------------------------------
// IMPLEMENTATION: AbstractBuiltinTypeDecoder
// -------------------------------------------------------------------------
@Override
public String decode(final byte[] bytes) throws DecodeException {
final ImmutableSet failures =
AsnByteValidator.validateAsUtf8String(bytes);
DecodeExceptions.throwIfHasFailures(failures);
return new String(bytes, Charsets.UTF_8);
}
}