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