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; 026 027import org.granite.messaging.jmf.DumpContext; 028import org.granite.messaging.jmf.InputContext; 029import org.granite.messaging.jmf.OutputContext; 030import org.granite.messaging.jmf.codec.std.IntegerCodec; 031import org.granite.messaging.jmf.codec.std.impl.util.IntegerUtil; 032 033/** 034 * @author Franck WOLFF 035 */ 036public class IntegerCodecImpl extends AbstractStandardCodec<Integer> implements IntegerCodec { 037 038 protected static final int LENGTH_BYTE_COUNT_OFFSET = 5; 039 040 public int getObjectType() { 041 return JMF_INTEGER_OBJECT; 042 } 043 044 public Class<?> getObjectClass() { 045 return Integer.class; 046 } 047 048 public int getPrimitiveType() { 049 return JMF_INTEGER; 050 } 051 052 public Class<?> getPrimitiveClass() { 053 return int.class; 054 } 055 056 public void encode(OutputContext ctx, Integer v) throws IOException { 057 writeIntData(ctx, JMF_INTEGER_OBJECT, v.intValue()); 058 } 059 060 public Integer decode(InputContext ctx, int parameterizedJmfType) throws IOException { 061 return Integer.valueOf(readIntData(ctx, parameterizedJmfType)); 062 } 063 064 public void encodePrimitive(OutputContext ctx, int v) throws IOException { 065 writeIntData(ctx, JMF_INTEGER, v); 066 } 067 068 public int decodePrimitive(InputContext ctx) throws IOException { 069 int parameterizedJmfType = ctx.safeRead(); 070 return readIntData(ctx, parameterizedJmfType); 071 } 072 073 public void dump(DumpContext ctx, int parameterizedJmfType) throws IOException { 074 int jmfType = ctx.getSharedContext().getCodecRegistry().extractJmfType(parameterizedJmfType); 075 076 switch (jmfType) { 077 case JMF_INTEGER: 078 ctx.indentPrintLn("int: " + readIntData(ctx, parameterizedJmfType)); 079 break; 080 case JMF_INTEGER_OBJECT: 081 ctx.indentPrintLn(Integer.class.getName() + ": " + Integer.valueOf(readIntData(ctx, parameterizedJmfType))); 082 break; 083 default: 084 throw newBadTypeJMFEncodingException(jmfType, parameterizedJmfType); 085 } 086 } 087 088 protected void writeIntData(OutputContext ctx, int jmfType, int v) throws IOException { 089 final OutputStream os = ctx.getOutputStream(); 090 091 int opposite = 0x00; 092 if (v < 0 && v != Integer.MIN_VALUE) { 093 opposite = 0x80; 094 v = -v; 095 } 096 097 int count = IntegerUtil.significantIntegerBytesCount0(v); 098 os.write(opposite | (count << LENGTH_BYTE_COUNT_OFFSET) | jmfType); 099 IntegerUtil.encodeInteger(ctx, v, count); 100 } 101 102 protected int readIntData(InputContext ctx, int parameterizedJmfType) throws IOException { 103 int v = IntegerUtil.decodeInteger(ctx, (parameterizedJmfType >>> LENGTH_BYTE_COUNT_OFFSET) & 0x03); 104 if ((parameterizedJmfType & 0x80) != 0) 105 v = -v; 106 return v; 107 } 108}