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