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    
021    package org.granite.messaging.jmf.codec.std.impl;
022    
023    import java.io.IOException;
024    import java.io.OutputStream;
025    import java.math.BigDecimal;
026    import java.math.BigInteger;
027    
028    import org.granite.messaging.jmf.DumpContext;
029    import org.granite.messaging.jmf.InputContext;
030    import org.granite.messaging.jmf.OutputContext;
031    import org.granite.messaging.jmf.codec.std.BigDecimalCodec;
032    
033    /**
034     * @author Franck WOLFF
035     */
036    public 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 scale = v.scale();
050                    byte[] magnitude = v.unscaledValue().toByteArray();
051    
052                    IntegerComponents ics = intComponents(magnitude.length);
053                    os.write((ics.length << 6) | JMF_BIG_DECIMAL);
054                    writeIntData(ctx, ics);
055                    
056                    os.write(scale);
057                    os.write(magnitude);
058            }
059    
060            public BigDecimal decode(InputContext ctx, int parameterizedJmfType) throws IOException {
061                    int jmfType = ctx.getSharedContext().getCodecRegistry().extractJmfType(parameterizedJmfType);
062                    
063                    if (jmfType != JMF_BIG_DECIMAL)
064                            throw newBadTypeJMFEncodingException(jmfType, parameterizedJmfType);
065                    
066                    int magnitudeLength = readIntData(ctx, (parameterizedJmfType >> 6) & 0x03, false);
067                    int scale = ctx.safeRead();
068    
069                    byte[] magnitude = new byte[magnitudeLength];
070                    ctx.safeReadFully(magnitude);
071                    
072                    BigDecimal v = new BigDecimal(new BigInteger(magnitude), scale);
073                    
074                    if (BigDecimal.ZERO.equals(v))
075                            v = BigDecimal.ZERO;
076                    else if (BigDecimal.ONE.equals(v))
077                            v = BigDecimal.ONE;
078                    else if (BigDecimal.TEN.equals(v))
079                            v = BigDecimal.TEN;
080                    
081                    return v;
082            }
083    
084            public void dump(DumpContext ctx, int parameterizedJmfType) throws IOException {
085                    int jmfType = ctx.getSharedContext().getCodecRegistry().extractJmfType(parameterizedJmfType);
086                    if (jmfType != JMF_BIG_DECIMAL)
087                            throw newBadTypeJMFEncodingException(jmfType, parameterizedJmfType);
088                    ctx.indentPrintLn(BigDecimal.class.getName() + ": " + decode(ctx, parameterizedJmfType));
089            }
090    }