001/** 002 * GRANITE DATA SERVICES 003 * Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S. 004 * 005 * This file is part of the Granite Data Services Platform. 006 * 007 * Granite Data Services is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * Granite Data Services is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 015 * General Public License for more details. 016 * 017 * You should have received a copy of the GNU Lesser General Public 018 * License along with this library; if not, write to the Free Software 019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 020 * USA, or see <http://www.gnu.org/licenses/>. 021 */ 022package org.granite.messaging.jmf.codec.std.impl; 023 024import java.io.IOException; 025import java.io.OutputStream; 026 027import org.granite.messaging.jmf.DumpContext; 028import org.granite.messaging.jmf.InputContext; 029import org.granite.messaging.jmf.OutputContext; 030import org.granite.messaging.jmf.codec.std.ByteCodec; 031 032/** 033 * @author Franck WOLFF 034 */ 035public class ByteCodecImpl extends AbstractStandardCodec<Byte> implements ByteCodec { 036 037 public int getObjectType() { 038 return JMF_BYTE_OBJECT; 039 } 040 041 public Class<?> getObjectClass() { 042 return Byte.class; 043 } 044 045 public int getPrimitiveType() { 046 return JMF_BYTE; 047 } 048 049 public Class<?> getPrimitiveClass() { 050 return byte.class; 051 } 052 053 public void encode(OutputContext ctx, Byte v) throws IOException { 054 final OutputStream os = ctx.getOutputStream(); 055 os.write(JMF_BYTE_OBJECT); 056 os.write(v.intValue()); 057 } 058 059 public Byte decode(InputContext ctx, int parameterizedJmfType) throws IOException { 060 return Byte.valueOf((byte)ctx.safeRead()); 061 } 062 063 public void encodePrimitive(OutputContext ctx, int v) throws IOException { 064 final OutputStream os = ctx.getOutputStream(); 065 os.write(JMF_BYTE); 066 os.write(v); 067 } 068 069 public byte decodePrimitive(InputContext ctx) throws IOException { 070 ctx.safeRead(); 071 return (byte)ctx.safeRead(); 072 } 073 074 public void dump(DumpContext ctx, int parameterizedJmfType) throws IOException { 075 int jmfType = ctx.getSharedContext().getCodecRegistry().extractJmfType(parameterizedJmfType); 076 077 switch (jmfType) { 078 case JMF_BYTE: 079 ctx.indentPrintLn("byte: " + (byte)ctx.safeRead()); 080 break; 081 case JMF_BYTE_OBJECT: 082 ctx.indentPrintLn(Byte.class.getName() + ": " + (byte)ctx.safeRead()); 083 break; 084 default: 085 throw newBadTypeJMFEncodingException(jmfType, parameterizedJmfType); 086 } 087 } 088}