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