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