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