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.TYPE; 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 int jmfType = ctx.getSharedContext().getCodecRegistry().extractJmfType(parameterizedJmfType); 061 if (jmfType != JMF_BYTE_OBJECT) 062 throw newBadTypeJMFEncodingException(jmfType, parameterizedJmfType); 063 064 return Byte.valueOf((byte)ctx.safeRead()); 065 } 066 067 public void encodePrimitive(OutputContext ctx, int v) throws IOException { 068 final OutputStream os = ctx.getOutputStream(); 069 os.write(JMF_BYTE); 070 os.write(v); 071 } 072 073 public byte decodePrimitive(InputContext ctx) throws IOException { 074 int parameterizedJmfType = ctx.safeRead(); 075 int jmfType = ctx.getSharedContext().getCodecRegistry().extractJmfType(parameterizedJmfType); 076 077 if (jmfType != JMF_BYTE) 078 throw newBadTypeJMFEncodingException(jmfType, parameterizedJmfType); 079 080 return (byte)ctx.safeRead(); 081 } 082 083 public void dump(DumpContext ctx, int parameterizedJmfType) throws IOException { 084 int jmfType = ctx.getSharedContext().getCodecRegistry().extractJmfType(parameterizedJmfType); 085 086 switch (jmfType) { 087 case JMF_BYTE: 088 ctx.indentPrintLn("byte: " + (byte)ctx.safeRead()); 089 break; 090 case JMF_BYTE_OBJECT: 091 ctx.indentPrintLn(Byte.class.getName() + ": " + (byte)ctx.safeRead()); 092 break; 093 default: 094 throw newBadTypeJMFEncodingException(jmfType, parameterizedJmfType); 095 } 096 } 097}