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.DoubleCodec;
030
031/**
032 * @author Franck WOLFF
033 */
034public class DoubleCodecImpl extends AbstractStandardCodec<Double> implements DoubleCodec {
035
036        public int getObjectType() {
037                return JMF_DOUBLE_OBJECT;
038        }
039
040        public Class<?> getObjectClass() {
041                return Double.class;
042        }
043
044        public int getPrimitiveType() {
045                return JMF_DOUBLE;
046        }
047
048        public Class<?> getPrimitiveClass() {
049                return Double.TYPE;
050        }
051
052        public void encode(OutputContext ctx, Double v) throws IOException {
053                writeDoubleData(ctx, JMF_DOUBLE_OBJECT, v.doubleValue());
054        }
055        
056        public Double decode(InputContext ctx, int parameterizedJmfType) throws IOException {
057                int jmfType = ctx.getSharedContext().getCodecRegistry().extractJmfType(parameterizedJmfType);
058
059                if (jmfType != JMF_DOUBLE_OBJECT)
060                        throw newBadTypeJMFEncodingException(jmfType, parameterizedJmfType);
061                
062                return Double.valueOf(readDoubleData(ctx, parameterizedJmfType));
063        }
064
065        public void encodePrimitive(OutputContext ctx, double v) throws IOException {
066                writeDoubleData(ctx, JMF_DOUBLE, v);
067        }
068        
069        public double decodePrimitive(InputContext ctx) throws IOException {
070                int parameterizedJmfType = ctx.safeRead();
071                int jmfType = ctx.getSharedContext().getCodecRegistry().extractJmfType(parameterizedJmfType);
072
073                if (jmfType != JMF_DOUBLE)
074                        throw newBadTypeJMFEncodingException(jmfType, parameterizedJmfType);
075                
076                return readDoubleData(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_DOUBLE:
084                        ctx.indentPrintLn("double: " + readDoubleData(ctx, parameterizedJmfType));
085                        break;
086                case JMF_DOUBLE_OBJECT:
087                        ctx.indentPrintLn(Double.class.getName() + ": " + Double.valueOf(readDoubleData(ctx, parameterizedJmfType)));
088                        break;
089                default:
090                        throw newBadTypeJMFEncodingException(jmfType, parameterizedJmfType);
091                }
092        }
093        
094        public static void writeDoubleData(OutputContext ctx, int jmfType, double v) throws IOException {
095                final OutputStream os = ctx.getOutputStream();
096                
097                if (v == (float)v || Double.isNaN(v)) {
098                        os.write(0x80 | jmfType);
099                        
100                        int bits = Float.floatToIntBits((float)v);
101                        os.write(bits);
102                        os.write(bits >> 8);
103                        os.write(bits >> 16);
104                        os.write(bits >> 24);
105                }
106                else {
107                        os.write(jmfType);
108
109                        long bits = Double.doubleToLongBits(v);
110                        os.write((int)bits);
111                        os.write((int)(bits >> 8));
112                        os.write((int)(bits >> 16));
113                        os.write((int)(bits >> 24));
114                        os.write((int)(bits >> 32));
115                        os.write((int)(bits >> 40));
116                        os.write((int)(bits >> 48));
117                        os.write((int)(bits >> 56));
118                }
119        }
120        
121        public static double readDoubleData(InputContext ctx, int type) throws IOException {
122                double v = 0.0;
123                
124                if ((type & 0x80) != 0) {
125                        int i = ctx.safeRead();
126                        i |= ctx.safeRead() << 8;
127                        i |= ctx.safeRead() << 16;
128                        i |= ctx.safeRead() << 24;
129                        v = Float.intBitsToFloat(i);
130                }
131                else {
132                        long l = ctx.safeRead();
133                        l |= ((long)ctx.safeRead()) << 8;
134                        l |= ((long)ctx.safeRead()) << 16;
135                        l |= ((long)ctx.safeRead()) << 24;
136                        l |= ((long)ctx.safeRead()) << 32;
137                        l |= ((long)ctx.safeRead()) << 40;
138                        l |= ((long)ctx.safeRead()) << 48;
139                        l |= ((long)ctx.safeRead()) << 56;
140                        v = Double.longBitsToDouble(l);
141                }
142                
143                return v;
144        }
145}