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.TYPE;
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                int jmfType = ctx.getSharedContext().getCodecRegistry().extractJmfType(parameterizedJmfType);
059                
060                if (jmfType != JMF_SHORT_OBJECT)
061                        throw newBadTypeJMFEncodingException(jmfType, parameterizedJmfType);
062
063                return Short.valueOf(readShortData(ctx, parameterizedJmfType));
064        }
065
066        public void encodePrimitive(OutputContext ctx, int v) throws IOException {
067                writeShortData(ctx, JMF_SHORT, v);
068        }
069        
070        public short decodePrimitive(InputContext ctx) throws IOException {
071                int parameterizedJmfType = ctx.safeRead();
072                int jmfType = ctx.getSharedContext().getCodecRegistry().extractJmfType(parameterizedJmfType);
073                
074                if (jmfType != JMF_SHORT)
075                        throw newBadTypeJMFEncodingException(jmfType, parameterizedJmfType);
076                
077                return readShortData(ctx, parameterizedJmfType);
078        }
079        
080        public void dump(DumpContext ctx, int parameterizedJmfType) throws IOException {
081                int jmfType = ctx.getSharedContext().getCodecRegistry().extractJmfType(parameterizedJmfType);
082
083                switch (jmfType) {
084                case JMF_SHORT:
085                        ctx.indentPrintLn("short: " + readShortData(ctx, parameterizedJmfType));
086                        break;
087                case JMF_SHORT_OBJECT:
088                        ctx.indentPrintLn(Short.class.getName() + ": " + Short.valueOf(readShortData(ctx, parameterizedJmfType)));
089                        break;
090                default:
091                        throw newBadTypeJMFEncodingException(jmfType, parameterizedJmfType);
092                }
093        }
094        
095        public void writeShortData(OutputContext ctx, int jmfType, int v) throws IOException {
096                final OutputStream os = ctx.getOutputStream();
097                
098                if (v == Short.MIN_VALUE) {
099                        os.write(0x40 | jmfType);
100                        os.write(v >> 8);
101                        os.write(v);
102                }
103                else {
104                        int s = 0x00;
105                        int a = v;
106                        if (v < 0) {
107                                a = -v;
108                                s = 0x80;
109                        }
110                        
111                        if (a <= 0xFF) {
112                                os.write(s | jmfType);
113                                os.write(a);
114                        }
115                        else {
116                                os.write(s | 0x40 | jmfType);
117                                os.write(a >> 8);
118                                os.write(a);
119                        }
120                }
121        }
122        
123        public short readShortData(InputContext ctx, int parameterizedJmfType) throws IOException {
124                short v = (short)ctx.safeRead();
125                
126                if ((parameterizedJmfType & 0x40) != 0)
127                        v = (short)((v << 8) | ctx.safeRead());
128                
129                if ((parameterizedJmfType & 0x80) != 0)
130                        v = (short)-v;
131                
132                return v;
133        }
134}