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