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.ShortCodec; 031 032/** 033 * @author Franck WOLFF 034 */ 035public class ShortCodecImpl extends AbstractStandardCodec<Short> implements ShortCodec { 036 037 public int getObjectType() { 038 return JMF_SHORT_OBJECT; 039 } 040 041 public Class<?> getObjectClass() { 042 return Short.class; 043 } 044 045 public int getPrimitiveType() { 046 return JMF_SHORT; 047 } 048 049 public Class<?> getPrimitiveClass() { 050 return short.class; 051 } 052 053 public void encode(OutputContext ctx, Short v) throws IOException { 054 writeShortData(ctx, JMF_SHORT_OBJECT, v.intValue()); 055 } 056 057 public Short decode(InputContext ctx, int parameterizedJmfType) throws IOException { 058 return Short.valueOf(readShortData(ctx, parameterizedJmfType)); 059 } 060 061 public void encodePrimitive(OutputContext ctx, int v) throws IOException { 062 writeShortData(ctx, JMF_SHORT, v); 063 } 064 065 public short decodePrimitive(InputContext ctx) throws IOException { 066 int parameterizedJmfType = ctx.safeRead(); 067 return readShortData(ctx, parameterizedJmfType); 068 } 069 070 public void dump(DumpContext ctx, int parameterizedJmfType) throws IOException { 071 int jmfType = ctx.getSharedContext().getCodecRegistry().extractJmfType(parameterizedJmfType); 072 073 switch (jmfType) { 074 case JMF_SHORT: 075 ctx.indentPrintLn("short: " + readShortData(ctx, parameterizedJmfType)); 076 break; 077 case JMF_SHORT_OBJECT: 078 ctx.indentPrintLn(Short.class.getName() + ": " + Short.valueOf(readShortData(ctx, parameterizedJmfType))); 079 break; 080 default: 081 throw newBadTypeJMFEncodingException(jmfType, parameterizedJmfType); 082 } 083 } 084 085 protected void writeShortData(OutputContext ctx, int jmfType, int v) throws IOException { 086 final OutputStream os = ctx.getOutputStream(); 087 088 if (v == Short.MIN_VALUE) { 089 os.write(0x40 | jmfType); 090 os.write(v >>> 8); 091 os.write(v); 092 } 093 else { 094 int opposite = 0x00; 095 if (v < 0) { 096 opposite = 0x80; 097 v = -v; 098 } 099 100 if (v <= 0xFF) { 101 os.write(opposite | jmfType); 102 os.write(v); 103 } 104 else { 105 os.write(opposite | 0x40 | jmfType); 106 os.write(v >>> 8); 107 os.write(v); 108 } 109 } 110 } 111 112 protected short readShortData(InputContext ctx, int parameterizedJmfType) throws IOException { 113 short v = (short)ctx.safeRead(); 114 115 if ((parameterizedJmfType & 0x40) != 0) 116 v = (short)((v << 8) | ctx.safeRead()); 117 118 if ((parameterizedJmfType & 0x80) != 0) 119 v = (short)-v; 120 121 return v; 122 } 123}