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.LongCodec; 031import org.granite.messaging.jmf.codec.std.impl.util.LongUtil; 032 033/** 034 * @author Franck WOLFF 035 */ 036public class LongCodecImpl extends AbstractStandardCodec<Long> implements LongCodec { 037 038 protected static final int LENGTH_BYTE_COUNT_OFFSET = 4; 039 040 public int getObjectType() { 041 return JMF_LONG_OBJECT; 042 } 043 044 public Class<?> getObjectClass() { 045 return Long.class; 046 } 047 048 public int getPrimitiveType() { 049 return JMF_LONG; 050 } 051 052 public Class<?> getPrimitiveClass() { 053 return long.class; 054 } 055 056 public void encode(OutputContext ctx, Long v) throws IOException { 057 writeLongData(ctx, JMF_LONG_OBJECT, v.longValue()); 058 } 059 060 public Long decode(InputContext ctx, int parameterizedJmfType) throws IOException { 061 return Long.valueOf(readLongData(ctx, parameterizedJmfType)); 062 } 063 064 public void encodePrimitive(OutputContext ctx, long v) throws IOException { 065 writeLongData(ctx, JMF_LONG, v); 066 } 067 068 public long decodePrimitive(InputContext ctx) throws IOException { 069 int parameterizedJmfType = ctx.safeRead(); 070 return readLongData(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_LONG: 078 ctx.indentPrintLn("long: " + readLongData(ctx, parameterizedJmfType)); 079 break; 080 case JMF_LONG_OBJECT: 081 ctx.indentPrintLn(Long.class.getName() + ": " + Long.valueOf(readLongData(ctx, parameterizedJmfType))); 082 break; 083 default: 084 throw newBadTypeJMFEncodingException(jmfType, parameterizedJmfType); 085 } 086 } 087 088 protected void writeLongData(OutputContext ctx, int jmfType, long v) throws IOException { 089 final OutputStream os = ctx.getOutputStream(); 090 091 int opposite = 0x00; 092 if (v < 0 && v != Long.MIN_VALUE) { 093 opposite = 0x80; 094 v = -v; 095 } 096 097 int count = LongUtil.significantLongBytesCount0(v); 098 os.write(opposite | (count << LENGTH_BYTE_COUNT_OFFSET) | jmfType); 099 LongUtil.encodeLong(ctx, v, count); 100 } 101 102 protected long readLongData(InputContext ctx, int parameterizedJmfType) throws IOException { 103 long v = LongUtil.decodeLong(ctx, (parameterizedJmfType >>> LENGTH_BYTE_COUNT_OFFSET) & 0x07); 104 if ((parameterizedJmfType & 0x80) != 0) 105 v = -v; 106 return v; 107 } 108}