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;
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.BooleanCodec;
030
031/**
032 * @author Franck WOLFF
033 */
034public class BooleanCodecImpl extends AbstractStandardCodec<Boolean> implements BooleanCodec {
035
036        public int getObjectType() {
037                return JMF_BOOLEAN_OBJECT;
038        }
039
040        public Class<?> getObjectClass() {
041                return Boolean.class;
042        }
043
044        public int getPrimitiveType() {
045                return JMF_BOOLEAN;
046        }
047
048        public Class<?> getPrimitiveClass() {
049                return Boolean.TYPE;
050        }
051
052        public void encode(OutputContext ctx, Boolean v) throws IOException {
053                if (v.booleanValue())
054                        ctx.getOutputStream().write(0x80 | JMF_BOOLEAN_OBJECT);
055                else
056                        ctx.getOutputStream().write(JMF_BOOLEAN_OBJECT);
057        }
058        
059        public Boolean decode(InputContext ctx, int parameterizedJmfType) throws IOException {
060                int jmfType = ctx.getSharedContext().getCodecRegistry().extractJmfType(parameterizedJmfType);
061                if (jmfType != JMF_BOOLEAN_OBJECT)
062                        throw newBadTypeJMFEncodingException(jmfType, parameterizedJmfType);
063                
064                return Boolean.valueOf(((parameterizedJmfType & 0x80) != 0));
065        }
066
067        public void encodePrimitive(OutputContext ctx, boolean v) throws IOException {
068                if (v)
069                        ctx.getOutputStream().write(0x80 | JMF_BOOLEAN);
070                else
071                        ctx.getOutputStream().write(JMF_BOOLEAN);
072        }
073        
074        public boolean decodePrimitive(InputContext ctx) throws IOException {
075                int parameterizedJmfType = ctx.safeRead();
076                int jmfType = ctx.getSharedContext().getCodecRegistry().extractJmfType(parameterizedJmfType);
077                
078                if (jmfType != JMF_BOOLEAN)
079                        throw newBadTypeJMFEncodingException(jmfType, parameterizedJmfType);
080                
081                return ((parameterizedJmfType & 0x80) != 0);
082        }
083
084        public void dump(DumpContext ctx, int parameterizedJmfType) throws IOException {
085                int jmfType = ctx.getSharedContext().getCodecRegistry().extractJmfType(parameterizedJmfType);
086                
087                switch (jmfType) {
088                        case JMF_BOOLEAN:
089                                ctx.indentPrintLn("boolean: " + ((parameterizedJmfType & 0x80) != 0));
090                                break;
091                        case JMF_BOOLEAN_OBJECT:
092                                ctx.indentPrintLn(Boolean.class.getName() + ": " + ((parameterizedJmfType & 0x80) != 0));
093                                break;
094                        default:
095                                throw newBadTypeJMFEncodingException(jmfType, parameterizedJmfType);
096                }
097        }
098}