storm.net.codec.impl.BerLongLongEncoder.java Source code

Java tutorial

Introduction

Here is the source code for storm.net.codec.impl.BerLongLongEncoder.java

Source

/*
 * Copyright (C) (2014) Diljot Randhawa <rdiljot@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package storm.net.codec.impl;

import io.netty.buffer.ByteBuf;
import storm.net.ber.BerType;
import storm.net.codec.BerEncodingContext;
import storm.net.codec.BerTypeEncoder;

import java.math.BigInteger;

/**
 * Decodes from an incoming ByteBuffer to the expected type.
 * Expects incoming data to be in the form of an unsigned long or
 * a variant unsigned long.
 *
 * @author Diljot
 */
public class BerLongLongEncoder<T extends BerType<?>> extends BerTypeEncoder<T> {

    /**
     * Constructs a new BerLongLongEncoder with an expected type of Class<T>.
     *
     * @param expectedType The Class of the expected type.
     */
    public BerLongLongEncoder(Class<T> expectedType) {
        super(expectedType);
    }

    /**
     * Encodes the data from an object to a byte buffer.
     *
     * @param ctx Encoding context for recursive encoding.
     * @param buf The buffer to which data will be written.
     * @param obj The object which is the source of the data.
     */
    @Override
    protected void encodeData(BerEncodingContext ctx, ByteBuf buf, T obj) {
        final BigInteger value = (BigInteger) obj.value();

        buf.writeBytes(value.toByteArray());
    }

    /**
     * Decodes the data from within the byte buffer.
     *
     * @param ctx Encoding context for recursive decoding.
     * @param buf The buffer which houses the source data.
     */
    @Override
    protected Object decodeData(BerEncodingContext ctx, ByteBuf buf) {

        return new BigInteger(buf.array());
    }
}