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     */
022    package org.granite.messaging.jmf.codec.std.impl;
023    
024    import java.io.IOException;
025    import java.io.OutputStream;
026    
027    import org.granite.messaging.jmf.DumpContext;
028    import org.granite.messaging.jmf.InputContext;
029    import org.granite.messaging.jmf.OutputContext;
030    import org.granite.messaging.jmf.codec.std.CharacterCodec;
031    
032    /**
033     * @author Franck WOLFF
034     */
035    public 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 Character.TYPE;
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                    int jmfType = ctx.getSharedContext().getCodecRegistry().extractJmfType(parameterizedJmfType);
059                    if (jmfType != JMF_CHARACTER_OBJECT)
060                            throw newBadTypeJMFEncodingException(jmfType, parameterizedJmfType);
061                    return Character.valueOf(readCharData(ctx, parameterizedJmfType));
062            }
063    
064            public void encodePrimitive(OutputContext ctx, int v) throws IOException {
065                    writeCharData(ctx, JMF_CHARACTER, v);
066            }
067            
068            public char decodePrimitive(InputContext ctx) throws IOException {
069                    int parameterizedJmfType = ctx.safeRead();
070                    int jmfType = ctx.getSharedContext().getCodecRegistry().extractJmfType(parameterizedJmfType);
071                    
072                    if (jmfType != JMF_CHARACTER)
073                            throw newBadTypeJMFEncodingException(jmfType, parameterizedJmfType);
074                    
075                    return readCharData(ctx, parameterizedJmfType);
076            }
077            
078            public void dump(DumpContext ctx, int parameterizedJmfType) throws IOException {
079                    int jmfType = ctx.getSharedContext().getCodecRegistry().extractJmfType(parameterizedJmfType);
080                    
081                    switch (jmfType) {
082                    case JMF_CHARACTER:
083                            ctx.indentPrintLn("char: '" + escape(readCharData(ctx, parameterizedJmfType)) + "'");
084                            break;
085                    case JMF_CHARACTER_OBJECT:
086                            ctx.indentPrintLn(Character.class.getName() + ": '" + escape(readCharData(ctx, parameterizedJmfType)) + "'");
087                            break;
088                    default:
089                            throw newBadTypeJMFEncodingException(jmfType, parameterizedJmfType);
090                    }
091            }
092            
093            protected void writeCharData(OutputContext ctx, int jmfType, int v) throws IOException {
094                    final OutputStream os = ctx.getOutputStream();
095                    
096                    if (v <= 0x00FF) {
097                            os.write(jmfType);
098                            os.write(v);
099                    }
100                    else {  
101                            os.write(0x80 | jmfType);
102                            os.write(v >> 8);
103                            os.write(v);
104                    }
105            }
106            
107            protected char readCharData(InputContext ctx, int parameterizedJmfType) throws IOException {
108                    char v = (char)ctx.safeRead();
109                    
110                    if ((parameterizedJmfType & 0x80) != 0)
111                            v = (char)((v << 8) | ctx.safeRead());
112                    
113                    return v;
114            }
115    }