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