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.class;
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                return Boolean.valueOf(((parameterizedJmfType & 0x80) != 0));
061        }
062
063        public void encodePrimitive(OutputContext ctx, boolean v) throws IOException {
064                if (v)
065                        ctx.getOutputStream().write(0x80 | JMF_BOOLEAN);
066                else
067                        ctx.getOutputStream().write(JMF_BOOLEAN);
068        }
069        
070        public boolean decodePrimitive(InputContext ctx) throws IOException {
071                int parameterizedJmfType = ctx.safeRead();
072                return ((parameterizedJmfType & 0x80) != 0);
073        }
074
075        public void dump(DumpContext ctx, int parameterizedJmfType) throws IOException {
076                int jmfType = ctx.getSharedContext().getCodecRegistry().extractJmfType(parameterizedJmfType);
077                
078                switch (jmfType) {
079                        case JMF_BOOLEAN:
080                                ctx.indentPrintLn("boolean: " + ((parameterizedJmfType & 0x80) != 0));
081                                break;
082                        case JMF_BOOLEAN_OBJECT:
083                                ctx.indentPrintLn(Boolean.class.getName() + ": " + ((parameterizedJmfType & 0x80) != 0));
084                                break;
085                        default:
086                                throw newBadTypeJMFEncodingException(jmfType, parameterizedJmfType);
087                }
088        }
089}