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;
025import java.math.BigDecimal;
026import java.math.BigInteger;
027
028import org.granite.messaging.jmf.DumpContext;
029import org.granite.messaging.jmf.InputContext;
030import org.granite.messaging.jmf.OutputContext;
031import org.granite.messaging.jmf.codec.std.BigDecimalCodec;
032
033/**
034 * @author Franck WOLFF
035 */
036public class BigDecimalCodecImpl extends AbstractIntegerStringCodec<BigDecimal> implements BigDecimalCodec {
037
038        public int getObjectType() {
039                return JMF_BIG_DECIMAL;
040        }
041
042        public Class<?> getObjectClass() {
043                return BigDecimal.class;
044        }
045
046        public void encode(OutputContext ctx, BigDecimal v) throws IOException {
047                final OutputStream os = ctx.getOutputStream();
048                
049                int indexOfStoredObject = ctx.indexOfStoredObjects(v);
050                if (indexOfStoredObject >= 0) {
051                        IntegerComponents ics = intComponents(indexOfStoredObject);
052                        os.write(0x80 | (ics.length << 5) | JMF_BIG_DECIMAL);
053                        writeIntData(ctx, ics);
054                }
055                else {
056                        ctx.addToStoredObjects(v);
057                        
058                        int scale = v.scale();
059                        byte[] magnitude = v.unscaledValue().toByteArray();
060
061                        IntegerComponents ics = intComponents(magnitude.length);
062                        os.write((ics.length << 5) | JMF_BIG_DECIMAL);
063                        writeIntData(ctx, ics);
064                        
065                        os.write(scale);
066                        os.write(magnitude);
067                }
068        }
069
070        public BigDecimal decode(InputContext ctx, int parameterizedJmfType) throws IOException {
071                int jmfType = ctx.getSharedContext().getCodecRegistry().extractJmfType(parameterizedJmfType);
072                
073                if (jmfType != JMF_BIG_DECIMAL)
074                        throw newBadTypeJMFEncodingException(jmfType, parameterizedJmfType);
075                
076                int indexOrLength = readIntData(ctx, (parameterizedJmfType >> 5) & 0x03, false);
077                if ((parameterizedJmfType & 0x80) != 0)
078                        return (BigDecimal)ctx.getSharedObject(indexOrLength);
079
080                int scale = ctx.safeRead();
081                
082                byte[] magnitude = new byte[indexOrLength];
083                ctx.safeReadFully(magnitude);
084                
085                BigDecimal v = new BigDecimal(new BigInteger(magnitude), scale);
086                
087                if (BigDecimal.ZERO.equals(v))
088                        v = BigDecimal.ZERO;
089                else if (BigDecimal.ONE.equals(v))
090                        v = BigDecimal.ONE;
091                else if (BigDecimal.TEN.equals(v))
092                        v = BigDecimal.TEN;
093                
094                return v;
095        }
096
097        public void dump(DumpContext ctx, int parameterizedJmfType) throws IOException {
098                int jmfType = ctx.getSharedContext().getCodecRegistry().extractJmfType(parameterizedJmfType);
099                if (jmfType != JMF_BIG_DECIMAL)
100                        throw newBadTypeJMFEncodingException(jmfType, parameterizedJmfType);
101                ctx.indentPrintLn(BigDecimal.class.getName() + ": " + decode(ctx, parameterizedJmfType));
102        }
103}