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