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.FloatCodec; 030 031/** 032 * @author Franck WOLFF 033 */ 034public class FloatCodecImpl extends AbstractStandardCodec<Float> implements FloatCodec { 035 036 public int getObjectType() { 037 return JMF_FLOAT_OBJECT; 038 } 039 040 public Class<?> getObjectClass() { 041 return Float.class; 042 } 043 044 public int getPrimitiveType() { 045 return JMF_FLOAT; 046 } 047 048 public Class<?> getPrimitiveClass() { 049 return Float.TYPE; 050 } 051 052 public void encode(OutputContext ctx, Float v) throws IOException { 053 writeFloatData(ctx, JMF_FLOAT_OBJECT, v.floatValue()); 054 } 055 056 public Float decode(InputContext ctx, int parameterizedJmfType) throws IOException { 057 int jmfType = ctx.getSharedContext().getCodecRegistry().extractJmfType(parameterizedJmfType); 058 059 if (jmfType != JMF_FLOAT_OBJECT) 060 throw newBadTypeJMFEncodingException(jmfType, parameterizedJmfType); 061 062 return Float.valueOf(readFloatData(ctx, parameterizedJmfType)); 063 } 064 065 public void encodePrimitive(OutputContext ctx, float v) throws IOException { 066 writeFloatData(ctx, JMF_FLOAT, v); 067 } 068 069 public float decodePrimitive(InputContext ctx) throws IOException { 070 int parameterizedJmfType = ctx.safeRead(); 071 int jmfType = ctx.getSharedContext().getCodecRegistry().extractJmfType(parameterizedJmfType); 072 073 if (jmfType != JMF_FLOAT) 074 throw newBadTypeJMFEncodingException(jmfType, parameterizedJmfType); 075 076 return readFloatData(ctx, parameterizedJmfType); 077 } 078 079 public void dump(DumpContext ctx, int parameterizedJmfType) throws IOException { 080 int jmfType = ctx.getSharedContext().getCodecRegistry().extractJmfType(parameterizedJmfType); 081 082 switch (jmfType) { 083 case JMF_FLOAT: 084 ctx.indentPrintLn("float: " + readFloatData(ctx, parameterizedJmfType)); 085 break; 086 case JMF_FLOAT_OBJECT: 087 ctx.indentPrintLn(Float.class.getName() + ": " + Float.valueOf(readFloatData(ctx, parameterizedJmfType))); 088 break; 089 default: 090 throw newBadTypeJMFEncodingException(jmfType, parameterizedJmfType); 091 } 092 } 093 094 public static void writeFloatData(OutputContext ctx, int jmfType, float v) throws IOException { 095 int bits = Float.floatToIntBits(v); 096 097 final OutputStream os = ctx.getOutputStream(); 098 099 os.write(jmfType); 100 101 os.write(bits); 102 os.write(bits >> 8); 103 os.write(bits >> 16); 104 os.write(bits >> 24); 105 } 106 107 public static float readFloatData(InputContext ctx, int type) throws IOException { 108 int i = ctx.safeRead(); 109 110 i |= ctx.safeRead() << 8; 111 i |= ctx.safeRead() << 16; 112 i |= ctx.safeRead() << 24; 113 114 return Float.intBitsToFloat(i); 115 } 116}