Java tutorial
/* * 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; /** * Decodes from an incoming ByteBuffer to the expected type. * Expects incoming data to be in the form of a string of bytes. * * @author Diljot */ public class BerStringEncoder<T extends BerType<?>> extends BerTypeEncoder<T> { /** * Constructs a new BerStringEncoder with an expected type of Class<T>. * * @param expectedType The Class of the expected type. */ public BerStringEncoder(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 String data = obj.value().toString(); buf.writeByte(obj.tag().composedId()); buf.writeByte(obj.length()); buf.writeBytes(data.getBytes()); } /** * 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) { final byte[] data = buf.array(); return new String(data); } }